Question

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 will return a new list containing the integers in reversed order. The mutating variant will retur None, and will instead modify the list that its given Your functions should have these signatures: def reverse pure (xs) signature: list(int)> list(int) Returns a list of integers in reverse order pass def reverse ut (x) signature: list(int) > NoneType Reverses the list of integers in place pass When implementing reverse nut, consider carefully how one can reverse the items in a list without creating a new list A contrasting example: >> mylist [5, 12, 34, 0. 11 # The list of numbers >>> newlist - reverse-pure (aylist) # Call the pure function >> print(nevlist) 1, 0, 34, 12, 5] >> print (ylist) [5, 12, 34, 0, 11 >> reverse aut (nylist) >> print (ylist) 1, 0, 34, 12, 5] # The new , revers ed list # The original list is unchanged # Call the mutating function (returns None) # Original list is reversed in-place For this problem, you may not use the built-in Python functions for reversing list, reverse and reversed. You should implement the function using a loop.

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. Make sure that you maintain proper indentations while pasting the below code into your program, I’m also attaching the code in both text and image formats, so in case if the indentation is messed up, you can refer the image and maintain proper spacing in each and every line. Thanks

If you are satisfied with the solution, please rate the answer. If not, let me know before you rate the answer, I’ll correct anything you want.

#code

def reverse_pure(lst):
    #defining an empty list
   
reversed=[]
    #finding last index of parameter list
   
index=len(lst)-1
    #iterating from rear to front
   
while index>=0:
        #adding current element to new list
       
reversed.append(lst[index])
        index-=1 #previous index
   
return reversed #returning new list containing elements in reverse order

def reverse_mut(lst):
    #finding the length of lst list
   
size=len(lst)
    #looping from i=0 to i=size/2
   
for i in range(size//2):
        #swapping elements at index i and index size-i-i
        #that is swapping ith element from front and ith element from rear
       
temp=lst[i]
        lst[i]=lst[size-1-i]
        lst[size - 1 - i]=temp
    #end of method, now the lst will be in reverse order.
   
#testing

myList=[5,12,34,0,1]
newList=reverse_pure(myList)
print(newList) # [1, 0, 34, 12, 5]
print(myList) # [5, 12, 34, 0, 1]
reverse_mut(myList)
print(myList) # [1, 0, 34, 12, 5]

#code screenshot

1 def reverse_pure(1st): #defining an empty list reversed-[] #finding last index of parameter list index-len(1st)-1 #iterating from rear to front while index>-e: 4 9 1e #adding current element to new list reversed.append (1st[index]) index-=1 #previous index return reversed #returning new list containing elements in reverse order 12 13- 14 15 16 17 18 19 def reverse-mut(1st): #finding the length of 1st list size-len(lst) #looping from i-e to i size/2 for i in range(size//2): #swapping elements at index i and index size-i-i #that is swapping ith element from front and ith element from rear temp-1st[i] 1st[i]-lstisize-1-i] 1st[size - 1 - i]-temp #end of method, now the 1st will be in reverse order. 24 25 26 27 28 29 30 31 #testing myList-[5,12,34,0,1] newList-reverse_pure(myList) print(newList) # [1, 0, 34, 12, 5] print(myList) # [5, 12, 34, 0, 1] reverse_mut(myList) print(myList) # [1, , 34, 12, 5]

#output

[1, 0, 34, 12, 5]

[5, 12, 34, 0, 1]

[1, 0, 34, 12, 5]

Add a comment
Know the answer?
Add Answer to:
Problem 8 In class, we discussed the difference between a pure (or effect-free) function (i.e. one...
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 program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply...

    Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply inheritance and method overriding in order to create a hierarchy of array sorters. There exist many algorithms to sort arrays. In this problem set, we are especially interested in “in-place” sorting algorithms like Selection Sort and Insertion Sort. In-place sorting refers to an approach in which we perform the sorting steps on the underlying array rather than using extra storage. When using object-oriented design,...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • In this lab you will convert lab5.py to use object oriented programming techniques. The createList and...

    In this lab you will convert lab5.py to use object oriented programming techniques. The createList and checkList functions in lab5.py become methods of the MagicList class, and the main function of lab6.py calls methods of the MagicList class to let the user play the guessing game.                              A. (4pts) Use IDLE to create a lab6.py. Change the 2 comment lines at the top of lab6.py file: First line: your full name Second line: a short description of what the program...

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