Ucest105 Algorithmic Thinking With Python, May 2025 Solved
Ucest105 Algorithmic Thinking With Python, May 2025 Solved
PART A
Identify the most suitable problem-solving strategies for the following use Col (3)
i) You are solving a complex Sudoku puzzle and are unsure how to
ii) You are tasked with planning a family trip that involves selecting a
What will be the result of the following expressions in Python? Col, (3)
i) (50 - 5 * 6) // 4 •C04
at are the advantages of using pseudocode overdirectly writing the code? - C02 (3)
mbers - [10, 20, 30, 40, 50, 60, 70, 80, 90] C04
Write the slicing expressions to:
Page 1 of 4
03UCEST 10512240 i
What is circularity in recursion? How can you prevent circularity in recursive C03 (3)
functions?
Compare Dynamic programming and Recursioli in terms of their approach, C04 (3)
What is the brute-force computational problem-solving approach, and how C04 (3)
PART 8
(Answer any one full question from each module, each question carries 9 marks)
Module -1
9 a) Walkthrough the six problem-solving steps to calculate the area ofa circle. Col (6)
C04
10 a) What isthe difference between algorithms and heuristics in problem-solving? Col (5)
algorithm.
the shop offers a 10% discount on all mobiles. In addition, the shop also gives
a flat exchange price of 1000 for old mobiles. Draw a flowchart to input the
original price of the mobile and print its selling price. Note that all customers
Page 2 of 4
03UCEST 105122401
b) If the three sides ofa triangle are input, write an algorithm/pseudocode to C02 (4)
sequentially.
Module -3
13 a) What arethe primary differences between lists, tuples, and strings in python C03, (3)
number is a valid mobile number or not. The program should validate the
14 a) Write a python program to Input two lists from the user. Merge these lists C03, (4)
into a third list such that in the merged list, all even numbers occur first C04
followed by odd numbers. Both the even numbers and odd numbers should be
in sorted order.
b) Write a recursive function to multiply two positive numbers. Trace the C03 (5)
function calls for multiplying 3 and 4, and explain the changes in the call
Module -4
merge sort algorithm. Draw a diagram showing how the array is split and
Page 3 of 4
03UCEST 10512240 l
b) What is memoization, and how does it differ from tabulation in dynamic C04 (3)
- programming?
16 a) A vending machine needs to return change to a customer using the least C04 (5)
combination of coins that minimizes the total number of coins used to make a
b) Compare Greedy Algorithms and Dynamic programming in terms of their C04 (4)
Page 4 of 4
UCEST105 ALGORITHMIC THINKING WITH PYTHON - SOLVED PYQ
1. Identify the most suitable problem-solving strategies for the following use cases. Justify your
answer:
i) You are solving a complex Sudoku puzzle and are unsure how to proceed as you approach the
final few cells. (3 Marks)
Strategy: Backtracking.
Justification: Sudoku is a classic constraint satisfaction problem. When nearing the final cells, if a
number is placed and later found to violate a rule (i.e., a dead end is reached), the solver must undo
the last move and systematically try an alternative number. Backtracking is the strategy that
involves building a solution step-by-step and abandoning (backtracking) any partial path that is
found not to satisfy all constraints.
ii) You are tasked with planning a family trip that involves selecting a destination, booking flights,
and finding accommodation within a budget. (3 Marks)
Justification: MEA is suitable because the overall goal (a successful trip within budget) requires
identifying and reducing the differences between the current state (no plan) and the desired final
state. The planning is divided into sub-problems (ends), such as "find accommodation," "book
flights," and "select destination." MEA systematically addresses these sub-goals, ensuring constraints
like the budget are maintained throughout the process.
i) (50 - 5 * 6) // 4
Result: 5
• Step 1 (Multiplication): 5 * 6 = 30
• Step 2 (Subtraction): 50 - 30 = 20
Result: True
Result: 5
• In Python, non-zero numerical values are considered logically True. In a boolean AND
operation, if the first operand is true (True), the result is the second operand.
3. What are the advantages of using pseudocode over directly writing the code? (3 Marks)
1. Clarity and Readability: It uses natural language and mathematical notation, making the
logic easy to follow for both technical and non-technical individuals, irrespective of the
target programming language.
2. Focus on Logic: It allows the developer to concentrate purely on the structural logic of the
algorithm without being constrained by the strict syntax rules of a specific programming
language.
(Self-Correction: Since I cannot draw a diagram, I will describe the required flowchart structure and
steps.)
Flowchart Description:
6. Loop Back: Draw a flowline returning to the Decision Loop (Step 3).
numbers[:5]
numbers[::-1]
6. What is circularity in recursion? How can you prevent circularity in recursive functions? (3
Marks)
Circularity in Recursion: Circularity, also known as infinite recursion, occurs when a recursive
function calls itself indefinitely. This happens because the function fails to reach a base case, or the
input modifications in the recursive step do not drive the function toward the base case. This
ultimately causes the program to crash due to a stack overflow error.
1. Defining a Base Case: A clear condition must be defined that stops the recursion and returns
a value without making further recursive calls.
2. Guaranteeing Convergence: Each recursive call must operate on a modified input that is
strictly "closer" to satisfying the base case condition than the previous input.
7. Compare Dynamic Programming and Recursion in terms of their approach, efficiency, and use
cases. (3 Marks)
8. What is the brute-force computational problem-solving approach, and how many maximum
attempts are required to guarantee opening a four-digit numerical padlock (digits ranging from 0
to 9) using this approach? (3 Marks)
Maximum Attempts: A four-digit numerical padlock, using digits 0 through 9, has 10 possibilities for
each position.
PART B
(Answer any one full question from each module, each question carries 9 marks)
Module - 1
9. a) Walk through the six problem-solving steps to calculate the area of a circle. (6 Marks)
The calculation of the area of a circle ($\text{Area} = \pi r^2$) follows these typical problem-solving
steps:
1. Understand the Problem (Definition): The goal is to determine the area of a circle. The
required input is the radius ($r$), and the necessary constant is $\pi$. The output must be
the calculated area.
2. Devise a Plan (Algorithm Design): Formulate the sequence of steps. Step 1: Get the radius
$r$. Step 2: Use the formula $A = \pi \times r \times r$. Step 3: Display the result $A$.
3. Carry out the Plan (Implementation): Implement the algorithm using code or pseudocode.
(e.g., In Python: import math; r = input(); area = math.pi * r**2; print(area)).
4. Review/Look Back (Verification): Test the implementation with known values (e.g., if $r=1$,
the area should be $\pi$). Ensure the output matches expectations and handles different
inputs correctly.
5. Refinement: Improve the solution, perhaps by adding input validation (e.g., ensuring the
radius is a positive number).
6. Documentation: Clearly document the purpose, input requirements, formula used, and
expected output of the program.
9. b) What are the basic data types in Python? Provide examples for each. (3 Marks)
10. a) What is the difference between algorithms and heuristics in problem-solving? Provide an
example where heuristics lead to a faster solution than an algorithm. (5 Marks)
Example of Faster Heuristic Solution: Problem: A chess engine needs to determine the best move in
a complex game state.
• Heuristic (Evaluation Function): Chess engines use heuristic evaluation functions (e.g.,
assigning point values to pieces, controlling the center of the board) to quickly evaluate the
quality of a game state. By using this rule of thumb, the engine only searches promising
branches of the game tree, leading to a decision that is fast enough for real-time play, even
if it might not be the absolute mathematical best move.
10. b) Write a Python program to convert the time input in minutes to hours and minutes. For
example, 85 minutes is 1 hour 25 minutes. (4 Marks)
def convert_minutes(total_minutes):
hours = total_minutes // 60
minutes = total_minutes % 60
# Output
input_time = 85
convert_minutes(input_time)
Module - 2
11. a) Draw a flowchart to input the original price of the mobile and print its selling price,
considering a 10% discount and an optional flat exchange price of 1000 for old mobiles. (5 Marks)
(Self-Correction: Since I cannot draw a diagram, I will describe the required flowchart structure and
steps.)
Flowchart Description:
4. Input/Decision: Read Exchange (e.g., 'Y' or 'N' indicating if the customer has an old mobile).
(Input/Output Symbol).
11. b) If the three sides of a triangle are input, write an algorithm/pseudocode to check whether
the triangle is isosceles, equilateral, or scalene. (4 Marks)
// ALGORITHM: CLASSIFY_TRIANGLE_TYPE
START
// Check if sides are valid (Triangle Inequality Check - Optional but robust)
// IF (SideA + SideB <= SideC) OR (SideA + SideC <= SideB) OR (SideB + SideC <= SideA) THEN
// RETURN
ELSE
END
12. a) Write pseudocode to determine the average age of students in a class. The user will stop
giving the input by giving the age as 0. (5 Marks)
// PSEUDOCODE: AVERAGE_AGE_CALCULATOR
START
SET TotalAge = 0
SET StudentCount = 0
LOOP
INPUT Age
IF Age == 0 THEN
BREAK LOOP
StudentCount = StudentCount + 1
END LOOP
ELSE
END
12. b) Draw a flowchart to find the largest number from a series of numbers entered sequentially.
(4 Marks)
(Self-Correction: Since I cannot draw a diagram, I will describe the required flowchart structure and
steps.)
Flowchart Description (Assuming the user inputs N numbers, or uses a simple counter):
2. Initialization: Initialize Largest = -Infinity or read the first input and set Largest to that value.
Initialize Count = 1 and read N (total numbers). (Process/Input Symbols).
Module - 3
13. a) What are the primary differences between lists, tuples, and strings in Python in terms of
mutability, usage, and methods? (3 Marks)
13. b) Write a Python function is_valid_mobile_no to check whether a given number is a valid
mobile number or not, based on the rules: 1. Exactly 10 digits. 2. First digit is 7, 8, or 9. (3 Marks)
def is_valid_mobile_no(number_str):
"""Checks if a string represents a valid mobile number based on rules 1 and 2."""
if len(number_str) != 10:
return False
if not number_str.isdigit():
return False
first_digit = number_str
return False
return True
# Example Tests:
# print(is_valid_mobile_no("9876543210")) # True
13. c) Write a recursive function in python to find the GCD of two numbers. (3 Marks)
The GCD (Greatest Common Divisor) is typically found using the Euclidean algorithm, which is
naturally recursive.
if b == 0:
return a
else:
return recursive_gcd(b, a % b)
# Example Usage
14. a) Write a Python program to Input two lists from the user. Merge these lists into a third list
such that in the merged list, all even numbers occur first followed by odd numbers. Both the even
numbers and odd numbers should be in sorted order. (4 Marks)
list1 =
list2 =
even_list = []
odd_list = []
if num % 2 == 0:
even_list.append(num)
else:
odd_list.append(num)
even_list.sort() #
odd_list.sort() #
# Output:
14. b) Write a recursive function to multiply two positive numbers. Trace the function calls for
multiplying 3 and 4, and explain the changes in the call stack during execution. (5 Marks)
if b == 0:
return 0
return a + multiply_recursive(a, b - 1)
Call Stack Explanation: The call stack grows as each recursive call (from 1 to 4) is pushed onto the
stack. Each frame holds the necessary context (like the return address and local variables) for that
function call to resume when the subsequent call returns. When the base case (Call 5, where $b=0$)
is reached, the recursion stops. The stack then unwinds, meaning the values stored in the stack
frames are processed sequentially from top to bottom, accumulating the results (the additions) until
the initial function call (Call 1) returns the final answer, 12.
Module - 4
15. a) Illustrate the process of sorting the array using the merge sort algorithm. Draw a diagram
showing how the array is split and merged at each stage. (6 Marks)
Merge sort is a Divide and Conquer algorithm that splits the array until single elements remain, and
then merges them back in sorted order.
1. Splitting (Divide Stage): The array is recursively divided into two halves until the sub-arrays
contain only one element. $$
• Merge 1:
o , $\rightarrow$ ``
o , $\rightarrow$ ``
o , $\rightarrow$ ``
o , $\rightarrow$ ``
• Merge 2:
o , $\rightarrow$ ``
o , $\rightarrow$ ``
o , $\rightarrow$ ``
15. b) What is memoization, and how does it differ from tabulation in dynamic programming? (3
Marks)
Uses Recursion with a lookup table (e.g., Uses Iteration (loops) to systematically
Implementation
dictionary or array). fill a table (DP table).
Uses the function call stack heavily, Avoids recursion, relying primarily on
Stack Usage potentially leading to stack overflow loops, and thus generally does not use
issues for extremely deep problems. the call stack for problem solving.
16. a) A vending machine needs to return change to a customer using the least number of coins...
Implement the greedy algorithm to determine the combination of coins that minimizes the total
number of coins used to make a given amount of change. (5 Marks)
The change-making problem, with standard denominations (e.g.,), is optimally solved using the
Greedy Algorithm. The strategy is to always pick the largest coin that is less than or equal to the
remaining amount.
denominations.sort(reverse=True)
change_count = {}
remaining_amount = amount
total_coins = 0
change_count[coin] = count
total_coins += count
remaining_amount %= coin
if remaining_amount == 0:
else:
# This occurs if the denominations cannot make the exact change (not an issue here)
coins =
# Example Trace:
16. b) Compare Greedy Algorithms and Dynamic Programming in terms of their approach, solution
guarantee, and applicable scenarios. Provide examples where each approach is preferred. (4
Marks)
Preferred: Standard currency change- Preferred: 0/1 Knapsack Problem, Matrix Chain
Examples making, Minimum Spanning Trees Multiplication, Fibonacci sequence calculation
(e.g., Prim's algorithm). (efficiently).
Analogy to Clarify: Think of the difference between Dynamic Programming and the Brute Force
approach (which recursion often embodies) as navigating a maze. Brute Force is like exploring every
single dead-end path repeatedly whenever you reach a junction. Dynamic Programming is like
navigating the maze and, every time you reach a junction you've been to before, you immediately
know the outcome of that path without having to walk it again, thanks to a mental map (the cache
or DP table) you built along the way. This drastically speeds up finding the exit.