Example 1
Explanation
- Code measures and prints execution time of a loop that counts from 1 to 9
- Uses
time.time()function to capture timestamps before and after the loop - Records start time before loop begins and end time after loop completes
- Calculates difference between end and start times to show how long loop took
- Prints individual numbers 1 through 9 followed by total execution time in seconds
Output
Example 2
Explanation
- Converts an integer to its string representation by repeatedly extracting digits using modulo and integer division operations
- Uses a digit mapping string '0123456789' to convert numeric values to their character equivalents
- Builds the result string by prepending each digit character to the front of the existing result
- Processes the input number by taking remainder when divided by 10 to get last digit, then integer dividing by 10 to remove it
- Has logarithmic time complexity O(log n) where n is the input number, since it processes one digit per iteration until number becomes zero
Output
Example 3
Explanation
- Code calculates sum and product of all elements in list L by iterating through it twice
- Uses basic for loops with accumulation variables sum and product initialized to 0 and 1 respectively
- Prints final values 10 (1+2+3+4) and 24 (1×2×3×4) to console
- First loop adds each element to running sum total
- Second loop multiplies each element to running product total
- Both operations have linear time complexity O(n) where n is list length
Output
Example 4
Explanation
- This code uses nested loops to iterate through each element of list A and pair it with every element of list B
- The inner loop runs completely for each iteration of the outer loop, creating all possible combinations of elements from both lists
- Key APIs used include basic for loops and the print function to display the paired values
- Expected output shows each element from A paired with every element from B in sequence (1,5), (1,6), (1,7), (1,8), (2,5), etc.
- Time complexity is O(n²) because for each of the n elements in A, we iterate through all n elements in B
Output
Example 5
Explanation
- Code uses nested loops to iterate through elements of list A, then list B, then a range of 5 numbers
- For each combination of i from A and j from B, it prints the pair (i,j) five times due to the innermost loop
- Key APIs/functions:
range(),print(), nested for-loops for iteration - Expected output shows each element from A paired with each element from B, repeated 5 times total per pair
- Time complexity is O(n²) where n is the size of input lists since the constant 5 doesn't affect asymptotic growth
Output
Example 6
Explanation
- This code reverses the elements of a list
Lin-place by swapping elements from both ends toward the center - It uses
range(0, len(L)//2)to iterate only halfway through the list, avoiding redundant swaps - The swapping is done using a temporary variable
tempto hold one value while exchanging positions - Key APIs used include
len()to get list length,range()for iteration, and direct index assignment - Expected output is
[5, 4, 3, 2, 1]- the original list reversed in place
Output
Example 7
Explanation
- The code initializes three variables n=10, j=10, k=0 and then uses nested loops to calculate a value for k
- The outer loop runs from i=n//2 to n-1 (so i goes from 5 to 9), and the inner loop has a strange range call that will likely cause issues
- The inner loop range(2,n,pow(2,j)) is problematic because pow(2,j) uses the loop variable j which is also the loop iterator, making the step size unpredictable
- The calculation k = k + n/2 adds 5 to k during each iteration of the inner loop
- The code attempts to demonstrate O(n log n) complexity but contains a logic error in the inner loop's range parameters that would likely cause runtime issues
Output
Example 8
Explanation
- Code performs integer division of a=10 by b=3, storing result in div variable
- Conditional check if b is less than or equal to -1, which is false so nothing prints
- Calculates and prints value of a - div - b which equals 10 - (-3) - 3 = 10
- Uses floor division operator // which gives quotient without remainder
- Time complexity is O(1) meaning execution time is constant regardless of input size
Output
Example 9
Explanation
- This code calculates the sum of all digits in the number 345 by repeatedly extracting the last digit using modulo operator (%) and adding it to a running total
- The while loop continues until n becomes 0, with n being integer divided by 10 in each iteration to remove the last digit
- Key functions used: modulo (%) for extracting last digit, integer division (//) for removing last digit, and print() for displaying result
- Expected output is 12 (3+4+5=12) as the sum of digits in 345
- Time complexity is logarithmic O(log n) since the number of iterations equals the number of digits in n
Output
Example 10
Explanation
- This code implements a recursive Fibonacci function that calculates the nth Fibonacci number by calling itself with smaller values
- The base cases return 1 when n is 0 or 1, while larger values recursively compute the sum of the two preceding Fibonacci numbers
- Key functions used include recursive function calls and simple conditional logic (if/else statements)
- The time complexity is exponential O(2^n) due to repeated calculations of the same Fibonacci numbers
- Side effect is that it becomes very slow for large inputs due to the exponential growth in function calls

