Question

Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a...

Python Question?

Write a function called reverse(user_list, num = -1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed.

Conditions:

The number parameter must be a default argument.
-      If the default argument for
number is given in the function call, only the first number of items are reversed.
-      If the default argument for
number is not provided in the function call, then the entire list is reversed.

Check for the number value exceeding the list bounds (i.e. is greater than the length of the list).
-    If the number value exceeds the list bounds, then make the number value the length of the list.
-    If the number value entered is less than two, then return a copy of the list with no items reversed.

Must use a loop(s) in your solution.

Make use of the list_name.append(item) method in order to build the new list.

Must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution.

user_list1 = ['e', 'd', 'o', 'm']

user_list2 = ['l', 'o', 'w', 'b', 'e', 'd']

new_list = list_function.reverse(user_list1, 4)

print(new_list)

new_list = reverse(user_list2, 3)

print(new_list

new_list = reverse(userlist2, 2)

print(new_list)

new_list = reverse(user_list1)

print(new_list)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required. Assuming we can also use len() method (not mentioned) to find the length of list. If not, let me know, I’ll update the answer.

#code

#required method
def reverse(user_list, num = -1):
    #creating an empty list
    new_list=[]
    #checking if num is -1 (default argument; I suggest using None value as default which makes more
    # sense when the argument is not provided)
    if num==-1:
        #looping in reverse order, from last index to first
        #assuming we can use len() method to find list length, if not, let me know
        for i in range(len(user_list)-1,-1,-1):
            #appending element at index i to new_list
            new_list.append(user_list[i])
        #returning new list
        return new_list
    else:
        #if num exceeds length of user_list, setting length of user_list as num
        if num>len(user_list):
            num=len(user_list)
        #if num is less than 2, setting 0 as num
        elif num<2:
            num=0
        #looping down from num-1 to 0
        for i in range(num - 1, -1, -1):
            #adding element at index i to new_list
            new_list.append(user_list[i])
        #adding remaining elements in original order
        for i in range(num,len(user_list)):
            new_list.append(user_list[i])
        #returning new_list
        return new_list


#testing
user_list1 = ['e', 'd', 'o', 'm']
user_list2 = ['l', 'o', 'w', 'b', 'e', 'd']
new_list = reverse(user_list1, 4) 
print(new_list) # ['m', 'o', 'd', 'e']
new_list = reverse(user_list2, 3)
print(new_list) # ['w', 'o', 'l', 'b', 'e', 'd']
new_list = reverse(user_list2, 2)
print(new_list) # ['o', 'l', 'w', 'b', 'e', 'd']
new_list = reverse(user_list1)
print(new_list) # ['m', 'o', 'd', 'e']
Add a comment
Know the answer?
Add Answer to:
Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a...
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
  • Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice...

    Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution. Write a function called reverse(my_list, number=-1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: - The number parameter must be a default argument. -      If the default argument for number is given in...

  • 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...

  • 5. Write a new function called "listinsert" based on the following IPO # function: listinsert #...

    5. Write a new function called "listinsert" based on the following IPO # function: listinsert # INPUT: a list, a location (integer) and a data # element (can be a string, float, Boolean or # int) # PROCESSING: inserts the supplied data element into the # list at the desired location # OUTPUT: return a new copy of the list that contains # the inserted element Make sure that you DO NOT use any list methods, such as the "insert"...

  • Use Python Spyder to answer: First List – Loop (one that you created in lab 1...

    Use Python Spyder to answer: First List – Loop (one that you created in lab 1 or any new list) • Repeat First List, but this time use a loop to print out each value in the list. First Neat List - Loop • Repeat First Neat List, but this time use a loop to print out your statements. Make sure you are writing the same sentence for all values in your list. Loops are not effective when you are...

  • Python 2.7 Write a function cumsum() that takes a list l as argument and returns the...

    Python 2.7 Write a function cumsum() that takes a list l as argument and returns the cumulative sum (also known as the prefix sum) of l, which is a list, say cs of the same length as l such that each element cs[i] is equal to the sum of the first i + 1 elements of l, i.e., cs[i] == l[0] + l[1] + l[2] + ... + l[i] You should not modify the argument list l in any way....

  • The function below takes a single parameter, a list of numbers called number_list. Complete the function...

    The function below takes a single parameter, a list of numbers called number_list. Complete the function to return a string of the provided numbers as a series of comma separate values (CSV). For example, if the function was provided the argument [22, 33, 44], the function should return '22,33,44'. Hint: in order to use the join function you need to first convert the numbers into strings, which you can do by looping over the number list to create a new...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

  • Python Code Question# 1 General Instructions Create a class called LawnService that takes in the length...

    Python Code Question# 1 General Instructions Create a class called LawnService that takes in the length and width of a yard, along with the current state of the yard. The currect state can be: poor, acceptable or great. Within this class include a constructor that will assign these values to attributes. Additionally create three methods that will be named and do the following: *DetermineApplication: This method will focus on the lawn status attribute. If the status is poor, the lawn...

  • Create Code Using C program: 1. Write a function, reverse Digit, that takes an integer as...

    Create Code Using C program: 1. Write a function, reverse Digit, that takes an integer as a parameter and returns the number with its digits reversed. For example, the value of reverseDigit (12345) is 54321; the value of reverse Digit (5600) is 65, the value of reverse Digit (7008) is 8007 and the value of reverse Digit (-532) is -235.

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

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