Module 2: Problem Solving Techniques Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CDS/CDS101/IISERBPR

Module 2: Problem Solving Techniques


Unit 1
Lecture 1:
Q.1) What is the difference between declarative and imperative knowledge?
Q.2) What is the advantage of a stored-program computer?
Q.3) What are the syntax, static semantics, and semantics of a language?
Q.4) What sorts of errors can occur in a program?

Lecture 2:
A Very Simple Program: Entering and Printing Your Name The goal of this programming exercise is
simply to get you more comfortable with using IDLE, and to begin using simple elements of Python.
Standard elements of a program include the ability to print out results (using the print operation), the
ability to read input from a user at the console (for example using the raw input or input function),
and the ability to store values in a variable, so that the program can access that value as needed.

Problem 1
Learn Python for Beginners:

https://www.python.org/about/gettingstarted/

or

https://pynative.com/

Write a program that does the following in order:

Asks the user to enter his/her date of birth.

Asks the user to enter his/her last name.

Prints out the user’s last name and date of birth, in that order.

An example of an interaction with your program is shown below (the words printed in blue are from
the computer, based on your commands, the words in black are a user’s input – the colors are simply
here to help you distinguish the two components):

Hints: https://python.swaroopch.com/basics.html

pg. 1
CDS/CDS101/IISERBPR

Hand-In Procedure

1. Save your code in PS1.py Do not ignore this step or save your file(s) with different names.

Lecture 3:
Q.1) What does it mean for a program to terminate?
Q.2) What is a for loop?

Problem 1:
Python Program to Check If A Number Is A Perfect Square? Run the program for some test cases
gave the following output,

Hint: Square root of the number is calculated with math.sqrt method and the result is stored in root
variable
https://djangocentral.com/python-program-to-check-if-a-number-is-perfect-square/

Hand-In Procedure

1. Save your code in PS2.py Do not ignore this step or save your file(s) with different names.

2.Submit

Lecture 4:
Q.1) What is decomposition?
Q.2) What is abstraction?

Q.3) What is the difference between formal and actual parameters?


Problem 3:

1: Calculate the multiplication and sum of two numbers

Given two integer numbers return their product only if the product is equal to or lower than 100,
else return their sum.

Output Looks like

pg. 2
CDS/CDS101/IISERBPR

Show Hint

 Create a function that will take two numbers as parameters


 Next, inside a function, multiply two numbers and save their product in a product variable
 Next, use the if condition to check if the product >1000. If yes, return the product
 Otherwise, use the else block to calculate the sum of two numbers and return it.

Hand-In Procedure

1. Save your code in PS3.py Do not ignore this step or save your file(s) with different names.

2.Submit

Problem 4:

Print the sum of the current number and the previous number. Write a program to iterate the
first 10 numbers and in each iteration, print the sum of the current and previous number.

Expected Output:

Show Hint

 Create a variable called previous_num and assign it to 0


 Iterate the first 10 numbers one by one using for loop and range () function

pg. 3
CDS/CDS101/IISERBPR

 Next, display the current number (i), previous number, and the addition of both numbers in
each iteration of the loop. At last, change the value previous number to current number
(previous_num = i).
Problem 5:

Print characters from a string that are present at an even index number

Write a program to accept a string from the user and display characters that are present at an even
index number.

For example, str = "HelloDear" so you should display ‘H’, ‘l’, ‘o’, ‘e’, ’r’.

Expected Output:

Problem 6:

Remove first n characters from a string

Write a program to remove characters from a string starting from zero up to n and return a new
string.

For example: remove_chars("HelloDear", 4) so output must be oDear. Here we need to remove first
four characters from a string.

Note: n must be less than the length of the string.

Expected Output:

Hint: Use string slicing to get the substring. For example, to remove the first four characters and the
remeaning use s[4:].

pg. 4
CDS/CDS101/IISERBPR

Problem 7:

Check if the first and last number of a list is the same

Write a function to return True if the first and last number of a given list is same. If numbers are
different then return False.

Expected Output:

Problem 8:

Display numbers divisible by 6 from a list

Iterate the given list of numbers and print only those numbers which are divisible by 6

Expected Output:

Problem 9:

Return the count of a given substring from a string

Write a program to find how many times substring “Science” appears in the given string.

Given:

str_x =” Science Science everywhere science”

Hint:

Use string method count().

Expected Output:

Problem 10:

Print the following pattern

pg. 5
CDS/CDS101/IISERBPR

22

333

4444

55555

Lecture 5:
Q.1) What is mutability?
Q.2) What is the important difference between a list and a tuple?
Q.3) What is cloning?

Q.4) What are the important aspects of a dictionary?

Lecture 6:
Q.1) What is recursion?

Q.2) What is a recursive case?

Q.3) What is a base case?

Q.4) What is a base case?

Lecture 7:
Q.1) Why do computers use binary representations?

Q.2) Why shouldn’t we test for equality with floats?

Q.3) When debugging, how can you ensure that the values in your program are the ones you think
they are?

Lecture 8:
Q.1) Why is efficiency important?

pg. 6
CDS/CDS101/IISERBPR

Q.2) What notation do we use to state complexity?

Lecture 9:
Q.1) What is indirection (in computing)?

Q.2) We know that a linear search works on all lists and is O(len(L)). Can we sort a list in sub-
linear time?

Q.3) Can we even do it in linear time?

Unit 1 Completed

pg. 7

You might also like