-
Notifications
You must be signed in to change notification settings - Fork 1
/
look_n_say,py
41 lines (38 loc) · 1.04 KB
/
look_n_say,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
def look_n_say(term):
if term == 1:
return "1"
elif term == 2:
return "11"
result = "11"
for index in range(2,term):
count = 1
output = ""
for i in range(1,len(result)):
if result[i] == result[i-1]:
count += 1
else:
output += str(count) + result[i-1]
count = 1
if i == len(result) - 1:
output += str(count) + result[i]
result = output
return result
def look_n_say_recursive(term):
if term == 1:
return "1"
elif term == 2:
return "11"
result = look_n_say(term-1)
for index in range(2, term):
count = 1
output = ""
for i in range(1, len(result)):
if result[i] == result[i - 1]:
count += 1
else:
output += str(count) + result[i - 1]
count = 1
if i == len(result) - 1:
output += str(count) + result[i]
result = output
print(look_n_say(5))