Question

Write a function cycle(input_list) that performs an "in-place" cycling of the elements of a list, moving...

Write a function cycle(input_list) that performs an "in-place" cycling of the elements of a list, moving each element one position earlier in the list. Here "in place" means the operation should be performed by mutating the original list, and your function should not return anything. Note that you may assume that input_list is non-empty (i.e. contains at least one element)

For example:

>>> l = [1, 2, 4, 5, 'd']
>>> cycle(l)
>>> l
[2, 4, 5, 'd', 1]
>>> cycle(l)
>>> l
[4, 5, 'd', 1, 2]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def cycle(l):
    if len(l)==0:
        return
    a = l[0]
    l = l[1:]
    l.append(a)
    return l

lis = [1, 2, 4, 5, 'd']
print(lis)
lis = cycle(lis)
print(lis)



========================
See Output
New Project-20170903 t3 + ExecuteI >Share Code main.py x 1- def cycle(l): root main.py if len(1)-0: return 4 5 6 7 1 101: 1.a
Thanks, let me know if there is any concern.





def cycle(l):
    if len(l)==0:
        return
    a = l[0]
    del l[0]
    l.append(a)

lis = [1, 2, 4, 5, 'd']
cycle(lis)
print(lis)

lis = [2, 4, 5, 'd', 1]
cycle(lis)
print(lis)


====================================================================================
New Project-20170903 t+QExecute >Share Code main.py x if len(L)-0: a-10 L.append(a) root 1- def cycle(l): main.py return 4 7

Add a comment
Know the answer?
Add Answer to:
Write a function cycle(input_list) that performs an "in-place" cycling of the elements of a list, moving...
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
  • For this task you're going to write a function that joins strings from a list. The function calle...

    I need help with this python programming assignment please double check the indentations For this task you're going to write a function that joins strings from a list. The function called join ) has one formal parameter and one optional parameter. Recall that an optional parameter is like end or sep in the print( function. You don't have to use it because there's a default value. For the print () function, e.g., \'n' (newline) is the default value for end....

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • Please I need help with this. Thank you Write the function cycle of type 'a list...

    Please I need help with this. Thank you Write the function cycle of type 'a list *int-> 'a list that takes a list and an integer n as input and returns the same list, but with the first element cycled to the end of the list n times. For example, cycle ([1,2,3,4,5,6], 2) should return [3,4,5,6,1,2)]. You can make use of the cycle 1 function given below as a helper function. fun cycle1 list- tl list hd list]:

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

  • Write a Python function fun3(e) to update a list of numbers e such that the first...

    Write a Python function fun3(e) to update a list of numbers e such that the first and last elements have been exchanged. The function needs to also return multiplication of elements of e. You should assume that the list e has at least 2 elements. Example: >>>lst = [4, 1, 2, -1] >>>fun3(list) -8 >>>lst [-1, 1, 2, 4] *We are an intro to CS class so please do not use anything too advance or fancy. Stick to basics. Right...

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

  • Write the following function in Java: A function "makeMangler", that takes as input a list M...

    Write the following function in Java: A function "makeMangler", that takes as input a list M of three numbers. It then builds and returns a "Mangler" function based on M. The "Mangler" function that is produced (by your function makeMangler) would have the property that it takes as input a list, and returns the "mangled" version of that list. "Mangling" a list means doing the following sequence of operations to each item in a list: (1) multiply by the first...

  • iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of...

    iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...

  • [MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a...

    [MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element of the vector x, xj, is defined by pj = (x1)(x2) … (xj) for j = 1:length of the vector x. DO NOT USE CUMPROD For example, when you run your function it should look like this: >> x = [2 3 4 2]; >> myMultProd(x) >> ans = 2 6 24 48 That is,...

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

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