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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 12PE
icon
Related questions
Question
100%

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.

 

Instruction is below

Practice writing recursive functions in python3

 Make the five recursive functions described below in python3 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
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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Quicksort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning