Created
October 28, 2014 16:19
-
-
Save akaz00/e4d3a3372faa3e7a7dfb to your computer and use it in GitHub Desktop.
lookandsay seq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def mysplit(target): | |
| tuples = [] | |
| prev = target[0] | |
| count = 1 | |
| for idx, c in enumerate(target[:-1]): | |
| if prev != target[idx+1]: | |
| tuples.append((prev, count)) | |
| count = 1 | |
| else: | |
| count += 1 | |
| if idx == len(target) - 2: | |
| tuples.append((target[-1], count)) | |
| prev = target[idx+1] | |
| return tuples | |
| def lookandsay(target): | |
| if len(target) == 1: | |
| return "11" | |
| ret = "" | |
| for t in mysplit(target): | |
| ret += (str(t[1]) + "" + t[0]) | |
| return ret | |
| def lseq(times): | |
| st = "1" | |
| for i in range(times): | |
| ret = lookandsay(st) | |
| print ret | |
| st = ret | |
| lseq(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment