SHARE
TWEET

Untitled

a guest Jun 16th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. UPPER_BOUND = 100 # highest number to go to per tile
  4. TILE_WIDTH = 3
  5. TILE_HEIGHT = 3
  6.  
  7.  
  8. def generate_random_tile(width, height):
  9.     tile = []
  10.     for y in range(0, height):
  11.         horizontal_tile = []
  12.         for x in range(0, width):
  13.             horizontal_tile.append(random.randint(1, UPPER_BOUND))
  14.         tile.append(horizontal_tile)
  15.     return tile
  16.  
  17. def square_every_tile(tile):
  18.     new_tile = tile
  19.     width, height = tile_get_size(new_tile)
  20.  
  21.     for y in range(0, height):
  22.         for x in range(0, width):
  23.             new_tile[y][x] = new_tile[y][x] ** 2
  24.     return new_tile
  25.  
  26. def tile_get_size(tile):
  27.     # return int of width and height of tile
  28.     # width, height
  29.     return len(tile[0]), len(tile)
  30.  
  31. def tile_sum_horizontals(tile):
  32.     width, height = tile_get_size(tile)
  33.     sum_list = []
  34.     for y in range(0, height):
  35.         sum_list.append(sum(tile[y]))
  36.         #print(f"{y}, {sum(tile[y])}, {sum_list}")
  37.     return sum_list
  38.  
  39. def tile_sum_verticals(tile):
  40.     width, height = tile_get_size(tile)
  41.     sum_list = []
  42.    
  43.     for x in range(0, width):
  44.         sum_x = 0
  45.         for y in range(0, height):
  46.             sum_x += tile[y][x]
  47.         sum_list.append(sum_x)
  48.        
  49.     return sum_list
  50.  
  51. def tile_sum_diagonals(tile):
  52.     width, height = tile_get_size(tile)
  53.     coords_tl_br = get_diagonal_coords_topleft_to_bottomright(width, height)
  54.     coords_bl_tr = get_diagonal_coords_bottomleft_to_topright(width, height)
  55.    
  56.     sum_list = []
  57.     sum_tl_br = 0
  58.     for coord in coords_tl_br:
  59.         y = coord[0]
  60.         x = coord[1]
  61.         sum_tl_br += tile[y][x]
  62.     sum_list.append(sum_tl_br)
  63.  
  64.     sum_bl_tr = 0
  65.     for coord in coords_bl_tr:
  66.         y = coord[0]
  67.         x = coord[1]
  68.         sum_bl_tr += tile[y][x]
  69.     sum_list.append(sum_tl_br)
  70.  
  71.     return sum_list
  72.  
  73. def all_sums(tile):
  74.     return tile_sum_horizontals(tile) + tile_sum_verticals(tile) + tile_sum_diagonals(tile)
  75.  
  76. def all_sums_same(all_sums_list):
  77.     first_val = all_sums_list[0]
  78.     for val in all_sums_list:
  79.         if val != first_val:
  80.             return False
  81.     return True
  82.    
  83.  
  84. def get_diagonal_coords_topleft_to_bottomright(width, height):
  85.     # return list of tuples for diagonals of a given WxH tile
  86.     list_out = []
  87.     # do top-left to bottom-right
  88.     for i in range(0, height):
  89.         list_out.append((i,i))
  90.  
  91.     return list_out
  92.  
  93. def get_diagonal_coords_bottomleft_to_topright(width, height):
  94.     # return list of tuples for diagonals of a given WxH tile
  95.     list_out = []
  96.        
  97.     for i in range(0, height):
  98.         list_out.append((height-(i+1), i))
  99.  
  100.     return list_out
  101.  
  102. test_tile = [[2,2,2],[2,2,2],[2,2,2]]
  103. print(all_sums(test_tile))
  104. print(all_sums_same(all_sums(test_tile)))
  105.  
  106. found = False
  107. while found is False:
  108.     tile = generate_random_tile(TILE_WIDTH, TILE_HEIGHT)
  109.     square = square_every_tile(tile)
  110.     print(f"{tile} -> {square}")
  111.     if all_sums_same(all_sums(square)):
  112.         break
  113.    
  114.  
  115. ##tile = generate_random_tile(TILE_WIDTH, TILE_HEIGHT)
  116. ##print(tile)
  117. ##square = square_every_tile(tile)
  118. ##print(square)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top