Example 1
Explanation
- This function multiplies two numbers by adding the first number to itself as many times as the second number indicates
- It uses a loop with range() to repeat the addition process exactly 'b' times
- The key operations are initializing result to 0, then repeatedly adding 'a' to it inside the loop
- The function prints the final multiplication result instead of returning it
- When called with multiply(3,4), it calculates 3+3+3+3 and prints 12
Output
Example 2
Explanation
- This code implements multiplication using recursion by adding a number to itself b times
- The
mulfunction takes two parameters a and b, and recursively calculates their product - When b equals 1, it returns a as the base case to stop the recursion
- For other values, it adds a to the result of multiplying a by (b-1), effectively building up the total
- The print statement calls mul(5,6) which outputs 30, showing 5 multiplied by 6 using recursive addition
Output
Example 3
Explanation
- This code defines a recursive function called
factthat calculates the factorial of a given number - The function works by checking if the number equals 1, and if so, returns 1 as the base case
- For any other number, it multiplies the number by the result of calling itself with one less than the current number
- The
print(fact(5))statement calls the function with the argument 5 and displays the result - The expected output is 120, which is 5! (5 factorial = 5 × 4 × 3 × 2 × 1)
Output
Example 4
Explanation
- This code defines a recursive function that checks if a string is a palindrome by comparing the first and last characters
- The function uses string slicing (
text[1:-1]) to remove the first and last characters and recursively check the remaining substring - When the string length is 1 or less, it prints 'Palindrome', otherwise it continues checking character by character
- For 'madam' and 'malayalam', it will print 'Palindrome' since they read the same forwards and backwards
- For 'python' and 'abba', it will print 'Not a Palindrome' for 'python' and likely 'Palindrome' for 'abba' (though the logic has a flaw in handling even-length strings correctly)
Output
Example 5
Explanation
- This code defines a recursive function called
fibthat calculates Fibonacci numbers where each number is the sum of the two preceding ones - The function has a base case for months 0 and 1, returning 1, and uses recursion to calculate higher values by adding the results of calling itself with smaller inputs
- The
print(fib(5))statement calls the function with input 5 and displays the result, which is 8 - Key APIs/functions used include basic conditional statements (if/else), function definition (def), and recursive function calls
- The side effect is printing the calculated Fibonacci number to the console, with no other visible output or state changes
Output
Example 6
Explanation
- This code implements a memoized recursive Fibonacci-like function that calculates values based on the sum of previous two values
- The
memofunction uses a dictionarydto store previously calculated results to avoid redundant computations - Key APIs used include dictionary lookup (
if m in d) and recursive function calls (memo(m-1,d) + memo(m-2,d)) - The function initializes a dictionary with base cases
{0:1, 1:1}and computes the 48th value in the sequence - Side effect is that the dictionary
dgets populated with all intermediate calculated values from 0 to 48
Output

