Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def shift_left(lst, n):
- """Shifts the elements of lst over by n indices.
- >>> lst = [1, 2, 3, 4, 5]
- >>> shift_left(lst, 2)
- >>> lst
- [3, 4, 5, 1, 2]
- """
- def shift_left(lst, n):
- """Shifts the lst over by n indices
- >>> lst = [1, 2, 3, 4, 5]
- >>> shift_left(lst, 2)
- >>> lst
- [3, 4, 5, 1, 2]
- """
- assert (n > 0), "n should be non-negative integer"
- def shift(ntimes):
- if ntimes == 0:
- return
- else:
- temp = lst[0]
- for index in range(len(lst) - 1):
- lst[index] = lst[index + 1]
- lst[index + 1] = temp
- return shift(ntimes-1)
- return shift(n)
- if n < 0:
- raise ValueError("Input %d is not a non-negative integer" % n)
- lst[:-1] = lst[1:]
- lst[:] = lst[1:] + [lst[0]]
- if n == 0:
- return
- else:
- lst[:] = lst[1:] + [lst[0]]
- shift_left(lst, n-1)
- def shift_left(lst, n):
- """Shifts the lst over by n indices
- >>> lst = [1, 2, 3, 4, 5]
- >>> shift_left(lst, 2)
- >>> lst
- [3, 4, 5, 1, 2]
- """
- if n < 0:
- raise ValueError('n must be a positive integer')
- if n > 0:
- lst.insert(0, lst.pop(-1)) # shift one place
- shift_left(lst, n-1) # repeat
- temp = lst[0]
- def shift_left_once(lst):
- temp = lst[0]
- for index in range(len(lst) - 1):
- lst[index] = lst[index + 1]
- lst[index + 1] = temp
- def shift_left(lst, n):
- """Shifts the lst over by n indices
- >>> lst = [1, 2, 3, 4, 5]
- >>> shift_left(lst, 2)
- >>> lst
- [3, 4, 5, 1, 2]
- """
- assert (n >= 0), "n should be non-negative integer"
- for _ in range(n):
- shift_left_once(lst)
- def shift_left(lst, n):
- """Shifts the lst over by n indices
- >>> lst = [1, 2, 3, 4, 5]
- >>> shift_left(lst, 2)
- >>> lst
- [3, 4, 5, 1, 2]
- """
- assert (n >= 0), "n should be non-negative integer"
- if n == 0:
- return
- shift_left_once(lst)
- shift_left(lst, n-1)
- tmp = lst[i]
- dst = i
- src = (dst - ntimes) % len
- while src != i:
- lst[dst] = lst[src]
- dst = src
- src = (dst - ntimes) % len
- lst[dst] = tmp
- def shift_left(l, n):
- """
- In place shift n elements of list l to the left.
- Won't work on strings.
- """
- n = n % len(l)
- head = l[:n]
- l[:n] = []
- l.extend(head)
- return l
- import unittest
- from random import randrange
- class TestShiftLeft(unittest.TestCase):
- def test_zero_shifts(self):
- self.assertEqual([1], shift_left([1], 0))
- self.assertEqual([1, 2], shift_left([1, 2], 0))
- def test_single_element(self):
- self.assertEqual([1], shift_left([1], 1))
- self.assertEqual([1], shift_left([1], 2))
- self.assertEqual([1], shift_left([1], 3))
- def test_two_elements(self):
- self.assertEqual([2, 1], shift_left([1, 2], 1))
- self.assertEqual([1, 2], shift_left([1, 2], 2))
- self.assertEqual([2, 1], shift_left([1, 2], 3))
- self.assertEqual([1, 2], shift_left([1, 2], 4))
- def test_odd_number_elements(self):
- self.assertEqual([2, 3, 1], shift_left([1, 2, 3], 1))
- self.assertEqual([3, 1, 2], shift_left([1, 2, 3], 2))
- self.assertEqual([1, 2, 3], shift_left([1, 2, 3], 3))
- self.assertEqual([2, 3, 1], shift_left([1, 2, 3], 4))
- def test_even_number_elements(self):
- self.assertEqual([2, 3, 4, 1], shift_left([1, 2, 3, 4], 1))
- self.assertEqual([3, 4, 1, 2], shift_left([1, 2, 3, 4], 2))
- self.assertEqual([4, 1, 2, 3], shift_left([1, 2, 3, 4], 3))
- self.assertEqual([1, 2, 3, 4], shift_left([1, 2, 3, 4], 4))
- self.assertEqual([2, 3, 4, 1], shift_left([1, 2, 3, 4], 5))
- def test_len_l_shift(self):
- l = list(range(randrange(1000)))
- self.assertEqual(l, shift_left(l, len(l)))
- if __name__ == '__main__':
- unittest.main()
- x=int(input("enter size of list"))
- lst=[]
- for c in range(x):
- e=int(input("enter element"))
- lst.append(e)
- sw=int(input("enter no. of rotations"))
- nw=[]
- while(sw!=0):
- for no in range(sw):
- temp=lst.pop()
- nw.append(temp)
- nw.reverse()
- lst=nw+lst
- nw.clear()
- print(lst)
- sw= int(input("enter no. of rotations"))
Add Comment
Please, Sign In to add comment