Question

Here's the problem I'm working on:

Assume that the developers of Myro are developing a new black box function called traveler(String str ) that moves a robot in a specific direction (i.e., North, South, East, or West) for a specific number of feet (e.g., 1 = one foot, 2 = two feet, etc.) The parameter of the traveler( ) function is a String variable that expresses both the direction and the distance. For example, traveler(E2) would move the robot East for two feet while traveler(N6) would move the robot North for six feet.

Write a short segment of code that uses the black box traveler( ) function to move the robot from Point A to Point B in the grid shown below.

  1. Assign the parameters (e.g. N2, etc.) to a data structure.
  2. Traverse the data structure with a loop that supplies the parameters to the traveler( ) function.

Path to follow

The traveler(String str) function does not really exist. Treat it as if it did and simply write a short segment of code to accomplish the task of moving the robot along the pattern given above.

Here's what I have:

    def main():
    go = [N2, W1, N3, E4, S1, W1, S1, E2]
    for g go:
        traveler(g)

main()

My question is how would finish this up?

Was it helpful?

Solution

traversing lists in python is generally done by using a for loop

Example from python documentation:

>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print w, len(w)
...
cat 3
window 6
defenestrate 12

In the above example len is a built in function. They are passing w to the len function. This is very simliar to what you need to do with the traveler function.
Loop through every item in go list. Pass that item to traveler

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top