Solutions (Mid Semester 2024)
1. (a) A company has implemented a password policy for its online accounts that requires users to create
passwords meeting the following criteria: [3]
a. The password must be at least 8 characters long.
b. The password must contain at least one uppercase letter.
c. The password must contain at least one lowercase letter.
d. The password must contain at least one digit.
e. The password must contain at least one special character.
Take input from the user. If input is valid as per above criteria then print login successful otherwise ask
user to reenter the password in loop. Draw flowchart for the given problem.
Solution 1a.
All boxes having correct structure 1 mark
All boxes are in proper sequence and All test cases satisfied 1 mark
Complete information in all the boxes 1 mark
1 (b) Rewrite following code with proper indentation to get the output as [2]
Expected output Code without indentation
cube of 1 is: abc = [1,2,3,4,5]
1 for x in abc:
if x%2 == 0:
square of 2 is: print("square of ", x , "is:")
4 print(x**2)
cube of 3 is: else:
27 print("cube of ", x , "is:")
square of 4 is: print(x**3)
16
cube of 5 is:
125
Solution 1b.
abc = [1,2,3,4,5]
for x in abc:
if x%2 == 0:
print("square of ", x , "is:")
print(x**2)
else:
print("cube of ", x , "is:")
print(x**3)
for indentation of ‘if’ part 1 mark
for complete correct code 2 mark
2. (a)You have three jugs with capacities of 8 litres, 5 litres, and 3 litres. The 8-liter jug is full of water,
and the other two jugs are empty. Your goal is to divide the water so that you have exactly 4 litres of
water in the 5-litres jug. Write the step-by-step process for how to move the water between the jugs to
achieve this result. [3]
Solution 2a.
Initial Setup:
Jug A (8 litres): Full (8 litres of water)
Jug B (5 litres): Empty (0 litres)
Jug C (3 litres): Empty (0 litres)
Step 1:
Pour water from Jug A (8 litres) into Jug C (3 litres) until Jug C is full.
Jug A: 5 litres
Jug B: 0 litres
Jug C: 3 litres
Step 2:
Pour water from Jug C (3 litres) into Jug B (5 litres).
Jug A: 5 litres
Jug B: 3 litres
Jug C: 0 litres
Step 3:
Pour water from Jug A (8 litres) into Jug C (3 litres) until jug C is full.
Jug A: 2 litres
Jug B: 3 litres
Jug C: 3 litres
Step 4:
Pour water from Jug C (3 litres) into Jug B (5 litres) until jug B is full.
Jug A: 2 litres
Jug B: 5 litres
Jug C: 1 litres
Step 5:
Discard the remaining water of Jug C (3 litres).
Jug A: 2 litres
Jug B: 5 litres
Jug C: 0 litres
Step 6:
Pour water from Jug B (5 litres) into Jug C (3 litres) to fill Jug C.
Jug A: 2 litres
Jug B: 2 litres
Jug C: 3 litres
Step 7:
Pour water from Jug A (8 litres) to Jug B (5 litres)
Jug A: 0 litres
Jug B: 4 litres
Jug C: 3 litres
Final Result after discarding the water from Jug C:
Jug A (8 litres): 0 litres
Jug B (5 litres): 4 litres
Jug C (3 litres): 0 liters
Now, you have 4 litres of water in the 5-litres jug (Jug B).
Logically reaching to the correct output 1.5 mark
Clarity and completeness in all the step 1 mark
Information about availability of water at each step 0.5 mark
2 (b) What output do you think the code will produce if aNumber is 30, for the given codes: [2]
(i) (ii)
Solution 2b.
(i) second string (ii) third string (1 mark)
third string (1 mark)
3. (a) Write a python fruitful function count_vowels that takes a statement as string s and returns
the number of vowels (a, e, i, o, u) in it. Use a loop to iterate through the string. [3]
Solution 3a.
Output
For correct function definition statement with return statement and print 0.5 marks
For correct definition and partial correct if-else conditions 1 marks
For correct definition statement with return statement and correct if-else conditions 2 marks
Correct use of local variables 2.5 marks
Correct return of variable to global variables 3
(b) Explain the concept of an infinite loop and how to avoid it with suitable examples? [2]
Solution 3b.
An infinite loop is a loop that continues to execute indefinitely because its terminating condition is
never met. This can lead to the program running endlessly and can be a significant issue. To avoid
infinite loops, it's crucial to set a proper termination condition.
Let's look at an example of an infinite loop and how to avoid it:
# Example: Infinite loop
while True:
print("This is an infinite loop")
# Uncomment the following line to create an infinite loop
# pass
In this example, the while True: creates an infinite loop because the condition True is always true.
Uncommenting the pass statement would make the loop truly infinite.
To avoid an infinite loop, a proper termination condition should be used. Here's an updated example:
# Example: Avoiding infinite loop
counter = 0
while counter < 5:
print("This is iteration", counter + 1)
counter += 1
Output:
This is iteration 1
This is iteration 2
This is iteration 3
This is iteration 4
This is iteration 5
In this corrected example, the loop iterates five times, and the counter variable ensures that the
termination condition is met
For explanation of infinite loop 1 mark
for avoidance answer without example 1.5 mark
for avoidance answer with example 2 mark
4. (a) Write a program to input electricity unit charge and calculate the total electricity bill
according to the given condition: [3]
a. For first 50 units Rs. 0.50/unit
b. For next 100 units Rs. 0.75/unit
c. For next 100 units Rs. 1.20/unit
d. For unit above 250 Rs. 1.50/unit
e. An additional surcharge of 20% is added to the bill.
Solution 4a.
Output:
For correct user input with print line 0.5 marks
For user input and surcharge calculation 1 marks
For user input and surcharge calculation with total_amt 1.5 marks
For partial correct conditions 2/2.5 marks
For complete correct code 3 marks
(b) Write down the difference between iteration and recursion. [2]
Solution 4b.
For each difference 0.5 mark
for equal to and more than 4 differences 2 mark
5. (a) Write a function that checks if two strings are anagrams of each other.
# Example usage
string1 = "Listen"
string2 = "Silent"
Here, both string1 and string2 consist of the same alphabets placed in shuffled order. [3]
Solution 5a.
def are_anagrams(s1, s2):
s1 = s1.replace(" ", "").lower()
s2 = s2.replace(" ", "").lower()
return sorted(s1) == sorted(s2)
# Example usage
string1 = "Listen"
string2 = "Silent"
result = are_anagrams(string1, string2)
print(result)
for creating function and taking input from user using input function, and returning the 0.5
value mark
For partial correct conditions 1.5
mark
for complete correct code 2 mark
(b) What is the output of following program: [2]
n = 5
for i in range(1,n+1):
k = 1
for j in range(1,2*n+1):
if(i+j<=n):
print(" ", end = " ")
elif(i+j-n <= i):
print(i+j-n, end = " ")
elif(i+j-n < 2*i):
print(i-k, end = " ")
k = k + 1
else:
break
print()
Solution 5b.
Output
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Writing correct spaces and correct pattern of numbers 1 mark
Complete correct output along with reasoning 2 mark
6. (a) Write a function to remove duplicates from the given input
string. [3]
Solution 6a.
def remove_duplicates(input_string):
result = []
for char in input_string:
if char not in result:
result.append(char)
return ''.join(result)
input_str = input(“Enter the string”) "programming"
non_duplicate_string = remove_duplicates(input_str)
print("Non-duplicate string:", non_duplicate_string)
For correct user input with print line 0.5 marks
For iterating over string using loop 1.5 marks
For returning output 1 marks
For partial correct conditions 2/2.5 marks
For complete correct code 3 marks
(b) Taking a suitable example code of python programming, illustrate the significance of
global and local variables. [2]
Solution 6b.
Local and global variables are important in programming because they determine the scope
and lifetime of variables in a program. The main difference between local and global
variables is where they are declared and how they can be accessed:
Local variables
Declared inside a function or block, and can only be accessed within that function or block.
Local variables only exist while the function is executing.
Global variables
Declared outside of all functions or blocks, and can be accessed by any function in the
program. Global variables remain in memory for the duration of the program.
For explanation of global and local 1 mark
for explanation with example 2 mark