- #1
- 6
- 0
"A classic problem that can be solved using a list is a random walk. Imagine a drunken man standing on the center square of a sidewalk consisting of 11 squares. At each step, the drunk can elect to go either left or right. How long will it be until he reaches the end of the sidewalk, and how many times will he have stood on each square? Write a Python program that solves this problem and simulates the steps taken by the drunk. The simulation ends when the drunken man moves out of the sidewalk. To help you with the solution, you can represent the sidewalk as a list. Store the number of times the drunk has stood on a square as a value in the list. The list can be created and initialized to 0's with the following statement:
times = [0] * 11
The function random.randint(0,1) outputs 0 or 1 with equal probability. Move to the left if 0 is produced, and to the right if 1 is produced. Maintain a variable that indicates the current location of the drunk and another variable that maintains the number of moves. Display the direction, the location, and the list at each step of the simulation. A sample output is as follows:
Starting at location 5
| 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Moving right into location 6
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
Moving right into location 7
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
Moving left into location 6
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 1 | 0 | 0 | 0 |
Moving right into location 7
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 0 | 0 | 0 |
Moving right into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 | 0 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 | 1 | 0 |
Moving left into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 2 | 1 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 2 | 2 | 0 |
Moving left into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 2 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 3 | 0 |
Moving right into location 10
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 3 | 1 |
Moving right into location 11
number of moves is 11"
So, I've not had any problems understanding anything being taught in this class but I am having problems with how to implement things. This is an old lab assignment that I didnt finish. I'm not entirely sure how I would set this program up at all. I think if I could see how this is done I would have an easier time with the other homework assignment I have.
Any help would be appreciated.
times = [0] * 11
The function random.randint(0,1) outputs 0 or 1 with equal probability. Move to the left if 0 is produced, and to the right if 1 is produced. Maintain a variable that indicates the current location of the drunk and another variable that maintains the number of moves. Display the direction, the location, and the list at each step of the simulation. A sample output is as follows:
Starting at location 5
| 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Moving right into location 6
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
Moving right into location 7
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
Moving left into location 6
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 1 | 0 | 0 | 0 |
Moving right into location 7
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 0 | 0 | 0 |
Moving right into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 | 0 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 | 1 | 0 |
Moving left into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 2 | 1 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 2 | 2 | 0 |
Moving left into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 2 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 3 | 0 |
Moving right into location 10
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 3 | 1 |
Moving right into location 11
number of moves is 11"
So, I've not had any problems understanding anything being taught in this class but I am having problems with how to implement things. This is an old lab assignment that I didnt finish. I'm not entirely sure how I would set this program up at all. I think if I could see how this is done I would have an easier time with the other homework assignment I have.
Any help would be appreciated.