Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- UPPER_BOUND = 100 # highest number to go to per tile
- TILE_WIDTH = 3
- TILE_HEIGHT = 3
- def generate_random_tile(width, height):
- tile = []
- for y in range(0, height):
- horizontal_tile = []
- for x in range(0, width):
- horizontal_tile.append(random.randint(1, UPPER_BOUND))
- tile.append(horizontal_tile)
- return tile
- def square_every_tile(tile):
- new_tile = tile
- width, height = tile_get_size(new_tile)
- for y in range(0, height):
- for x in range(0, width):
- new_tile[y][x] = new_tile[y][x] ** 2
- return new_tile
- def tile_get_size(tile):
- # return int of width and height of tile
- # width, height
- return len(tile[0]), len(tile)
- def tile_sum_horizontals(tile):
- width, height = tile_get_size(tile)
- sum_list = []
- for y in range(0, height):
- sum_list.append(sum(tile[y]))
- #print(f"{y}, {sum(tile[y])}, {sum_list}")
- return sum_list
- def tile_sum_verticals(tile):
- width, height = tile_get_size(tile)
- sum_list = []
- for x in range(0, width):
- sum_x = 0
- for y in range(0, height):
- sum_x += tile[y][x]
- sum_list.append(sum_x)
- return sum_list
- def tile_sum_diagonals(tile):
- width, height = tile_get_size(tile)
- coords_tl_br = get_diagonal_coords_topleft_to_bottomright(width, height)
- coords_bl_tr = get_diagonal_coords_bottomleft_to_topright(width, height)
- sum_list = []
- sum_tl_br = 0
- for coord in coords_tl_br:
- y = coord[0]
- x = coord[1]
- sum_tl_br += tile[y][x]
- sum_list.append(sum_tl_br)
- sum_bl_tr = 0
- for coord in coords_bl_tr:
- y = coord[0]
- x = coord[1]
- sum_bl_tr += tile[y][x]
- sum_list.append(sum_tl_br)
- return sum_list
- def all_sums(tile):
- return tile_sum_horizontals(tile) + tile_sum_verticals(tile) + tile_sum_diagonals(tile)
- def all_sums_same(all_sums_list):
- first_val = all_sums_list[0]
- for val in all_sums_list:
- if val != first_val:
- return False
- return True
- def get_diagonal_coords_topleft_to_bottomright(width, height):
- # return list of tuples for diagonals of a given WxH tile
- list_out = []
- # do top-left to bottom-right
- for i in range(0, height):
- list_out.append((i,i))
- return list_out
- def get_diagonal_coords_bottomleft_to_topright(width, height):
- # return list of tuples for diagonals of a given WxH tile
- list_out = []
- for i in range(0, height):
- list_out.append((height-(i+1), i))
- return list_out
- test_tile = [[2,2,2],[2,2,2],[2,2,2]]
- print(all_sums(test_tile))
- print(all_sums_same(all_sums(test_tile)))
- found = False
- while found is False:
- tile = generate_random_tile(TILE_WIDTH, TILE_HEIGHT)
- square = square_every_tile(tile)
- print(f"{tile} -> {square}")
- if all_sums_same(all_sums(square)):
- break
- ##tile = generate_random_tile(TILE_WIDTH, TILE_HEIGHT)
- ##print(tile)
- ##square = square_every_tile(tile)
- ##print(square)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement