1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

CSC131 Python Help

  1. Sep 1, 2012 #1
    "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.
     
  2. jcsd
  3. Sep 2, 2012 #2
    You should start by thinking of an algorithm. Really, the algorithm is in the paragraph, you just have to extract the important information (NOTE: I don't know much about Python, so I used C++; I would not, however, turn in the code I wrote below, as it was written in a few minutes):

    There are 11 squares (array of 11 ints, but could be a size other than 11). Also, initial step count is 0. Initialize randomNumber and direction as well, since they will be used in a loop.

    Code (Text):
    const int ARRAY_SIZE = 11;
    int squareArray[ARRAY_SIZE];
    int stepsCounter = 0;
    int randomNumber;
    string direction;
    Starting location is 5. Set the corresponding array value to 1 (remember that the index of an array starts at 0). All others are set to 0.

    Code (Text):
    int arrayLocation = 5;
    squareArray[arrayLocation-1] = 1;
    A random number of 0 or 1 is generated. If 1, move right; if 0, move left (ONLY move if arrayLocation is valid; if it is not, generate a random number again).

    Code (Text):
    randomNumber = rand()%2;
    if(randomNumber == 1)
    {
      if((arrayLocation + 1) < (ARRAY_SIZE))
      {
        direction = "Right";
        arrayLocation++;
      }
      else
      {
        randomNumber = rand()%2;
      }
    }
    else
    {
      if((arrayLocation - 1) >= 0)
      {
        direction = "Left";
        arrayLocation--;
      }
      else
      {
        randomNumber = rand()%2;
      }
    }
    For each valid step, increment the array value at arrayLocation by 1. Also increment stepsCounter by 1. Output the direction, arrayLocation, and squareArray.

    Code (Text):
    stepsCounter++;
    cout << "Moving " << direction << " into location " << arrayLocation+1 << endl;
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
      cout << " " << squareArray[i];
    }
    cout << endl;
     
    Repeat until you end on the last square squareArray[ARRAY_SIZE-1].

    Code (Text):
    while(arrayLocation < (ARRAY_SIZE-1))
    {
    ...
    }
    Once you reach the last square, display how many steps it took.

    Code (Text):
    cout << "Number of moves is " << stepsCounter << endl;
    Hope this helps!
     
    Last edited: Sep 2, 2012
  4. Sep 2, 2012 #3
    Python can be much simpler than the above... let me put something here (when I figure out how to add code to the messages in this forum)
     
    Last edited: Sep 2, 2012
  5. Sep 2, 2012 #4
    Here you go:

    Works in Python 2.7

    Code (Text):
    import random

    times = [0] * 11
    loc = 5
    times[5] = 1
    steps = 0

    while 0 <= loc <= 10:
        step = random.randint(0,1)
        if step == 0:
            loc -= 1
            print 'Moving left to location', loc
        else:
            loc += 1
            print 'Moving right to location', loc
        times[loc] = times[loc] + 1
        steps += 1
        print times
    print 'That took ', steps, ' steps.'
     
  6. Sep 3, 2012 #5
    Slightly altered to cover the event where you step off the right (position 11) and the code produces and error (index out of range) or off the left and the code wraps around to the right in the list

    Should work fine now.

    Code (Text):
    import random

    times = [0] * 11
    loc = 5
    times[5] = 1
    track_steps = 0

    while 0 <= loc <= 10:
        step = random.randint(0,1)
        track_steps += 1
        if step == 0:
            loc -= 1
            print 'Moving left to location', loc
            if loc < 0:
                break
        else:
            loc += 1
            print 'Moving right to location', loc
            if loc > 10:
                break
        times[loc] = times[loc] + 1
        print times
    print 'That took ', track_steps, ' steps.'
     
    Last edited: Sep 3, 2012
  7. Sep 3, 2012 #6
    Thank you. My next homework assignment is somewhat similar and that should definitely help me figure it out.
     
  8. Sep 3, 2012 #7
    You're welcome. I'm new to this language myself but it seems to be a great language to learn programming with.

    Good luck on your future assignments.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: CSC131 Python Help
  1. Python Help in Plotting (Replies: 14)

Loading...