""" chapter 11, problem 18 random walk sidewalk with reporting of how many times the person has been in each square Cell() Cell.enter() Cell.count how many times it was visited Sidewalk(length) Sidewalk.cells[0 ... length-1] Sidewalk.current_position Sidewalk.step() take one step, update cell counts Sidewalk.fallen_off() true or false Sidewalk.do_random_walk() Sidewalk.summary() return string of situation """ class Cell: def __init__(self): self.count = 0 def enter(self): """ move into this cell""" self.count += 1 class Sidewalk: def __init__(self, length): self.cells = [None] * length # create empty list for i in range(length): self.cells[i] = Cell() # fills it with cells self.current_position = length/2 self.cells[self.current_position].enter() def do_random_walk(self): ## ... didn't quite finish this before class ran out. ## Basic idea : # while (not off end): # move left or right with 50/50 probability # enter that cell pass def summary(self): """ Return string summary of counts of each cell """ result = "how many times on each cell : " for cell in self.cells: result += (" " + str(cell.count)) return result def main(): print "Sidewalk random walk simulation" size = int(input("How big is the sidewalk? ")) sidewalk = Sidewalk(size) sidewalk.do_random_walk() print sidewalk.summary() main()