Ihmemies

Kirjainlaatikko ohj1 MOOC 2022

Jun 22nd, 2022 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # populates a dict with 0-25 key and A-Z values
  2. def generate_letters(letters:dict):
  3.     key = 0
  4.     for i in range(65,91): # A-Z
  5.         letters[key] = chr(i)
  6.         key += 1
  7.  
  8. # renders 1/4 of the square shape, example below:
  9. # CCC
  10. # CBB
  11. # CBA
  12. # == all we need to render the whole shape later
  13. def construct_shape(shape: dict, letters:dict, layers: int):
  14.     dist_from_x = layers-1
  15.  
  16.     for i in range(layers):
  17.         #initialize a blank key-value to avoid key errors
  18.         shape[i] = ""
  19.  
  20.         # first row
  21.         if i == 0:
  22.              shape[i] = letters[layers-1] * (layers)
  23.  
  24.         # middle row
  25.         elif i == layers-1:
  26.             middle = 1
  27.             # first left to middle letter
  28.             while middle <= layers:          
  29.                 shape[i] += letters[layers-middle]
  30.                 middle += 1
  31.             middle -= 1
  32.        
  33.         # rows in between
  34.         else:
  35.             dist_from_x -= 1
  36.             temp = layers-1
  37.             for val in range(layers, 0, -1):
  38.                 shape[i] += letters[temp]
  39.                 if temp > dist_from_x:
  40.                     temp -=1
  41.  
  42. # copies the left top quadrant (1/4) characters to left bottom.
  43. # then copies the left side charaters to the right side
  44. # function has probably a bad name, i don't even know what it means
  45. def transpose(matrix: dict, layers: int):
  46.     # read from top to middle, and write from middle to bottom
  47.     original_key = layers-2
  48.     new_key = layers
  49.     while original_key > -1:
  50.         matrix[new_key] = matrix[original_key]
  51.         new_key += 1
  52.         original_key -= 1
  53.  
  54.     # reverse the string and add it to the previous string
  55.     for key, row in matrix.items():
  56.         last_char = int(-layers-1)
  57.         matrix[key] += row[-2:last_char:-1]
  58.  
  59. # prints the final shape to terminal
  60. def print_shape(shape: dict):
  61.     for key, row in shape.items():
  62.         print(row)
  63.  
  64. def main():
  65.     letters = {}
  66.     shape = {}
  67.     layers = int(input("Kerrokset:"))
  68.  
  69.     generate_letters(letters)
  70.     construct_shape(shape, letters, layers) # create 1/4 of shape
  71.     transpose(shape, layers)   # flip & transform to fill the whole pattern
  72.     print_shape(shape) # print final result
  73.  
  74. main()
  75.  
Add Comment
Please, Sign In to add comment