-
Notifications
You must be signed in to change notification settings - Fork 3
/
Buddy_pairs.py
50 lines (35 loc) · 1.07 KB
/
Buddy_pairs.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
# Kata link:
# https://www.codewars.com/kata/59ccf051dcc4050f7800008f/train/python
# -------------------------------------
# -------------------------------------
# Solution:
def s(x):
t = []
for p in ([i, x//i] for i in range(1, int(x**0.5)+1) if not x % i):
for n in p:
t.append(n)
return sum(set(t)) - x
def buddy(start, limit):
for n in range(start,limit):
t_m = n + 1
t_n = s(n)
m = t_n - 1
if(m < start):
continue
if(s(m) == t_m):
return [n,m]
return 'Nothing'
# -------------------------------------
# Test
import unittest
class Test(unittest.TestCase):
def test1(self):
return self.assertEqual(buddy(10, 50), [48, 75])
def test2(self):
return self.assertEqual(buddy(2177, 4357), "Nothing")
def test3(self):
return self.assertEqual(buddy(57345, 90061), [62744, 75495])
def test4(self):
return self.assertEqual(buddy(1071625, 1103735), [1081184, 1331967])
if __name__ == '__main__':
unittest.main(verbosity=2)