Last active
January 15, 2019 18:20
-
-
Save huangsam/1358941 to your computer and use it in GitHub Desktop.
Prints n iterations of a certain pattern...guess it!
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/env python | |
import sys | |
def pattern(n): | |
"""Prints n iterations of a certain pattern...guess it!""" | |
old = [1] | |
print("1") | |
for i in range(n - 1): | |
new = [] | |
for j in set(old): | |
new.append([old.count(j), j]) | |
temp = '' # immutable object | |
for j in sorted(new, key=lambda i: i[1]): | |
temp += ''.join([str(x) for x in j]) | |
old = [] | |
for j in temp: | |
old.append(int(j)) | |
if i > 14: # too slow... ;) | |
print("You suck! Look back at the previous ones.") | |
break | |
print(''.join([str(k) for k in old])) | |
def main(): | |
try: | |
k = int(sys.argv[1]) | |
if k < 1: | |
print("You must request at least 1 pattern!") | |
return | |
pattern(int(sys.argv[1])) | |
except IndexError: | |
print(' '.join(["usage: python", sys.argv[0], "[n patterns]"])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment