- #1

- 3

- 0

This is an old lab assignment I never got around to finishing and we are allowed to resubmit it for partial credit. I don't really even know how to set up this program. I understand what I need, I just don't know how to implement it. Any help is appreciated.

"You may know that the streets and avenues of Manhattan form a grid. A random walk through the grid (i.e., Manhattan) is a walk in which a random direction (N, E, S, or W) is chosen with equal probability at every intersection. For example, a random walk on a 5 X 11 grid starting at (5, 2) could visit grid points (6,2), (7,2), (8, 2), (9, 2), (10,2 ), back to (9,2) and then back to (10, 2) before leaving the grid. Remark: coordinates given here are in the Cartesian plane.

Write a function manhattan() that takes the number of rows and columns in the grid, simulates a random walk starting in the center of the grid, computes the number of times each intersection has been visited by the random walk. Your function should print the table line by line once the random walk moves outside the grid. The function should also print the direction and coordinates of the intersection at each step of the simulation. Notice, the coordinates given in the output are the indices in the two-dimensional list representing the grid. Sample outputs are shown below:

>>> manhattan(3,3)

(x, y) 1 1

direction W (x, y) 1 0

direction N (x, y) 0 0

direction E (x, y) 0 1

direction N

1 1 0

1 1 0

0 0 0

>>> manhattan(3,3)

(x, y) 1 1

direction N (x, y) 0 1

direction E (x, y) 0 2

direction W (x, y) 0 1

direction W (x, y) 0 0

direction S (x, y) 1 0

direction E (x, y) 1 1

direction S (x, y) 2 1

direction N (x, y) 1 1

direction E (x, y) 1 2

direction N (x, y) 0 2

direction E

1 2 2

1 3 1

0 1 0

>>> manhattan(5,7)

(x, y) 2 3

direction N (x, y) 1 3

direction N (x, y) 0 3

direction S (x, y) 1 3

direction W (x, y) 1 2

direction S (x, y) 2 2

direction E (x, y) 2 3

direction S (x, y) 3 3

direction W (x, y) 3 2

direction N (x, y) 2 2

direction W (x, y) 2 1

direction E (x, y) 2 2

direction W (x, y) 2 1

direction N (x, y) 1 1

direction N (x, y) 0 1

direction S (x, y) 1 1

direction W (x, y) 1 0

direction E (x, y) 1 1

direction W (x, y) 1 0

direction W

0 1 0 1 0 0 0

2 3 1 2 0 0 0

0 2 3 2 0 0 0

0 0 1 1 0 0 0

0 0 0 0 0 0 0

>>> "

I haven not had any problems understanding material in this class, it's just sitting down and writing the program that's the problem. I just don't know where to start! Any help would be greatly appreciated! :)

"You may know that the streets and avenues of Manhattan form a grid. A random walk through the grid (i.e., Manhattan) is a walk in which a random direction (N, E, S, or W) is chosen with equal probability at every intersection. For example, a random walk on a 5 X 11 grid starting at (5, 2) could visit grid points (6,2), (7,2), (8, 2), (9, 2), (10,2 ), back to (9,2) and then back to (10, 2) before leaving the grid. Remark: coordinates given here are in the Cartesian plane.

Write a function manhattan() that takes the number of rows and columns in the grid, simulates a random walk starting in the center of the grid, computes the number of times each intersection has been visited by the random walk. Your function should print the table line by line once the random walk moves outside the grid. The function should also print the direction and coordinates of the intersection at each step of the simulation. Notice, the coordinates given in the output are the indices in the two-dimensional list representing the grid. Sample outputs are shown below:

>>> manhattan(3,3)

(x, y) 1 1

direction W (x, y) 1 0

direction N (x, y) 0 0

direction E (x, y) 0 1

direction N

1 1 0

1 1 0

0 0 0

>>> manhattan(3,3)

(x, y) 1 1

direction N (x, y) 0 1

direction E (x, y) 0 2

direction W (x, y) 0 1

direction W (x, y) 0 0

direction S (x, y) 1 0

direction E (x, y) 1 1

direction S (x, y) 2 1

direction N (x, y) 1 1

direction E (x, y) 1 2

direction N (x, y) 0 2

direction E

1 2 2

1 3 1

0 1 0

>>> manhattan(5,7)

(x, y) 2 3

direction N (x, y) 1 3

direction N (x, y) 0 3

direction S (x, y) 1 3

direction W (x, y) 1 2

direction S (x, y) 2 2

direction E (x, y) 2 3

direction S (x, y) 3 3

direction W (x, y) 3 2

direction N (x, y) 2 2

direction W (x, y) 2 1

direction E (x, y) 2 2

direction W (x, y) 2 1

direction N (x, y) 1 1

direction N (x, y) 0 1

direction S (x, y) 1 1

direction W (x, y) 1 0

direction E (x, y) 1 1

direction W (x, y) 1 0

direction W

0 1 0 1 0 0 0

2 3 1 2 0 0 0

0 2 3 2 0 0 0

0 0 1 1 0 0 0

0 0 0 0 0 0 0

>>> "

I haven not had any problems understanding material in this class, it's just sitting down and writing the program that's the problem. I just don't know where to start! Any help would be greatly appreciated! :)

Last edited: