-
Notifications
You must be signed in to change notification settings - Fork 17
/
circular-array-loop.py
55 lines (37 loc) · 1.45 KB
/
circular-array-loop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import unittest
# TODO: Create solution with O(1) extra space
class Solution:
def circularArrayLoop(self, nums):
for start in range(len(nums)):
visited = [None] * len(nums)
prev_pos = None
pos = start
while not visited[pos] and nums[start] * nums[pos] > 0:
visited[pos] = True
prev_pos = pos
pos += nums[pos]
pos = pos % len(nums) if pos % len(nums) >= 0 else len(nums) - abs(pos) % len(nums)
if visited[pos] and pos != prev_pos and nums[start] * nums[pos] > 0:
return True
return False
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_1(self):
self.assertFalse(self.sol.circularArrayLoop([1]))
def test_2(self):
self.assertTrue(self.sol.circularArrayLoop([1,1]))
def test_3(self):
self.assertTrue(self.sol.circularArrayLoop([2,-1,1,2,2]))
def test_4(self):
self.assertFalse(self.sol.circularArrayLoop([-1,2]))
def test_5(self):
self.assertFalse(self.sol.circularArrayLoop([-2,1,-1,-2,-2]))
def test_6(self):
self.assertFalse(self.sol.circularArrayLoop([-1]))
def test_7(self):
self.assertFalse(self.sol.circularArrayLoop([-2]))
def test_8(self):
self.assertFalse(self.sol.circularArrayLoop([-1,-2,-3,-4,-5]))
if __name__ == "__main__":
unittest.main()