Question

Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...

Python: P2

Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook)

E.g.:

Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Constraints:
    - List elements are integers
    - The function should return a reversed array, not print its elements

You may use the following code as template:

def reverse(my_list, index = None):
    # your code here
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: here is the answer for your code:

def reverse(my_list):
if(len(my_list)==0):
return my_list
  
last_element=[my_list[-1]] # storing the last last_element of list
remaining_list=my_list[:-1] # remaining list is without the last element
return last_element+reverse(remaining_list) # recursively reversing the list

my_list=[i for i in range(2,21)]
my_list=reverse(my_list)
print("reversed list is: {}".format(my_list))

CODE SNIPPET:

OUTPUT:

I hope this would help you out

If you have any doubt, you can provide comment /feedback below the answer

Thanks

Add a comment
Know the answer?
Add Answer to:
Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

  • Using PYTHON: (Find the index of the smallest element) Write a function that returns the index...

    Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

    Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list) bounds. if the insert_position is greater than the length of the list, insert the value at the end of the list. if the insert_position is less than or equal...

  • Problem 8 In class, we discussed the difference between a pure (or effect-free) function (i.e. one...

    Problem 8 In class, we discussed the difference between a pure (or effect-free) function (i.e. one that returns a value, but does not produce an effect) and a mutating function i.e one that changes the value of its parameters). In this exercise, we ask you to demonstrate your understanding o this distinction by implementing the same functionality as both a pure function, and as a mutating function. Both functions take a list of int as a parameter. The pure variant...

  • Write a function in python, index(arr, value) to find indices of elements equal to some value...

    Write a function in python, index(arr, value) to find indices of elements equal to some value in a Numpy array. The input arr is a 1-d numpy array, and input value is the value to search for. The function should return the index of the value. If the value occurs for multiple times, then all the indices should be returned as a 1-d numpy array. For example, if arr=[1 0 2 0 3 0 4 0 5 0 6 7...

  • In python Write a function named printList to print the elements of a list with labels...

    In python Write a function named printList to print the elements of a list with labels showing each element's order in the list. The function header should like this: def printList(aList): And here is an example run: > > > myList = [92.5, 127.1, 9, 104.2, 78.4] > > > printList(myList) 0 92.5 1 127.1 2 9 3 104.2 4 78.4 Test your function from the Python shell window on various types of lists with varying lengths.

  • FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter....

    FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter. It returns the index of the minimum row in the list. The minimum row is the row with the smallest sum of elements. If the list has multiple rows that achieve the minimum value, the last such row should be returned. If the list is empty the function should return -1. The following shows several sample runs of the function: Python 3.4.1 Shell File...

  • PYTHON CODE In a program, write a function that accepts two arguments: a list, and a...

    PYTHON CODE In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. Output should look like: Number: 5 List of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List of numbers that are larger than 5: [6, 7, 8, 9, 10]

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT