1. Write a python program named “recursion_add_subtract.py”. The program should contain a recursive python function to compute the product of two positive integers’ m and n, using only addition and subtraction. The main method should test the function.
2. Write a python program named “recursion_integers.py”. The program should contain a recursive python function that rearranges a list of integers from largest to smallest. The initial list should not be sort and do not sort it with the sort method. It should all be done recursively. The main method should test the function.
3. Using timeit, code the measurements for 5 test runs but also code the average from the results received from timeit.
The answer
Description:
This program defines a recursive function to compute the product of two positive integers using only addition and subtraction, without using multiplication or built-in product functions. The main section tests the function with sample input.
Code:
2. recursion_integers.py
Title:
Recursive Sorting of Integers from Largest to Smallest
Description:
This program defines a recursive function that rearranges a list of integers from largest to smallest. The function does not use the sort method or any built-in sort; the sorting is done recursively. The main section demonstrates and tests the function.
Code:
3. Timing and Averaging with timeit
Title:
Measuring and Averaging Execution Time Using timeit
Description:
This example uses the timeit
module to measure the execution time of the recursive sort function for 5 runs and computes the average time.
Code:
Note:
If you place all code in one file, you can copy the sort and test functions above the timeit section.
If you want to measure the product function, replace recursive_sort_desc
with recursive_product
and adapt the arguments.
Now you have a complete, modular, and well-documented solution for all requirements!
📩 Need a similar solution? Email me: adel455@hotmail.com