Aug-13-2018, 11:16 AM
Hi Python Experts,
I'm trying to solve this problem but I am having a hard time understanding what exactly it is that I'm supposed to do? Or what is the desired output?
I have spent hours in Google/YouTube trying to help myself understand the tasks but I haven't found one, unfortunately.
If anyone of you could give me any ideas how to solve this, I would really appreciate it. Thank you
The tasks:
1. There is one small bug in the continent counter. Can you find it and fix it? (Hint: change the world so that the continent borders the edge)
2. Write a function that generates an n x n sized board with either land or water chosen randomly.
==============================================================================
I'm trying to solve this problem but I am having a hard time understanding what exactly it is that I'm supposed to do? Or what is the desired output?
I have spent hours in Google/YouTube trying to help myself understand the tasks but I haven't found one, unfortunately.
If anyone of you could give me any ideas how to solve this, I would really appreciate it. Thank you
The tasks:
1. There is one small bug in the continent counter. Can you find it and fix it? (Hint: change the world so that the continent borders the edge)
2. Write a function that generates an n x n sized board with either land or water chosen randomly.
==============================================================================
M = "land" o = "water" world = [ [o,o,o,o,o,o,o,o,o,o,o], [o,o,o,o,M,M,o,o,o,o,o], [o,o,o,o,o,o,o,o,M,M,o], [o,o,o,M,o,o,o,o,o,M,o], [o,o,o,M,o,M,M,o,o,o,o], [o,o,o,o,M,M,M,M,o,o,o], [o,o,o,M,M,M,M,M,M,M,o], [o,o,o,M,M,o,M,M,M,o,o], [o,o,o,o,o,o,M,M,o,o,o], [o,M,o,o,o,M,o,o,o,o,o], [o,o,o,o,o,o,o,o,o,o,o] ] def continent_counter(world, x, y): if world[y][x] != "land": return 0 size = 1 world[y][x] = "counted land" #row above size = size + continent_counter(world, x-1, y-1) # print("first recursion size: ", size) size = size + continent_counter(world, x, y-1) size = size + continent_counter(world, x+1, y-1) #same row size = size + continent_counter(world, x-1, y) size = size + continent_counter(world, x+1, y) #row below size = size + continent_counter(world, x-1, y+1) size = size + continent_counter(world, x, y+1) size = size + continent_counter(world, x+1, y+1) return size print(continent_counter(world, 5, 5))