+ 1

List rotation

A list rotation consists of taking the last element and moving it to the front. For instance, if we rotate the list [1,2,3,4,5], we get [5,1,2,3,4]. If we rotate it again, we get [4,5,1,2,3]. Write a Python function rotatelist(l,k) that takes a list l and a positive integer k and returns the list l after k rotations. If k is not positive, your function should return l unchanged. Note that your function should not change l itself, and should return the rotated list. Here are some examples to show how your

24th Feb 2018, 3:26 PM
SUNIL PATIL
SUNIL PATIL - avatar
2 Réponses
+ 6
But still, one can write his own implementation: 😉 https://code.sololearn.com/c9xldSJP2sUt/?ref=app
24th Feb 2018, 4:46 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
There's a ready-made datatype called deque (double-ended queue) implemented in the collections module. It sports the rotate() method ;) https://docs.python.org/3/library/collections.html#collections.deque
24th Feb 2018, 4:32 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar