Algorithms_and_Programming_Exercises_Memo
Algorithms_and_Programming_Exercises_Memo
1. An algorithm is implemented in Python using code structures such as functions, loops, and conditionals.
2. c) A reusable block of code that performs a task
3. def max_of_three(a, b, c): return max(a, b, c)
4. Indentation defines blocks of code. Missing indentation causes syntax errors.
5. a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print(a + b)
6. Output: 1
7. b) value_2
8. name = input("What is your name?") print(f"Hello, {name}!")
9. range(1, 6)
10. = assigns a value; == compares two values. Example: x = 5; if x == 5: print("Yes")
11. Use if-elif-else to check number sign.
12. Output: False; because a and b = True and False
13. for loops for known repetitions; while loops for unknown condition-based repetition.
14. Use loop: result = 1; for i in range(1, n+1): result *= i
15. c) Timsort (used by sorted())