Skip to content

Instantly share code, notes, and snippets.

@monkeymantra
Last active June 22, 2017 17:22
Show Gist options
  • Save monkeymantra/dc9a4afe82b2bde1654944591a06f8db to your computer and use it in GitHub Desktop.
Save monkeymantra/dc9a4afe82b2bde1654944591a06f8db to your computer and use it in GitHub Desktop.
Generate the following sequence:
[1, 11, 21, 1211, 111221, 312211, 13112221 ]
Starting with "1", generate a sequence of numbers that describe the previous member of the sequence.
In this example, "1" is followed by "11" because there's "one 1". Then, to describe "11", you use "21" because
there are "two 1's". The next, 1211, describes "21" because there's "1 2, and 1 1". The next is "1 1, 1 2, 2 2s, and 2 1s"
hence "111221", Which leads to "3 1, 2 2, 1 1".
def make_sequence(num_to_produce):
initial = "1"
for sequence_num in range(0,num_to_produce):
this_string = ""
current_num = None
current_count = 0
for index, number in enumerate(initial):
if number == current_num:
current_count += 1
else:
if current_count >= 1:
this_string += "{}{}".format(current_count, current_num)
current_num = number
current_count = 1
if len(initial) == index + 1:
this_string += "{}{}".format(current_count, current_num)
initial = this_string
yield this_string
if __name__ == "__main__":
print list(make_sequence(50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment