Python S1 Answer Key
Python S1 Answer Key
To solve a jigsaw puzzle without a reference picture using trial and error:
input_str = "12345"
if input_str.isdigit():
print("The input is numeric")
else:
print("The input is not numeric")
START
│
▼
Input n (number of terms)
│
▼
Initialize a=0, b=1, count=0
│
▼
While count < n:
│
├─► Print a
│ │
│ ▼
│ next = a + b
│ │
│ ▼
│ a = b
│ │
│ ▼
│ b = next
│ │
│ ▼
│ count += 1
│
▼
END
1. Start
2. Read input number
3. Initialize reverse = 0 and temp = input number
4. While temp > 0:
a. remainder = temp % 10
b. reverse = (reverse * 10) + remainder
c. temp = temp // 10
5. If input number equals reverse:
a. Print "Palindrome"
Else:
a. Print "Not Palindrome"
6. End
(i) -10-12-14-16-18
(ii)
10
8
6
4
2
0
(iii) 162 (evaluated as 2*(3(22)) = 2*(3^4) = 2*81 = 162
1. Start
2. Initialize attempt = 0000
3. While attempt <= 9999:
a. Try login with attempt
b. If successful:
i. Return attempt as password
ii. Exit
c. Increment attempt by 1
4. If no match found after 10000 attempts, return "Password not found"
5. End
Module 1
For each digit, check if it's valid (no conflict in row, column, or 3x3 box)
If valid, place the digit and recursively attempt to solve the rest
Select operators:
import math
def calculate_radius(area):
return math.sqrt(area / math.pi)
Module 2
FUNCTION calculateGrade(marks):
IF marks >= 90:
base_grade = 'A'
ELSE IF marks >= 80:
base_grade = 'B'
ELSE IF marks >= 70:
base_grade = 'C'
ELSE IF marks >= 60:
base_grade = 'D'
ELSE IF marks >= 50:
base_grade = 'E'
ELSE:
RETURN 'F'
Problem Statement:
Identify and print delivery times (in minutes) that are:
Algorithm (Pseudocode):
FUNCTION findOptimalDeliveries(delivery_times):
optimal_times = EMPTY_LIST
RETURN optimal_times
END FUNCTION
FUNCTION findMinMax(numbers):
IF length of numbers == 0:
RETURN error
min_num = numbers[0]
max_num = numbers[0]
FUNCTION factorial(n):
IF n < 0:
RETURN error
result = 1
FOR i FROM 1 TO n:
result = result * i
RETURN result
END FUNCTION
Module 3
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
seq = fibonacci(n-1)
seq.append(seq[-1] + seq[-2])
return seq
def reorganize_text(sentence):
capitals = []
lowers = []
digits = []
specials = []
Module 4
Algorithm:
If array has only 2 elements, return their sum
Else:
a. Split array into two halves
b. Recursively find two smallest in left half (L1, L2)
c. Recursively find two smallest in right half (R1, R2)
d. Merge results by comparing the four values
e. Return sum of the two smallest from the merged set
Example:
Input: [15, 10, 8, 12, 20, 5]
Return sum: 13
Greedy Approach:
Dynamic Programming:
Comparison:
Memoization (Top-down):
Recursive approach
Example:
python
memo = {0:0, 1:1}
def fib_memo(n):
if n not in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]
Tabulation (Bottom-up):
Iterative approach
Example:
python
def fib_tab(n):
table = [0, 1]
for i in range(2, n+1):
table.append(table[i-1] + table[i-2])
return table[n]
16b. Greedy algorithm for maximum tasks
Algorithm:
Return count
Reasoning: