Upst24105 - Problem Solving and Python Programming
Upst24105 - Problem Solving and Python Programming
min ← list[0]
DISPLAY min
STOP
11 b(i) Algorithm to Compute the Factorial of a Given Number (2 marks)
Step 1: Start
Step 2: Read the value of n
Step 3: Initialize factorial = 1
Step 4: IF n is 0 or 1, then
- Set factorial = 1
- Display factorial and Stop
Step 5: FOR i from 2 to n
- Multiply factorial by i
Step 6: Display factorial
Step 7: Stop
Pseudocode (2 marks)
START
READ n
factorial ← 1
IF n = 0 OR n = 1 THEN
DISPLAY 1
STOP
ENDIF
FOR i ← 2 TO n DO
factorial ← factorial * i
ENDFOR
DISPLAY factorial
STOP
Flow Chart: (4 marks)
Output: (3 marks)
Enter a number: 153
153 is an Armstrong number.
13 a
Write a program that prints all numbers from 1 to 10. (16 marks)
for num in range(1, 11):
print(num) # Print the current number
if num == 5:
break # Stop the loop when the number 5 is reached
Output:
1
2
3
4
5
(5 marks)
b Linear Search:
def linear_search(lst, target):
for index, value in enumerate(lst):
if value == target:
return index # Return the index where the target is found
return -1 # Return -1 if target is not found
# Test the linear search method
lst = [21, 34, 56, 31, 18, 23, 38]
target = 31
result_linear = linear_search(lst, target)
print(f "Linear Search: Target {target} found at index {result_linear}")
Output: (3 marks)
Linear Search: Target 31 found at index 3
Binary Search: (5 marks)
def binary_search(lst, target):
lst.sort() # Sort the list first
low = 0
high = len(lst) - 1
while low <= high:
mid = (low + high) // 2
if lst[mid] == target:
return mid # Return the index where the target is found
elif lst[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1 # Return -1 if target is not found
# Test the binary search method
lst = [21, 34, 56, 31, 18, 23, 38]
target = 31
result_binary = binary_search(lst, target)
print(f"Binary Search: Target {target} found at index {result_binary}")
Output: (3 marks)
Target 31 found at index 3
14 a 1.Creating List: (4 marks)
A list in Python is created by enclosing elements in square brackets []. Elements in the list are
separated by commas.
Syntax:
list_name = [element1, element2, element3, ...]
Example:
my_list = [10, 20, 30, 40, 50]
Syntax: (4 marks)
my_dict = {key1: value1, key2: value2, key3: value3}
Key: The unique identifier for each value (e.g., a string, number, or tuple).
Value: The associated data (e.g., any data type, including lists or other dictionaries).
1. Creating a Dictionary
You can create a dictionary using curly braces {} with key-value pairs.
You can access the value associated with a key by using the key inside square brackets [].
print(my_dict["name"])
Output:
Alice
You can modify the value of an existing key by assigning a new value to the key.
my_dict["age"] = 26
print(my_dict)
Output:
You can also use the update() method to add multiple key-value pairs or modify existing ones.
print(my_dict)
Output:
You can add new key-value pairs by assigning a value to a new key.
my_dict["email"] = "[email protected]"
print(my_dict)
Output:
{"name": "Alice", "age": 26, "city": "Los Angeles", "salary": 50000, "email":
"[email protected]"}
You can remove a key-value pair using the del statement or the pop() method.
Using del: