Search code examples
pythonstochastic-process

(Python) Regarding the following question, what's the difference between Option (A) & Option (C)?


I encountered this question while taking the online course. The correct answer is Option (C), however, why can not me choose Option (A)? What's the nuance between these two options?

---> Suppose we wanted to create a class PolarBearDrunk, a drunk polar bear who moves randomly along the x and y axes taking large steps when moving South, and small steps when moving North.

class PolarBearDrunk(Drunk):
    def takeStep(self):
        # code for takeStep()

Which of the following would be an appropriate implementation of takeStep()?

Option A)

directionList = [(0.0, 1.0), (1.0, 0.0), (-1.0, 0.0), (0.0, -1.0)]
myDirection = random.choice(directionList)
if myDirection[0] == 0.0:
    return myDirection + (0.0, -0.5)
return myDirection

Option B)

directionList = [(0.0, 0.5), (1.0, -0.5), (-1.0, -0.5), (0.0, -1.5)]
return random.choice(directionList)

Option C)

directionList = [(0.0, 0.5), (1.0, 0.0), (-1.0, 0.0), (0.0, -1.5)]
return random.choice(directionList)

Option D)

directionList = [(0.0, 1.0), (1.0, 0.0), (-1.0, 0.0), (0.0, -1.0), (0.0, -1.0)]
return random.choice(directionList)

Solution

  • + operator on tuples (like in option A) means concatenation:

    (0.0, 1.0) + (0.0, -0.5) == (0.0, 1.0, 0.0, -0.5)