Created
September 29, 2013 02:22
-
-
Save jkeesh/6748788 to your computer and use it in GitHub Desktop.
Look and Say
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
import sys | |
SENTINEL = -1 | |
def lookandsay(num): | |
"""Return the look and say version of num.""" | |
num = str(num) | |
cur_digit = SENTINEL | |
cur_streak = 0 | |
result = "" | |
for cur in num: | |
if cur == cur_digit or cur_digit == SENTINEL: | |
cur_streak += 1 | |
else: | |
result += str(cur_streak) | |
result += str(cur_digit) | |
cur_streak = 1 | |
cur_digit = cur | |
result += str(cur_streak) | |
result += str(cur_digit) | |
return result | |
def lookandsay_sequence(num_items): | |
"""Print the first num_items of the look an say sequence.""" | |
cur = 1 | |
for i in range(num_items): | |
print cur | |
cur = lookandsay(cur) | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
print lookandsay(sys.argv[1]) | |
else: | |
lookandsay_sequence(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. Pretty sure this works.
Here is mine:
Also can you make this private?