Last active
          August 29, 2015 14:15 
        
      - 
      
- 
        Save maxrothman/ddaf202a4604533bf717 to your computer and use it in GitHub Desktop. 
    Sierpinski triangles
  
        
  
    
      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
    
  
  
    
  | """ | |
| 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