Write your code: The Drunkard’s Walk#

Functions definition#

Step simulator#

Write a function that works as a single step simulator.

  1. Function returns a string value between forward, left and right.

  2. Step is not “fair”:

    • 50% forward

    • 25% left

    • 25% right

  3. You can use any random function from the random module.

from random import # add function

def step_simulator():
    
    # write your code here
  File "/tmp/ipykernel_2355/1833401410.py", line 1
    from random import # add function
                                     ^
SyntaxError: invalid syntax

Walk simulator#

Write another function (walk simulator) that calls the single step simulator several times to simulate an entire Sailor’s trajectory, which can end with the sailor falling into the water or reaching the ship

  1. Function returns a string value between water and ship.

  2. You should call the step_simulator() function.

Variables:

  • Bridge Length (x direction) = 70 m

  • Bridge Width (y direction) = 14 m

  • Sailor’s Step length: 70 cm

  • Sailor’s starting x position: 0

  • Sailor’s starting y position: Width/2

  • open y boundaries (the sailor may fall off the bridge)

def walk_simulator():
    
    # Declare variables here
    
    # write your code here: remember to call the step_simulator() function

Probability calculator#

Finally, write a probability function that calls the walk simulator several times to determine the approximate probability that the sailor will arrive on the ship.

  1. Function returns a float value between 0 and 1.

  2. Avoid using any knowledge of combinatorics to solve this problem. Instead, take advantage of the fact that the machine can simulate millions of steps/walks in a very short time.

Since this is based on random draws, the probability will be slightly different each time the code is run.

def get_probability():

    # write your code here: remember to call the walk_simulator() function

Testing#

Check if your function returns the expected value using the cell below.

import unittest

class UnitTests(unittest.TestCase):

    def setUp(self):
        self.N = 1000
        self.step = step_simulator()
        self.steps = [step_simulator() for _ in range(self.N)]

        self.walk = walk_simulator()
        self.walks = [walk_simulator() for _ in range(100)]


    def test_step_type(self):
        self.assertTrue(isinstance(self.step, str), 'The function should return a string')
    def test_step_value(self):
        self.assertTrue(sorted(set(self.steps)) == ['forward','left','right'], 'The function should return `forward`,`left` or `right`.')
    def test_step_forward(self):
        counts = self.steps.count('forward')/self.N
        self.assertAlmostEqual(counts, 0.5, places=1, msg=f'The drunk should have 0.50 chance to step forward.')
    def test_step_left(self):
        counts = self.steps.count('left')/self.N
        self.assertAlmostEqual(counts, 0.25, places=1, msg=f'The drunk should have 0.25 chance to step left.')
    def test_step_right(self):
        counts = self.steps.count('right')/self.N
        self.assertAlmostEqual(counts, 0.25, places=1, msg=f'The drunk should have 0.25 chance to step right.')

    def test_walk_type(self):
        self.assertTrue(isinstance(self.walk, str), 'The function should return a string')
    def test_walk_value(self):
        self.assertTrue(sorted(set(self.walks)) == ['ship', 'water'], 'The function should return `ship` or `water`.')

    def test_probability_ship(self):
        self.assertAlmostEqual(get_probability(), 0.4, places=1, msg='The function should return around 0.4 of success.')          


unittest.main(argv=[''], verbosity=2,exit=False)