Created
February 18, 2017 06:40
-
-
Save yeelan0319/714688c37742d576eca09352ce667edf to your computer and use it in GitHub Desktop.
Rotate a list with test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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