-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotpo.py
64 lines (51 loc) · 1.86 KB
/
hotpo.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
56
57
58
59
60
61
62
63
"""
Take any positive integer n. If n is even divide it by 2. If n is odd multiply it by 3 and add 1.
Repeat the process (aka: "Half Or Triple Plus One") indefinitely.
It's thought that no matter what number you start with you will always eventually reach 1.
Create a function that returns the number of steps taken to reach 1.
More information : https://en.wikipedia.org/wiki/Collatz_conjecture
For instance:
starting with n = 6 the sequence is 6->3->10->5->16->8->4->2->1.
Hence, HOTPO(6) == 8
n = 13 the sequence is 13->40->20->10->5->16->8->4->2->1.
Hence, HOTPO(13) == 9
"""
def is_even(n):
return n % 2 == 0
def hotpo(n,total=0):
if 0 < n <=1:
return total
else:
if is_even(n):
return hotpo(n /2 , total= total + 1)
else:
return hotpo((n*3)+1 , total= total + 1)
import unittest
class TestStringMethods(unittest.TestCase):
def test_first(self):
test = self
Test = self
self.assertEqual(hotpo(2), 1)
self.assertEqual(hotpo(3), 7)
self.assertEqual(hotpo(1), 0)
self.assertEqual(hotpo(4), 2)
self.assertEqual(hotpo(5), 5)
self.assertEqual(hotpo(6), 8)
self.assertEqual(hotpo(7), 16)
self.assertEqual(hotpo(8), 3)
self.assertEqual(hotpo(9), 19)
self.assertEqual(hotpo(10), 6)
self.assertEqual(hotpo(11), 14)
self.assertEqual(hotpo(12), 9)
self.assertEqual(hotpo(13), 9)
self.assertEqual(hotpo(14), 17)
self.assertEqual(hotpo(20), 7)
self.assertEqual(hotpo(40), 8)
self.assertEqual(hotpo(60), 19)
self.assertEqual(hotpo(80), 9)
self.assertEqual(hotpo(100), 25)
self.assertEqual(hotpo(120), 20)
self.assertEqual(hotpo(140), 15)
self.assertEqual(hotpo(6), 8)
self.assertEqual(hotpo(13), 9)
self.assertEqual(hotpo(3), 7)