Skip to content

Instantly share code, notes, and snippets.

@itsbth
Last active December 10, 2015 22:51
Show Gist options
  • Save itsbth/dd6c3178d5d66bf1d6ce to your computer and use it in GitHub Desktop.
Save itsbth/dd6c3178d5d66bf1d6ce to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import logging
def rle(iterable):
_next = next
it = iter(iterable)
value, count = None, 0
try:
while True:
item = _next(it)
if item == value:
count += 1
else:
if count > 0:
yield count, value
value = item
count = 1
except StopIteration:
if count > 0:
yield count, value
# number_to_string = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
def look_and_say(num):
# save ~100ms
# _nt = number_to_string
for c, v in rle(num):
# yield _nt[c]
yield c
yield v
if __name__ == '__main__':
logging.basicConfig(format='[+%(relativeCreated)d ms] %(levelname)s %(message)s', level=logging.INFO)
start = map(int, '1113222113')
for _ in range(50):
start = look_and_say(start)
logging.info("Iteration #{}".format(_))
logging.info('Solution: {}'.format(sum(1 for _ in start)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment