Question

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

You must use a loop(s) in your solution. You may make use of the list_name.append(item) method in order to build the new list. You 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.

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

Copy the below code in a .py file :-

#!/usr/bin/env python
# coding: utf-8

def reverse(my_list, number=-1):
lengthOfList =0
  
# Find the length of the list ( we annot use a list function)
for item in my_list:
lengthOfList = lengthOfList +1
# The list that represents the reversed List
reversedList = []
  
# If defualt argument was passed, reverse only first element
if number == -1:
# Copy last element first
reversedList.append(my_list[-1])
# Copy rest of list except the last parameter
for index in range(1,lengthOfList-1):
reversedList.append(my_list[index])
# copy the first element in the last
reversedList.append(my_list[0])
# If the no of elements > length of list
elif number >lengthOfList:
# Set the no of elements as list bound
number = lengthOfList
# If number value is less than 2
if number <=2:
# Copy the same list
for index in range(0,lengthOfList):
reversedList.append(my_list[index])
else:
# Other wise reverse the whole list
for index in range(lengthOfList-1, -1, -1):   
reversedList.append(my_list[index])
# If number value is less than 2
elif number <=2:
# Copy the same list
for index in range(0,lengthOfList):
reversedList.append(my_list[index])
# In all other cases reverse the entire list
else:
# Other wise reverse the whole list
for index in range(lengthOfList-1, -1, -1):   
reversedList.append(my_list[index])   

return reversedList


def main():
listOfNumbers =[1,2, 3,4, 5,6]   
print(listOfNumbers)
print("Reverse with no number parameters")   
print(reverse(listOfNumbers))
print("Reverse with number parameter value out of bounds")   
print(reverse(listOfNumbers,9))   
print("Reverse with number parameter value <=2")   
print(reverse(listOfNumbers,1))
print("Reverse with number parameter value 4")   
print(reverse(listOfNumbers,4))
  

if __name__== "__main__":
main()

Code Screenshot for indentation

Sample Output

Add a comment
Know the answer?
Add Answer to:
Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice...
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 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...

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

  • CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT...

    CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT ALLOWED) Write a function that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Write a function that accepts two arguments (a...

  • Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format()...

    Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format() all string methods are banned. Except for append() and extend() all list methods are banned. You may not import any module. Make sure to test your code with a variety of inputs. Do not assume the examples in these directions are reflective of the hidden test cases. Part 3 Echo (5 Points) Write a function called echo that takes as argument a list and...

  • Code the following function in JAVASCRIPT (Use of the given list of inbuilt functions is not...

    Code the following function in JAVASCRIPT (Use of the given list of inbuilt functions is not allowed) Write a function that searches the string (passed as first argument) for the character (passed as second argument) and changes the case for all instances of that char found in string. The function should return the new string as shown in example below: function(‘abRA’, ‘a’) // returns ‘AbRa’ Assignment-2(2).pdf + х A file:///C:/Users/bhati/OneDrive/Desktop/Assignment-2(2).pdf 5 of 6 O + Fit to page IL Page...

  • PYTHON: MUST NOT USE PYTHONS BUILT IN FUNCTIONS SUCH AS  result.append MUST BE SOLVED OUT WITHOUT SUCH...

    PYTHON: MUST NOT USE PYTHONS BUILT IN FUNCTIONS SUCH AS  result.append MUST BE SOLVED OUT WITHOUT SUCH FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1. Write a function interleave(vals1, vals2) that takes as inputs two lists vals1 and vals2 and uses recursion to construct and return a new string that is formed by interleaving the elements in the lists vals1 and vals2 to create a single list. In other words, the new list should alternate elements from the two input lists: the first element from vals1,...

  • In the python: Note 1: You may not use these python built-in functions: sorted(), min(), max(),...

    In the python: Note 1: You may not use these python built-in functions: sorted(), min(), max(), sum(), pow(), zip(), map(), append(), count() and counter(). For questions 1, 2 and 4: Ask the user to enter the size of a list. Fill the list with random numbers 0-9 and print the list. 1- (8 points) luckyNumbers.py: Find the sum of the numbers in a list, except the number 7 is considered unlucky, so we ignore it and do not include 7's...

  • In python... All functions must use recursion. Do not use any iteration or slicing. 1. Return...

    In python... All functions must use recursion. Do not use any iteration or slicing. 1. Return the reverse of the given string. Use the index argument to keep track of position, which starts at zero. reverse_string(chars: str, index: int) -> str 2. Return the highest number in the list of integers. Use the index argument to keep track of position, which starts at zero. find_max(ints: List[int], index: int) -> int 3. Return True if the given string is a palindrome...

  • using python with only using import math and copy no other powerful functions. Write a function...

    using python with only using import math and copy no other powerful functions. Write a function defined as: def GaussSeidel(Aaug, x, Niter = 15): Purpose: use the Gauss-Seidel method to estimate the solution to a set of N linear equations expressed in matrix form as Ax = b. Both A and b are contained in the function argument – Aaug. Aaug: an augmented matrix containing [A | b ] having N rows and N+1 columns, where N is the number...

  • No loops can be used and must only use functions that are built into matlab, aka...

    No loops can be used and must only use functions that are built into matlab, aka no toolboxes FACTORS All Factors of an Integer FACTORS(N) returns all the factors of the positive integer N. For example: FACTORS(60) returns the 2 X 6 array [ 1 2 3 4 5 6 60 30 20 15 12 10] FACTORS(25) returns the 2 X 2 array [ 1 5 25 5] FACTORS(7) returns the 2 X 1 array [1 7] If N is...

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