10_lookandsay.py 527 B

12345678910111213141516171819202122
  1. #!/usr/bin/env python3
  2. from collections import deque
  3. def play(seq, n):
  4. curseq = deque(map(int, seq))
  5. nextseq = deque()
  6. for step in range(n):
  7. while curseq:
  8. i = curseq.popleft()
  9. count = 1
  10. while curseq and curseq[0] == i:
  11. curseq.popleft()
  12. count += 1
  13. nextseq.extend((count, i))
  14. curseq, nextseq = nextseq, curseq
  15. nextseq.clear()
  16. return len(curseq)
  17. print(play('1113122113', 40))
  18. print(play('1113122113', 50))