-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookSayDigits.py
54 lines (46 loc) · 1.19 KB
/
LookSayDigits.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
#!/usr/bin/python3.7
"""
Написать генератор LookSay() цифр последовательности Конвея «Look and Say». (Сама последовательность Конвея). Описание в Википедии
for i,l in enumerate(LookSay()):
print(f"{i}: {l}")
if i>10:
break
0: 1
1: 1
2: 1
3: 2
4: 1
5: 1
6: 2
7: 1
8: 1
9: 1
10: 1
11: 1 """
def LookSay(firstval = 1):
yield 1
count = 1
yield count
currentval = str(firstval) + str(count)
yield int(firstval)
while True:
result = ""
prevval = currentval[0]
count = 1
for currentsub in currentval[1:]:
if prevval != currentsub:
yield count
yield int(prevval)
result += str(count) + str(prevval)
prevval = currentsub
count = 1
else:
count += 1
yield count
yield int(prevval)
result += str(count) + str(prevval)
currentval = result
for i,l in enumerate(LookSay()):
print(f"{i}: {l}")
if i>28:
break