191CSO605T Foundations for Problem Solving using Python(1)
191CSO605T Foundations for Problem Solving using Python(1)
PART A – (5 x 2 = 10 marks)
Page 1 of 8
Program:
Output :
3.0
Output :
(4-2j)
5. CO2 K3
Write a Python program to print the first n natural numbers that are divisible by 5 using a while loop.
Program :
n = int(input("Enter the number of multiples of 5 to print: "))
PART B – (2 x 13 = 26 marks)
Page 2 of 8
6.
i. An Indian citizen has to vote for the assembly election. Write a Python program to determine
CO3 K5
whether that person is eligible to vote or not. If he is not eligible, display how many years are left to
be eligible.
ii. Prompt the user to get three digit number and then use the Python code to calculate the sum of the
digits. Display the Error message when two digit or single digit number given as input.
def check_voting_eligibility(age):
if age >= 18:
print("You are eligible to vote in the assembly election.")
else:
years_left = 18 - age
print(f"You are not eligible to vote. You need to wait {years_left} more year(s).")
(ii) Prompt the user to get three digit number and then use the Python code to calculate the sum of the
digits
Program :
sumofdigits = 0
num = int (input(" Enter the Number"))
while (num != 0):
temp = num % 10
sumofdigits = sumofdigits + temp
num = num // 10
print("The sum of digits is ", sumofdigits)
Output :
Enter the Number123
The sum of digits is 6
(OR)
7.
i. Develop a Python code to check for palindrome number.
CO3 K4
ii. Develop a Python code to print the Fibonacci series upto “N” terms.
Program :
num = int (input (" Enter the Number"))
temp = num
reverse = 0
while temp > 0:
remainder = temp % 10
reverse = (reverse * 10) + remainder
temp = temp // 10
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
Output :
Enter the Number1221
Page 3 of 8
Palindrome
Enter the Number1234
Not Palindrome
(ii) Develop a Python code to print the Fibonacci series upto “N” terms
Program :
nterms = int(input("How many terms? "))
8. Demonstrate the steps to install Python on your operating system and verify the installation. CO2 K4
Python Installation under Windows
Python has evolved over the years. At the time of writing of this edition the latest version for Windows
and Linux environments was Python 3.8.2.
Python is not shipped as part of Windows OS. So we need to install it separately. For this we need to
download the Python installer from www.python.org/downloads/. (http://www.python.org/downloads/. )
While downloading ensure that you choose the appropriate installer from the following, based on
whether you wish to install it on a 32-bit machine or a 64-bit machine:
64-bit machine: Download Windows x86-64 executable installer
32-bit machine: Download Windows x86 executable installer
Once you have chosen and downloaded an installer, execute it by double-clicking on the downloaded
file.
In this dialog check the check box 'Add Python 3.8 to PATH' to ensure that the interpreter will be
placed in your execution path.
Click on 'Install Now' and the installation will happen in a few
minutes. Python files will get installed in the directory:
C:\Users\Kanetkar\AppData\Local\Programs\Python\Python 38-32
In this path ‘Kanetkar’ will be substituted by your user name and ‘Python38-32’ by version number of
Python installation that you have downloaded and installed.
If you forget to check the check box, you can add the path mentioned above to PATH variable through
Control Panel | System | Environment Variables | Edit. The PATH variable already contains many
semicolon separated values. Append the above path to existing values.
Page 4 of 8
(OR)
Output is 0
(b) 9 ** 2 // 5 - 3
# Order of evaluation: exponentiation, floor division, subtraction
Soln :
Step 1: 9**2 soln is 81
Step 2: 81 // 5 Soln is 16
Step 3 : 16 - 3 Soln is 13
Output : 13
(c) 10 + 6 - 2 % 3 + 7 - 2
Soln :
Step 1: 10 +6 =16
Step 2: 2%3 = 2
Step 3 : 16-2 =14
Step 4 : 14+7= 21
Step 5 : 21-2 = 19
Output is 19
(d) 5 % 10 + 10 -23 * 4 // 3
Soln :
Step 1: 23 * 4 = 92
Step 2: 92 // 3 = 30.66(So Consider integer value )
Step 3 : 5 % 10 = 5
Step 4 : 5 + 10 = 15
Step 5 : 15 - 30 = -15
Output : -15
(e) 5 + 5 // 5 - 5 * 5 ** 5 % 5
Soln :
Step 1: 5 ** 5 = 3125
Step 2 : 5 + 5 // 5 - 5 * 3125 % 5
Take 5 * 3125 = 15625
Step 3 : 5 + 5 // 5 - 15625 % 5
Take 15625 % 5 = 0
Step 4: 5 + 5 // 5 - 0
Take 5 // 5 = 1
Step 5 : 5 +1 - 0
Take 5 + 1 = 6
Step 6 : 6 - 0 = 6
Output is 6
Page 5 of 8
PART C – (1 x 14 = 14 marks)
10. Design an algorithm (in pseudocode) to check whether two strings are anagrams. Discuss the different CO1 K5
methods that can be used for checking anagrams and compare their time and space complexities.
What are the trade-offs between these methods?
Pseudocode for Linear Search:
Code snippet
FUNCTION linearSearch(array, target)
FOR i = 0 TO array.LENGTH - 1 DO
IF array[i] == target THEN
RETURN i // Target found at index i
END IF
END FOR
RETURN -1 // Target not found
END FUNCTION
Linear Search:
Average-case: O(n)
Page 6 of 8
Binary Search:
Average-case: O(log n)
(OR)
11. Write an algorithm (in pseudocode) to find the sum of digits of♀a number. Discuss the efficiency of CO1 K5
your algorithm in terms of both time complexity and space complexity. How would the algorithm
change if the number is extremely large?
Code snippet
FUNCTION sumOfDigits(number)
END IF
sum = 0
END WHILE
RETURN sum
END FUNCTION
Code snippet
FUNCTION sumOfDigitsLarge(numberString)
sum = 0
FOR i = 0 TO numberString.LENGTH - 1 DO
digitChar = numberString[i]
digit = CONVERT_CHARACTER_TO_INTEGER(digitChar)
Page 7 of 8
sum = sum + digit
END FOR
RETURN sum
END FUNCTION
Page 8 of 8