forked from girishgupta211-zz/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
look_n_say.py
59 lines (44 loc) · 1.29 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def look_n_say_iterative(input_count):
if input_count == 1:
return "1"
if input_count == 2:
return "11"
result = "11"
for index in range(2, input_count):
count = 1
output = ""
for i in range(1, len(result)):
if result[i] != result[i - 1]:
output = output + str(count) + result[i - 1]
count = 1
else:
count = count + 1
# if element is in last
if i == len(result) - 1:
output = output + str(count) + result[i]
result = output
return result
input_count = 6
result = look_n_say_iterative(input_count)
print(result)
def look_n_say_recursion(input_count):
if input_count == 1:
return "1"
if input_count == 2:
return "11"
result = look_n_say_recursion(input_count - 1)
count = 1
output = ""
for i in range(1, len(result)):
if result[i] != result[i - 1]:
output = output + str(count) + result[i - 1]
count = 1
else:
count = count + 1
# if element is in last
if i == len(result) - 1:
output = output + str(count) + result[i]
return output
input_count = 6
result = look_n_say_recursion(input_count)
print(result)