Advertisement
Guest User

Untitled

a guest
Mar 20th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import sys
  2. import string
  3.  
  4.  
  5. def distance_from_edge(N, x, y):
  6. """Return the distance of (x,y) from the edge of an NxN square.
  7.  
  8. The returned value will be in [0,N-1].
  9. """
  10.  
  11. size = 2*N - 1 # could pass as param, but...
  12.  
  13. # force x and y coords to be in top-left quadrant
  14. if x >= N:
  15. x = size - 1 - x
  16. if y >= N:
  17. y = size - 1 - y
  18.  
  19. # figure out the distance from edge in TL quadrant
  20. if y <= x:
  21. return y
  22. return x
  23.  
  24.  
  25. # get the desired size from the command line
  26. if len(sys.argv) != 2:
  27. print('Usage: test.py N')
  28. sys.exit(1)
  29.  
  30. try:
  31. N = int(sys.argv[1])
  32. except ValueError:
  33. print('The N value must be an integer value, 1 to 26 inclusive.')
  34. sys.exit(1)
  35.  
  36. if not (1 <= N <= 26):
  37. print('The N value must be an integer value, 1 to 26 inclusive.')
  38. sys.exit(1)
  39.  
  40. # calculate "size" and the characters we will need
  41. size = 2*N - 1
  42. chars = string.ascii_uppercase[:N]
  43.  
  44. # print the square, substituting the character for the distance integer
  45. for y in range(size):
  46. for x in range(size):
  47. ch = chars[distance_from_edge(N, x, y)]
  48. print(ch, end='')
  49. print() # finish the row
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement