I need help in 5: 5. Recursively Reverse a List Mke a recursive function that reverses a list. For example, given [1, 2, 3, 4], the function would return this list [4, 3, 2, 1]. It does not print...


I need help in 5:



5. Recursively Reverse a List

Mke a recursive function that reverses a list. For example, given [1, 2, 3, 4], the function


would return this list [4, 3, 2, 1]. It does not print anything.




Instructionis below


Practice writing recursive functions inpython3


 Make the five recursive functions described below inpython3 by using the starter code recursive_functions.py. For each function, figure out how to solve it conceptually : write down the base case (when recursion stops) and how each recursive function-call moves towards the base case. The functions should not print anything (except you may add temporary print statements to help debug them).


You can test your program by using the provided program test_recursive.py. Don't edit the test program. Put it into the same directory (folder) with your recursive_functions.py and run it. It will import your functions as a module, test your functions, and tell you when each function is returning correct results.



1. Factorial

In math, if you have a number n, the factorial function (written n!) computes


n x (n-1) x (n-2) x (n-3) x ... x 1. For example:




  • 0! is defined to be 1




  • 1! = 1




  • 2! = 2 x 1=2




  • 3! = 3 x 2 x 1=6




  • 4! = 4 x 3 x 2 x 1 = 24




  • 5! = 5 x 4 x 3 x 2 x 1 = 120


    Add your code to the provided function signature so it computes the factorial of the integer it is given. You may not use math.factorial() in your function.





2. Recursively Sum

Make a recursive function that takes an integer argument n and returns the sum of the


integers from 1 to n. E.g., if n=10, the function should return 55 (1 + 2 + 3 + . . . 10).



3. Recursively Sum List


Make a recursive function sumlist_recursively(l) that accepts a list of numbers as an argument, and returns the sum of all the numbers in the list. E.g., if the function is passed the list [1, 4, 8, 3, 0, 16], it should return 32.



4. Recursively Multiply


Make a recursive function that multiplies its two integer arguments (x and y) recursively. The function should return the value of x times y. Hint: multiplication can be performed as repeated addition, for example.:


7 x 4 = 4 + 4 + 4 + 4 + 4 + 4 + 4 = 28



5. Recursively Reverse a List

Mke a recursive function that reverses a list. For example, given [1, 2, 3, 4], the function


would return this list [4, 3, 2, 1]. It does not print anything.




starter code recursive_function.py:


def factorial(n):
    pass    # replace this line with your lines of recursive code


def sum_recursively(n):
    pass    # replace this line with your lines of recursive code


def sumlist_recursively(l):
    pass    # replace this line with your lines of recursive code


def multiply_recursively(n, m):
    pass    # replace this line with your lines of recursive code


def reverse_recursively(l):
    pass    # replace this line with your lines of recursive code





test_recursive.py:


import recursive_functions
import math


def main():
# Test factorial
print('Testing factorial.')
assert recursive_functions.factorial(0) == 1
assert recursive_functions.factorial(1) == math.factorial(1) == 1
assert recursive_functions.factorial(2) == math.factorial(2) == 2
assert recursive_functions.factorial(5) == math.factorial(5) == 120
assert recursive_functions.factorial(7) == math.factorial(7) == 5040
print('All tests pass for factorial\n')


# Test sum_recursively
print('Testing sum_recursively.')
assert recursive_functions.sum_recursively(0) == 0
assert recursive_functions.sum_recursively(1) == sum(range(1+1)) == 1
assert recursive_functions.sum_recursively(2) == sum(range(2+1)) == 3
assert recursive_functions.sum_recursively(10) == sum(range(10+1)) == 55
print('All tests pass for sum_recursively\n')


# Test sumlist_recursively(l)
print('Testing sumlist_recursively.')
assert recursive_functions.sumlist_recursively([1,2,3]) == sum([1,2,3])
assert recursive_functions.sumlist_recursively([42,16,99,102,1]) == sum([42,16,99,102,1])
assert recursive_functions.sumlist_recursively([17,13,9,5,1]) == sum([17,13,9,5,1])
print('All tests pass for r_sumlist \n')


# Test multiply_recursively
print('Testing multiply_recursively.')
assert recursive_functions.multiply_recursively(5, 1) == 5*1 == 5
assert recursive_functions.multiply_recursively(7, 4) == 7*4 == 28
print('All tests pass for multiply_recursively\n')


# Test reverse_recursively
print('Testing reverse_recursively.')
assert recursive_functions.reverse_recursively([1, 2, 3, 4]) == [4, 3, 2, 1]
life = ['born', 'grow up', 'grow old']
assert recursive_functions.reverse_recursively(life) == ['grow old', 'grow up', 'born']
print('All tests pass for reverse_recursively\n')


main()


test_recursive.py<br>Edited<br>import recursive_functions|<br>import math<br>3<br>1<br>def main():<br># Test factorial<br>print('Testing factorial.')<br>assert recursive_functions.factorial(0)<br>assert recursive_functions.factorial(1)<br>assert recursive_functions.factorial(2)<br>assert recursive_functions.factorial(5)<br>assert recursive_functions.factorial(7)<br>print('All tests pass for factorial\n')<br>6.<br>7<br>1<br>math.factorial(1)<br>math.factorial(2)<br>math.factorial(5)<br>math.factorial(7)<br>8<br>1<br>9.<br>2<br>10<br>120<br>11<br>5040<br>12<br>13<br># Test sum_recursively<br>print('Testing sum_recursively.')<br>assert recursive_functions.sum_recursively(0)<br>assert recursive_functions.sum_recursively(1)<br>assert recursive_functions.sum_recursively(2)<br>assert recursive_functions.sum_recursively(10)<br>print('All tests pass for sum_recursively\n')<br>14<br>15<br>16<br>sum(range(1+1))<br>sum(range(2+1))<br>sum(range(10+1))<br>17<br>1<br>18<br>3<br>19<br>55<br>20<br>21<br># Test sumlist_recursively(1)<br>print('Testing sumlist_recursively.')<br>assert recursive_functions.sumlist_recursively([1,2,3])<br>assert recursive_functions.sumlist_recursively([42,16,99,102,1])<br>assert recursive_functions.sumlist_recursively([17,13,9,5,1])<br>print('All tests pass for r_sumlist \n')<br>22<br>23<br>24<br>sum([1,2,3])<br>sum([42,16,99,102,1])<br>sum([17,13,9,5,1])<br>25<br>26<br>27<br>28<br># Test multiply_recursively<br>print('Testing multiply_recursively. ')<br>assert recursive_functions.multiply_recursively(5, 1)<br>assert recursive_functions.multiply_recursively(7, 4)<br>print('All tests pass for multiply_recursively\n')<br>29<br>30<br>31<br>5*1<br>--<br>32<br>7*4<br>28<br>33<br>34<br># Test reverse_recursively<br>print('Testing reverse_recursively.')<br>assert recursive_functions.reverse_recursively([1, 2, 3, 4])<br>life<br>35<br>36<br>37<br>[4, 3, 2, 1]<br>['born', 'grow up', 'grow old']<br>assert recursive_functions.reverse_recursively(life)<br>print('All tests pass for reverse_recursively\n')<br>38<br>39<br>['grow old', 'grow up', 'born']<br>40<br>41<br>42 main()<br>43<br>

Extracted text: test_recursive.py Edited import recursive_functions| import math 3 1 def main(): # Test factorial print('Testing factorial.') assert recursive_functions.factorial(0) assert recursive_functions.factorial(1) assert recursive_functions.factorial(2) assert recursive_functions.factorial(5) assert recursive_functions.factorial(7) print('All tests pass for factorial\n') 6. 7 1 math.factorial(1) math.factorial(2) math.factorial(5) math.factorial(7) 8 1 9. 2 10 120 11 5040 12 13 # Test sum_recursively print('Testing sum_recursively.') assert recursive_functions.sum_recursively(0) assert recursive_functions.sum_recursively(1) assert recursive_functions.sum_recursively(2) assert recursive_functions.sum_recursively(10) print('All tests pass for sum_recursively\n') 14 15 16 sum(range(1+1)) sum(range(2+1)) sum(range(10+1)) 17 1 18 3 19 55 20 21 # Test sumlist_recursively(1) print('Testing sumlist_recursively.') assert recursive_functions.sumlist_recursively([1,2,3]) assert recursive_functions.sumlist_recursively([42,16,99,102,1]) assert recursive_functions.sumlist_recursively([17,13,9,5,1]) print('All tests pass for r_sumlist \n') 22 23 24 sum([1,2,3]) sum([42,16,99,102,1]) sum([17,13,9,5,1]) 25 26 27 28 # Test multiply_recursively print('Testing multiply_recursively. ') assert recursive_functions.multiply_recursively(5, 1) assert recursive_functions.multiply_recursively(7, 4) print('All tests pass for multiply_recursively\n') 29 30 31 5*1 -- 32 7*4 28 33 34 # Test reverse_recursively print('Testing reverse_recursively.') assert recursive_functions.reverse_recursively([1, 2, 3, 4]) life 35 36 37 [4, 3, 2, 1] ['born', 'grow up', 'grow old'] assert recursive_functions.reverse_recursively(life) print('All tests pass for reverse_recursively\n') 38 39 ['grow old', 'grow up', 'born'] 40 41 42 main() 43
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here