Guest User

Untitled

a guest
Nov 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. def shift_left(lst, n):
  2. """Shifts the elements of lst over by n indices.
  3. >>> lst = [1, 2, 3, 4, 5]
  4. >>> shift_left(lst, 2)
  5. >>> lst
  6. [3, 4, 5, 1, 2]
  7. """
  8.  
  9. def shift_left(lst, n):
  10. """Shifts the lst over by n indices
  11.  
  12. >>> lst = [1, 2, 3, 4, 5]
  13. >>> shift_left(lst, 2)
  14. >>> lst
  15. [3, 4, 5, 1, 2]
  16. """
  17. assert (n > 0), "n should be non-negative integer"
  18. def shift(ntimes):
  19. if ntimes == 0:
  20. return
  21. else:
  22. temp = lst[0]
  23. for index in range(len(lst) - 1):
  24. lst[index] = lst[index + 1]
  25. lst[index + 1] = temp
  26. return shift(ntimes-1)
  27. return shift(n)
  28.  
  29. if n < 0:
  30. raise ValueError("Input %d is not a non-negative integer" % n)
  31.  
  32. lst[:-1] = lst[1:]
  33.  
  34. lst[:] = lst[1:] + [lst[0]]
  35.  
  36. if n == 0:
  37. return
  38. else:
  39. lst[:] = lst[1:] + [lst[0]]
  40. shift_left(lst, n-1)
  41.  
  42. def shift_left(lst, n):
  43. """Shifts the lst over by n indices
  44.  
  45. >>> lst = [1, 2, 3, 4, 5]
  46. >>> shift_left(lst, 2)
  47. >>> lst
  48. [3, 4, 5, 1, 2]
  49. """
  50. if n < 0:
  51. raise ValueError('n must be a positive integer')
  52. if n > 0:
  53. lst.insert(0, lst.pop(-1)) # shift one place
  54. shift_left(lst, n-1) # repeat
  55.  
  56. temp = lst[0]
  57.  
  58. def shift_left_once(lst):
  59. temp = lst[0]
  60. for index in range(len(lst) - 1):
  61. lst[index] = lst[index + 1]
  62. lst[index + 1] = temp
  63.  
  64. def shift_left(lst, n):
  65. """Shifts the lst over by n indices
  66.  
  67. >>> lst = [1, 2, 3, 4, 5]
  68. >>> shift_left(lst, 2)
  69. >>> lst
  70. [3, 4, 5, 1, 2]
  71. """
  72. assert (n >= 0), "n should be non-negative integer"
  73. for _ in range(n):
  74. shift_left_once(lst)
  75.  
  76. def shift_left(lst, n):
  77. """Shifts the lst over by n indices
  78.  
  79. >>> lst = [1, 2, 3, 4, 5]
  80. >>> shift_left(lst, 2)
  81. >>> lst
  82. [3, 4, 5, 1, 2]
  83. """
  84. assert (n >= 0), "n should be non-negative integer"
  85. if n == 0:
  86. return
  87. shift_left_once(lst)
  88. shift_left(lst, n-1)
  89.  
  90. tmp = lst[i]
  91. dst = i
  92. src = (dst - ntimes) % len
  93. while src != i:
  94. lst[dst] = lst[src]
  95. dst = src
  96. src = (dst - ntimes) % len
  97. lst[dst] = tmp
  98.  
  99. def shift_left(l, n):
  100. """
  101. In place shift n elements of list l to the left.
  102. Won't work on strings.
  103. """
  104. n = n % len(l)
  105. head = l[:n]
  106. l[:n] = []
  107. l.extend(head)
  108. return l
  109.  
  110. import unittest
  111. from random import randrange
  112.  
  113. class TestShiftLeft(unittest.TestCase):
  114. def test_zero_shifts(self):
  115. self.assertEqual([1], shift_left([1], 0))
  116. self.assertEqual([1, 2], shift_left([1, 2], 0))
  117.  
  118. def test_single_element(self):
  119. self.assertEqual([1], shift_left([1], 1))
  120. self.assertEqual([1], shift_left([1], 2))
  121. self.assertEqual([1], shift_left([1], 3))
  122.  
  123. def test_two_elements(self):
  124. self.assertEqual([2, 1], shift_left([1, 2], 1))
  125. self.assertEqual([1, 2], shift_left([1, 2], 2))
  126. self.assertEqual([2, 1], shift_left([1, 2], 3))
  127. self.assertEqual([1, 2], shift_left([1, 2], 4))
  128.  
  129. def test_odd_number_elements(self):
  130. self.assertEqual([2, 3, 1], shift_left([1, 2, 3], 1))
  131. self.assertEqual([3, 1, 2], shift_left([1, 2, 3], 2))
  132. self.assertEqual([1, 2, 3], shift_left([1, 2, 3], 3))
  133. self.assertEqual([2, 3, 1], shift_left([1, 2, 3], 4))
  134.  
  135. def test_even_number_elements(self):
  136. self.assertEqual([2, 3, 4, 1], shift_left([1, 2, 3, 4], 1))
  137. self.assertEqual([3, 4, 1, 2], shift_left([1, 2, 3, 4], 2))
  138. self.assertEqual([4, 1, 2, 3], shift_left([1, 2, 3, 4], 3))
  139. self.assertEqual([1, 2, 3, 4], shift_left([1, 2, 3, 4], 4))
  140. self.assertEqual([2, 3, 4, 1], shift_left([1, 2, 3, 4], 5))
  141.  
  142. def test_len_l_shift(self):
  143. l = list(range(randrange(1000)))
  144. self.assertEqual(l, shift_left(l, len(l)))
  145.  
  146. if __name__ == '__main__':
  147. unittest.main()
  148.  
  149. x=int(input("enter size of list"))
  150. lst=[]
  151. for c in range(x):
  152. e=int(input("enter element"))
  153. lst.append(e)
  154. sw=int(input("enter no. of rotations"))
  155.  
  156. nw=[]
  157. while(sw!=0):
  158. for no in range(sw):
  159. temp=lst.pop()
  160. nw.append(temp)
  161. nw.reverse()
  162. lst=nw+lst
  163. nw.clear()
  164. print(lst)
  165. sw= int(input("enter no. of rotations"))
Add Comment
Please, Sign In to add comment