Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # populates a dict with 0-25 key and A-Z values
- def generate_letters(letters:dict):
- key = 0
- for i in range(65,91): # A-Z
- letters[key] = chr(i)
- key += 1
- # renders 1/4 of the square shape, example below:
- # CCC
- # CBB
- # CBA
- # == all we need to render the whole shape later
- def construct_shape(shape: dict, letters:dict, layers: int):
- dist_from_x = layers-1
- for i in range(layers):
- #initialize a blank key-value to avoid key errors
- shape[i] = ""
- # first row
- if i == 0:
- shape[i] = letters[layers-1] * (layers)
- # middle row
- elif i == layers-1:
- middle = 1
- # first left to middle letter
- while middle <= layers:
- shape[i] += letters[layers-middle]
- middle += 1
- middle -= 1
- # rows in between
- else:
- dist_from_x -= 1
- temp = layers-1
- for val in range(layers, 0, -1):
- shape[i] += letters[temp]
- if temp > dist_from_x:
- temp -=1
- # copies the left top quadrant (1/4) characters to left bottom.
- # then copies the left side charaters to the right side
- # function has probably a bad name, i don't even know what it means
- def transpose(matrix: dict, layers: int):
- # read from top to middle, and write from middle to bottom
- original_key = layers-2
- new_key = layers
- while original_key > -1:
- matrix[new_key] = matrix[original_key]
- new_key += 1
- original_key -= 1
- # reverse the string and add it to the previous string
- for key, row in matrix.items():
- last_char = int(-layers-1)
- matrix[key] += row[-2:last_char:-1]
- # prints the final shape to terminal
- def print_shape(shape: dict):
- for key, row in shape.items():
- print(row)
- def main():
- letters = {}
- shape = {}
- layers = int(input("Kerrokset:"))
- generate_letters(letters)
- construct_shape(shape, letters, layers) # create 1/4 of shape
- transpose(shape, layers) # flip & transform to fill the whole pattern
- print_shape(shape) # print final result
- main()
Add Comment
Please, Sign In to add comment