Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import string
- def distance_from_edge(N, x, y):
- """Return the distance of (x,y) from the edge of an NxN square.
- The returned value will be in [0,N-1].
- """
- size = 2*N - 1 # could pass as param, but...
- # force x and y coords to be in top-left quadrant
- if x >= N:
- x = size - 1 - x
- if y >= N:
- y = size - 1 - y
- # figure out the distance from edge in TL quadrant
- if y <= x:
- return y
- return x
- # get the desired size from the command line
- if len(sys.argv) != 2:
- print('Usage: test.py N')
- sys.exit(1)
- try:
- N = int(sys.argv[1])
- except ValueError:
- print('The N value must be an integer value, 1 to 26 inclusive.')
- sys.exit(1)
- if not (1 <= N <= 26):
- print('The N value must be an integer value, 1 to 26 inclusive.')
- sys.exit(1)
- # calculate "size" and the characters we will need
- size = 2*N - 1
- chars = string.ascii_uppercase[:N]
- # print the square, substituting the character for the distance integer
- for y in range(size):
- for x in range(size):
- ch = chars[distance_from_edge(N, x, y)]
- print(ch, end='')
- print() # finish the row
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement