North North West
InterviewTime limit8sMemory limit512 MB
A direction string is built from copies of north and west; each prefix copy shifts the angle by 90/2^n, so compute the resulting angle as an integer or reduced fraction.
- Level
Medium4 of 10
- Topics
- Math, String, Implementation, Simulation
- Solved
- No attempts yet
Problem
Repeating the names of directions describes a finer direction. For example, northwest is the direction halfway between north and west, and northnorthwest is the direction halfway between north and northwest.
This problem fixes the directions between north and west by the following rules.
- "north" means 0 degrees.
- "west" means 90 degrees.
- If a direction string dir means degrees and the sum of the occurrences of "north" and "west" in dir is (), then "north"dir, the concatenation of "north" and dir, means degrees, and "west"dir means degrees.
Compute the angle in degrees that the given direction means.
Input
The input holds several datasets. The number of datasets does not exceed 100.
Each dataset is one line holding a string that denotes a direction. The string can be built by concatenating copies of "north" and "west", the sum of the occurrences of "north" and "west" in the string is between 1 and 20 inclusive, and the angle the string denotes is between 0 and 90 inclusive. A line holding only "#" follows the last dataset.
Output
For each dataset, print the angle that the direction means on its own line. Print an integer when the angle is an integer, and otherwise print the irreducible fraction p/q, where p and q are coprime positive integers and q is at least 2.