#C9220. Valid Walk
Valid Walk
Valid Walk
You are given a sequence of directions represented by lowercase letters: ( n ) (north), ( s ) (south), ( e ) (east) and ( w ) (west). Starting from the origin ( (0,0) ), follow these directions. Determine whether the walk ends at the origin. A valid walk must return to the origin.
For example, the walk n s e w
returns to the origin resulting in a valid walk, while n n s
does not.
Mathematically, if you denote the current position as ( (x, y) ), each move modifies the coordinates as follows:
( n: y = y+1 ),
( s: y = y-1 ),
( e: x = x+1 ),
( w: x = x-1 ).
Your task is to implement a program that reads the directions from the standard input and prints True
if the walk returns to the origin, or False
otherwise.
inputFormat
The input is provided via standard input as a single line containing zero or more space-separated directions. Each direction is one of: n
, s
, e
, or w
. If no directions are provided, the walk is considered empty and therefore valid.
outputFormat
Print to standard output a single line: True
if the sequence of moves brings you back to the origin; otherwise, print False
.## sample
n s e w
True