Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- organismMap = []
- def exploreOrganism(world,i,j,counter):
- global organismMap
- N = len(world) #num of rows
- M = len(world[0]) # num of columns
- if i < 0 or j < 0 or i >= N or j >= M:
- return
- if world[i][j] is 1 and organismMap[i][j] is 0:
- organismMap[i][j]=counter # i = row j = column
- exploreOrganism(world,i+1,j,counter) #down
- exploreOrganism(world,i-1,j,counter) #up
- exploreOrganism(world,i,j-1,counter) #left
- exploreOrganism(world,i,j+1,counter) #right
- def readFile(filename):
- f = open(filename,"r")
- world = []
- for line in f:
- world.append([ int (x) for x in line.split(',')])
- return world
- def printOrganismMap():
- global organismMap
- N = len(organismMap)
- for i in range(N):
- print(organismMap[i])
- def initializeMap(N,M):
- return [[0 for x in range(M)] for x in range(N)]
- def numOrganisms(world):
- global organismMap
- N = len(world) # num of rows
- M = len(world[0]) # num of columns
- organismsCounter = 0
- organismMap = initializeMap(N,M)
- for x in range(N):
- for y in range(M):
- if world[x][y] is 1 and organismMap[x][y] is 0:
- organismsCounter += 1
- exploreOrganism(world, x, y, organismsCounter) #call exploreOrganism - each cell will go down up left right
- print('Number of organisms = ', organismsCounter)
- printOrganismMap()
- world = readFile('world.txt')
- numOrganisms(world)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement