Write a function cycle (input_list) that performs an \"in-place\" cycling of the
ID: 3883724 • Letter: W
Question
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]Explanation / Answer
def cycle(input_list): input_list = input_list.copy() tmp = input_list[0] for i in range(0, len(input_list)-1): input_list[i] = input_list[i+1] input_list[-1] = tmp return input_list
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.