Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Look-and-say-sequence/Python/look-and-say-sequence-1.py
Normal file
22
Task/Look-and-say-sequence/Python/look-and-say-sequence-1.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
def lookandsay(number):
|
||||
result = ""
|
||||
|
||||
repeat = number[0]
|
||||
number = number[1:]+" "
|
||||
times = 1
|
||||
|
||||
for actual in number:
|
||||
if actual != repeat:
|
||||
result += str(times)+repeat
|
||||
times = 1
|
||||
repeat = actual
|
||||
else:
|
||||
times += 1
|
||||
|
||||
return result
|
||||
|
||||
num = "1"
|
||||
|
||||
for i in range(10):
|
||||
print num
|
||||
num = lookandsay(num)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
>>> from itertools import groupby
|
||||
>>> def lookandsay(number):
|
||||
return ''.join( str(len(list(g))) + k
|
||||
for k,g in groupby(number) )
|
||||
|
||||
>>> numberstring='1'
|
||||
>>> for i in range(10):
|
||||
print numberstring
|
||||
numberstring = lookandsay(numberstring)
|
||||
20
Task/Look-and-say-sequence/Python/look-and-say-sequence-3.py
Normal file
20
Task/Look-and-say-sequence/Python/look-and-say-sequence-3.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
>>> from itertools import groupby, islice
|
||||
>>>
|
||||
>>> def lookandsay(number='1'):
|
||||
while True:
|
||||
yield number
|
||||
number = ''.join( str(len(list(g))) + k
|
||||
for k,g in groupby(number) )
|
||||
|
||||
|
||||
>>> print('\n'.join(islice(lookandsay(), 10)))
|
||||
1
|
||||
11
|
||||
21
|
||||
1211
|
||||
111221
|
||||
312211
|
||||
13112221
|
||||
1113213211
|
||||
31131211131221
|
||||
13211311123113112211
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import re
|
||||
|
||||
def lookandsay(str):
|
||||
return re.sub(r'(.)\1*', lambda m: str(len(m.group(0))) + m.group(1), str)
|
||||
|
||||
num = "1"
|
||||
for i in range(10):
|
||||
print num
|
||||
num = lookandsay(num)
|
||||
Loading…
Add table
Add a link
Reference in a new issue