Skip to content

Instantly share code, notes, and snippets.

@yeelan0319
Created February 18, 2017 06:40
Show Gist options
  • Save yeelan0319/714688c37742d576eca09352ce667edf to your computer and use it in GitHub Desktop.
Save yeelan0319/714688c37742d576eca09352ce667edf to your computer and use it in GitHub Desktop.
Rotate a list with test
class Solution(object):
def rotate(self, nums, k):
n = len(nums)
if n == 0:
return
start = 0
count = 0
while count < n:
curr = start
tmp = nums[curr]
while True:
next = (curr + k) % n
nextval = nums[next]
nums[next] = tmp
curr = next
tmp = nextval
count += 1
if curr == start:
break
start += 1
import unittest
from rotate import Solution
class MyTestCase(unittest.TestCase):
def setUp(self):
self.s = Solution()
def testNormalCase(self):
nums = [1, 2]
k = 1
self.s.rotate(nums, k)
expect_nums = [2, 1]
self.assertEqual(nums, expect_nums)
def testArrayLengthZero(self):
nums = []
k = 1
self.s.rotate(nums, k)
expect_nums = []
self.assertEqual(nums, expect_nums)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment