-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_sum.py
38 lines (30 loc) · 898 Bytes
/
three_sum.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
import unittest
import timeit
class ThreeSum:
def __init__(self, array):
self.array = array
self.n = len(self.array)
def count(self):
count = 0
for i in range(self.n):
for j in range(i+1, self.n):
for k in range(j+1, self.n):
if (self.array[i] + self.array[j] + self.array[k] == 0):
count += 1
return count
class ThreeSumTest(unittest.TestCase):
def test_count(self):
arr1 = [-3, -2, -1, 0, 1, 2, 3]
obj = ThreeSum(arr1)
self.assertEqual(obj.count(), 5)
arr2 = [3, 2, 1, 0, 1, 2, 3]
obj = ThreeSum(arr2)
self.assertEqual(obj.count(), 0)
if __name__ == '__main__':
setup = '''
from __main__ import ThreeSum
import random
array = random.sample(range(-1000, 1000), 100)
'''
print(max(timeit.repeat("ThreeSum(array).count()", setup= setup, number= 10)))
unittest.main()