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

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

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

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

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

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

  • PYTHON 4. Define a function that takes two arguments: a string called strText and a number...

    PYTHON 4. Define a function that takes two arguments: a string called strText and a number called intNumber. This function will use a repetition structure (a While or For loop) to print strText intNumber of times. Call this function. 5. Get an input from the user that is a file name with an extension (e.g., "myfile.ipynb" or "myfile.txt"). Print only the characters that follow the "." in the file name (e.g., "ipynb" and "txt"). 6. Ask the user for 5...

  • Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s...

    Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse...

  • JAVA Program: reverse a linked list and find the middle node in the linked list. inFile...

    JAVA Program: reverse a linked list and find the middle node in the linked list. inFile (use argv[1]): A text file contains a list of English words (strings), giving below outFile1 (use argv[2])a text file includes       i) The completed sorted linked list, in ascending order. //With caption indicating you are printing the original sorted list       ii) The reversed linked list. //With caption indicating you are printing the reversed sorted list       outFile2( use argv[3]): All debugging outputs.                        ...

  • 1.Write a function called high_point that takes a list of integers and returns True if there...

    1.Write a function called high_point that takes a list of integers and returns True if there exists a value in the list that is bigger than the values immediately before and after it in the list. Your function must return False if no such value exists. The values in the beginning and the end of the list cannot be high points. (20 points) Test Cases: print(high_point([2,5,8,9,7,9])) True print(high_point([2,5,6,6,3])) False print(high_point([2,5,8,21,22])) False 2. Write a while loop to repeatedly ask for...

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
ADVERTISEMENT