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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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_functions.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_functions.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` (✿◠‿◠) ')
    
    # 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()

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY