fork download
  1. # a function to create a random map as simulated input for testing:
  2. def get_map(x_size, y_size, min_height, max_height):
  3. import random
  4. return [[random.randint(min_height, max_height) for x in range(x_size)] for y in range(y_size)]
  5.  
  6. # a function to nicely print the map for debug and visualization
  7. def print_map(hmap):
  8. print(*hmap, sep="\n")
  9.  
  10.  
  11. # calculate approximate land area where the height is greater than zero
  12. # map is a list of lists, tile_size is in m², min_level is the sea level
  13. def calc_land_area(hmap, tile_size=100, min_level=0):
  14. land_tiles = sum(len([tile for tile in row if tile>min_level]) for row in hmap)
  15. return tile_size * land_tiles
  16.  
  17.  
  18. try:
  19. hmap = get_map(5, 5, 0, 3)
  20. print_map(hmap)
  21. print("land area:", calc_land_area(hmap), "m²")
  22. except Exception as e:
  23. print(e)
Success #stdin #stdout 0.05s 12360KB
stdin
Standard input is empty
stdout
[2, 0, 3, 0, 2]
[3, 0, 0, 0, 2]
[1, 0, 0, 1, 2]
[3, 3, 3, 2, 1]
[3, 1, 1, 3, 0]
land area: 1700 m²