Skip to content

add python solution for array_rotate #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions array_rotate/solutions/rotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def rotate(array, n):
length = len(array)
if length == n or n == 0:
return array
else:
result = [0] * length
for i in xrange(length):
result[(i+n) % length] = array[i]
return result
31 changes: 31 additions & 0 deletions test_rotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/usr/env python
import unittest
from rotate import rotate as r


class RotateTestCase(unittest.TestCase):
def setUp(self):
test = [1, 2, 3]
self.test_cases = (
(test, 3),
(test, 0),
(test, 4),
(test, 5),
(test, 1)
)
self.test_answers = (
test, test,
[3, 1, 2], [2, 3, 1],
[3, 1, 2]
)

def tearDown(self):
del self.test_cases
del self.test_answers

def test_rotate_function(self):
for idx, test_case in enumerate(self.test_cases):
self.assertEqual(r(*test_case), self.test_answers[idx])

if __name__ == '__main__':
unittest.main()