Skip to content

Instantly share code, notes, and snippets.

@maxrothman
Last active August 29, 2015 14:15
Show Gist options
  • Save maxrothman/ddaf202a4604533bf717 to your computer and use it in GitHub Desktop.
Save maxrothman/ddaf202a4604533bf717 to your computer and use it in GitHub Desktop.
Sierpinski triangles
"""
Usage: test.py [l|c|r] <n>
Print a Sierpinski's Gasket of iteration n, left, center, or right-aligned (accordingly)
"""
import string, sys
def sierpinski(n, c):
gasket = [c]
for i in range(1, n+1):
gasket += ['{0}{0:>{1}}'.format(j, 2**i) for j in gasket]
return gasket
if __name__ == '__main__':
just, char = {'l': (string.ljust, 'L'),
'c': (string.center, 'A'),
'r': (string.rjust, 'J')
}[sys.argv[1]]
n = int(sys.argv[2])
gasket = sierpinski(n, char)
print '\n'.join(just(r, 2**(n+1)) for r in gasket)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment