0% found this document useful (0 votes)
6 views

191CSO605T Foundations for Problem Solving using Python(1)

This document is a question paper for a B.E./B.Tech degree continuous assessment test on Python programming, covering problem-solving techniques, Python syntax, control structures, and data structures. It includes various parts with questions requiring pseudocode, programming tasks, and theoretical explanations related to Python. The test aims to evaluate students' understanding and application of foundational concepts in Python as per the course outcomes.

Uploaded by

dharune2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

191CSO605T Foundations for Problem Solving using Python(1)

This document is a question paper for a B.E./B.Tech degree continuous assessment test on Python programming, covering problem-solving techniques, Python syntax, control structures, and data structures. It includes various parts with questions requiring pseudocode, programming tasks, and theoretical explanations related to Python. The test aims to evaluate students' understanding and application of foundational concepts in Python as per the course outcomes.

Uploaded by

dharune2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Register Number

Question Paper Code: 1C191CSO605B

B.E. / B.TECH. DEGREE CONTINUOUS ASSESSMENT TEST –


1, MARCH 2025
Sixth Semester
191CSO605T – Foundations for Problem Solving using Python (Common to AUTO, CIVIL and
MECH Branches) Regulations R 2019

Time: 90 Minutes Maximum: 50 Marks

Course Outcomes: The student will be able to


CO1: Apply fundamental problem solving techniques.
CO2: Explain the basics of Python programming, including syntax, variables, and
input/output operations.
CO3: Apply control structures, loops, and functions to solve basic computational problems.
CO4: Analyze and utilize appropriate data structures like lists, tuples, sets, and dictionaries
for various applications.
CO5: Design modular programs using Python functions and libraries to solve real-world
problems effectively.

K1-Remember K2-Understand K3-Apply K4-Analyse K5-Evaluate K6-Create

PART A – (5 x 2 = 10 marks)

Answer ALL Questions

1. List down the steps involved in problem solving strategies. CO1 K1


Step 1 – Define the problem. ...
Step 2 – Diagnose the root cause. ...
Step 3 – Identify and implement a solution. ...
Step 4 – Sustain the results.

2. Write a pseudocode for calculating the sum of digits of a number. CO1 K2


Start.
Initialize sum to 0.
Input a number (num)
Set temp = num.
While temp > 0.
a. Extract the last digit (digit) using temp % 10.
b. Add digit to sum.
c. Update temp by removing the last digit (temp = temp / 10)
Output "The sum of digits of", num, "is:", sum.
End.
3. How will you perform the following operations: CO2 K3
(a) Print imaginary part out of 2 + 3j.

(b) Obtain conjugate of 4 + 2j.


(a) Print imaginary part out of 2 + 3j.

Page 1 of 8
Program:

#Print imaginary part of 2+3j


cmplx = 2 + 3j
print(cmplx.imag)

Output :
3.0

(b) Obtain conjugate of 4 + 2j.


Program:

# Obtain conjugate of 4+2j


cmplx = 4+2j
print(cmplx.conjugate())

Output :
(4-2j)

4. What is an indentation in python? Give an example. CO2 K1


Indentation refers to the spaces or tabs at the beginning of a line of code. In Python, indentation is
mandatory to define blocks of code, unlike other languages that use {} (curly brackets). Incorrect
indentation will result in an IndentationError.
# Correct indentation
if True:
print("Hello, World!") # Indented block inside if statement
print("Python uses indentation.")

# Incorrect indentation (will cause an error)


if True:
print("This will cause an IndentationError") # No indentation

Key Points About Indentation:


✔ Used in loops, functions, conditional statements, etc.
✔ Typically 4 spaces per level (PEP 8 recommendation).
✔ Inconsistent indentation leads to errors.

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: "))

count = 0 # To keep track of numbers printed


num = 5 # Start from the first multiple of 5

while count < n:


print(num, end=" ")
num += 5 # Move to the next multiple of 5
count += 1
Output :
Enter the number of multiples of 5 to print: 10
5 10 15 20 25 30 35 40 45 50

PART B – (2 x 13 = 26 marks)

Answer ALL Questions

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).")

# Taking user input


try:
age = int(input("Enter your age: "))
if age < 0:
print("Age cannot be negative. Please enter a valid age.")
else:
check_voting_eligibility(age)
except ValueError:
print("Invalid input. Please enter a valid number.")
Output :
Enter your age: 20
You are eligible to vote in the assembly election.
Enter your age: 15
You are not eligible to vote. You need to wait 3 more year(s).
Enter your age: -1
Age cannot be negative. Please enter a valid age.
Enter your age: *
Invalid input. Please enter a valid number.

(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? "))

# first two terms


a, b = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return a
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(a)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(a)
c= a + b
# update values
a=b
b=c
count += 1
Output :
How many terms? 5
Fibonacci sequence:
0
1
1
2
3

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)

9. Evaluate the following expressions based on the precedence of operators: CO2 K4


(a) 2 ** 6 // 8 % 2
(b) 9 ** 2 // 5 – 3
(c) 10 + 6 - 2 % 3 + 7 – 2
(d) 5 % 10 + 10 -23 * 4 // 3
(e) 5 + 5 // 5 - 5 * 5 ** 5 % 5
# Order of evaluation: “exponentiation, floor division, modulo operation”
Soln :
Step 1 : 2**6 soln is 64
Step 2 : 64 // 8 soln is 8
Step 3 : 8 % 2 soln is 0

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)

Answer ALL Questions

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

Pseudocode for Binary Search:


Code snippet
FUNCTION binarySearch(sortedArray, target)
left = 0
right = sortedArray.LENGTH - 1

WHILE left <= right DO


mid = (left + right) / 2 // Integer division
IF sortedArray[mid] == target THEN
RETURN mid // Target found at index mid
ELSE IF sortedArray[mid] < target THEN
left = mid + 1 // Search in the right half
ELSE
right = mid - 1 // Search in the left half
END IF
END WHILE

RETURN -1 // Target not found


END FUNCTION

Time Complexity Analysis:

Linear Search:

Worst-case: O(n) - The target is at the end or not present.

Average-case: O(n)

Best-case: O(1) - The target is the first element.

Page 6 of 8
Binary Search:

Worst-case: O(log n) - The search space is halved with each step.

Average-case: O(log n)

Best-case: O(1) - The target is the middle element.

(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?

Pseudocode for Sum of Digits:

Code snippet

FUNCTION sumOfDigits(number)

IF number < 0 THEN

number = ABSOLUTE(number) // Handle negative numbers

END IF

sum = 0

WHILE number > 0 DO

digit = number MOD 10 // Extract the last digit

sum = sum + digit

number = number DIV 10 // Remove the last digit

END WHILE

RETURN sum

END FUNCTION

Modified Pseudocode for Extremely Large Numbers (String Representation):

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

You might also like