The random walker X74497


Statement
 

pdf   zip   main.py

html

A random walker is an agent that randomly decides which direction to go at each step.

We want a function walker(x, y, steps) that given the original coordinates (x,y) of the random walker, and a string with the direction to take at each step, computes the final coordinates of the walker.

The string steps contains only the characters ’N’, ’S’, ’E’, and ’W’. North (’N’) means moving up one position in the y axis, and South (’S’) moving down one position in y. Similarly, East (’E’) and West (’W’) correspond to moving along the x axis one position right or left, respectively.

For instance if the walker starts at position A (4,1) in the picture below, and the string with the steps taken is ’NEENWNWWS’, the walker will follow the path through positions marked with stars in the picture, and the final position will be B (3,3).

   |
 4-|        *--*--*  
   |        |     |
 3-|        B     *--*
   |                 |
 2-|           *--*--*
   |           |
 1-|           A
   | 
 0 +--|--|--|--|--|--|--
   0  1  2  3  4  5  6  

Important: Only the function is expected. If you submission includes a main program, it has to be either commented out, or under the condition     if __name__ == "__main__":

Sample session
>>> walker(4, 1, 'NEENWNWWS')
(3, 3)
>>> walker(3, 5, 'NESW')
(3, 5)
>>> walker(0, 4, 'SSSSEWEWNNNN')
(0, 4)
>>> walker(0, 0, 'SWSWNES')
(-1, -2)
>>> walker(6, 3, '')
(6, 3)
Information
Author
Language
English
Official solutions
Python
User solutions
Python