Question

Implement function reverse that takes a 2D list (a list of list of integers) and returns...

Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append().

Examples:

reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]]

reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]]

True

False

In python please

Homework Answers

Answer #1
def reverse(list):

    # declaring empty newList
    newList = []

    # iterating through every list
    for sublist in list:

        # declaring temp list
        temp = []

        # iterating through every sublist using range
        # starting from last element and decrementing length
        for index in range(len(sublist)-1, -1, -1):
            # appending temp list
            temp.append(sublist[index])

        # appending temp list to newList
        newList.append(temp)

    # returns new List
    return newList

print( reverse([[1,2,3],[4,5,6]]) )
print( reverse([[1,2],[3,4],[5,6]]) )

Code

Output

Know the answer?
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
Similar Questions
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses the list. You should mutate the original list, without creating any new lists. Do NOT return anything. Do not use any built-in list functions such as reverse(). def reverse(lst):    """Reverses lst in place (i.e. doesn't create new lists).     >>> L = [1, 2, 3, 4]     >>> reverse(L)     >>> L     [4, 3, 2, 1]     """
Question 1: Roll two fair dice. Then the sample space S is the following. S =...
Question 1: Roll two fair dice. Then the sample space S is the following. S = (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) Let E be the event that the sum of the dice is odd, let F be the event that the first die lands on 1, and let G...
9K)Below is a list of all possible outcomes in the experiment of rolling two die. (If...
9K)Below is a list of all possible outcomes in the experiment of rolling two die. (If the grid pops up twice, pay attention to only one table. Having formatting issues, apologies! And thank you for your help! 1,1 1,2 1,3 1,4 1,5 1,6 2,1 2,2 2,3 2,4 2,5 2,6 3,1 3,2 3,3 3,4 3,5 3,6 4,1 4,2 4,3 4,4 4,5 4,6 5,1 5,2 5,3 5,4 5,5 5,6 6,1 6,2 6,3 6,4 6,5 6,6 Determine the following probabilities. Write your answers...
using mysql lyrics.database. i will provide the lyrics schema database info below 1. List the first...
using mysql lyrics.database. i will provide the lyrics schema database info below 1. List the first name, last name, and region of members who do not have an email. 2. List the first name, last name, and region of members who do not have an email and they either have a homephone ending with a 2 or a 3. 3. List the number of track titles that begin with the letter 's' and the average length of these tracks in...
Q1. Use Union statements to show the following: list the number of artists that have a...
Q1. Use Union statements to show the following: list the number of artists that have a webaddress, the number of artists that do not have a webaddress, and the total number of artists. The output should look like: +--------------------------+----------+ | Has webaddress | count(*) | +--------------------------+----------+ | Has webaddress | 5 | | Does not have webaddress | 6 | | Total | 11 | +--------------------------+----------+ Q2. A new salesperson has been hired. Their SalesID is 5, full name is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT