Last active
December 12, 2023 17:20
-
-
Save efeciftci/89f83642fc5e8306ac78 to your computer and use it in GitHub Desktop.
look and say sequence in python
This file contains 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
#!/usr/bin/python3 | |
# Look-and-say sequence: https://en.wikipedia.org/wiki/Look-and-say_sequence | |
num = int(input("Enter nth term: ")) | |
i = 0 | |
while i < num: | |
if i == 0: | |
series = [1] | |
else: | |
cnt = 1 | |
tmpseries = [] | |
for j in range(1, len(series)): | |
if series[j] == series[j - 1]: | |
cnt += 1 | |
else: | |
tmpseries += [cnt, series[j - 1]] | |
cnt = 1 | |
tmpseries += [cnt, series[len(series) - 1]] | |
series = tmpseries | |
i += 1 | |
print("Term", i, "-", series) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment