Given a string of N, S, E and W, output a bearing (angle clockwise from North in degrees), correct to 5 decimal places.
In traditional compass notation, a string is made up of only 2 of these characters (like NNW or ESE). Here you must also accept strings that contain all 4 (like WNNNSE). Using only 2 symbols allows humans to intuitively understand the meaning. Allowing 4 symbols makes it horrible to read, but allows shorter ways of describing a bearing to a given accuracy.
(As pointed out in the comments by user2357112, it turns out you can prove that for any given bearing, the 4 symbol string will be exactly the same length as the 2 symbol string, so I've based this challenge on a false assumption. Hopefully this lack of a practical purpose doesn't detract from your enjoyment of the challenge...)
The exact method is described below, and is equivalent to the traditional notation (it expands on it rather than changing it).
Input
- The input is a single string containing only the characters
NESW
. - The input may be a sequence of characters if you prefer, provided this does not include any preprocessing. For example, taking a nested list
[N, [E, [S, [W]]]]
to help with the order of processing is not permitted. - Taking different characters is not permitted. You may not take a string of
1234
instead ofNESW
.
Output
- The output must be a decimal number or string representation of one (not a rational/fraction).
- Trailing zeros do not need to be displayed. If the bearing is
9.00000
, then the output9
also counts as correct to 5 decimal places. - The output is in the range [0, 360). That is, including 0 but excluding 360.
- Correctness is checked by rounding the output to 5 decimal places. If the bearing is 0.000005, this rounds to 0.00001. Outputs 0.00001 and 0.000005 are both correct.
- Output in scientific notation for some inputs is acceptable. For example,
1e-5
instead of0.00001
.
Conversion
- The single character compass points
N
,E
,S
, andW
correspond to 0, 90, 180, and 270 degrees respectively. - Prepending one of these to a string results in the bearing that bisects the bearing of the single character and the bearing of the original string.
- The closest of the two possible bisecting bearings is chosen, so that NE represents 45 degrees, not 225 degrees.
- This is unambiguous except where the angle to be bisected is 180 degrees. Therefore
NS
,SN
,WE
, andEW
correspond to undefined bearings, and the input will never end in any of these. They may however appear anywhere else in the input string, as this causes no ambiguity. - If the final two characters are identical, the final character will be redundant as the bisection will return the same bearing. Since this adds nothing to the notation, your code does not need to handle this. Therefore
NN
,EE
,SS
, andWW
correspond to undefined bearings, and the input will never end in any of these. They may however appear anywhere else in the input string.
Examples
N: 0
E: 90
S: 180
SE: halfway between S and E: 135
NSE: halfway between N and SE: 67.5
NNSE: halfway between N and NSE: 33.75
NNNSE: halfway between N and NNSE: 16.875
NNNNSE: halfway between N and NNNSE: 8.4375
Test cases
A submission is only valid if it gives correct output for all of the test cases. Note that the test cases push to the limits of what can be handled with double precision. For languages that default to single precision, you will probably need to spend the bytes to specify double precision in order to get correct outputs.
Test case outputs are shown rounded to 5 decimal places, and also to arbitrary precision. Both are valid outputs.
WNE 337.5 337.5
WEN 337.5 337.5
WEWEWEWEWEWEWEWEWEWEWEN 330.00001 330.000007152557373046875
NESWNESWNESWNESWNESWNESWNESW 90 89.99999932944774627685546875
NNNNNNNNNNNNNNNNNNNNNNNE 0.00001 0.0000107288360595703125
NNNNNNNNNNNNNNNNNNNNNNNW 359.99999 359.9999892711639404296875
SNNNNNNNNNNNNNNNNNNNNNNNE 90.00001 90.00000536441802978515625
SNNNNNNNNNNNNNNNNNNNNNNNW 269.99999 269.99999463558197021484375
Scoring
This is code-golf. The score is the length of the source code in bytes, and the shortest wins.
Pedantry
I made the mistake of thinking that "North by North West" was a valid compass direction. A happy mistake, since it led to a challenge idea, but I then discovered from the Wikipedia page:
"The title of the Alfred Hitchcock 1959 movie, North by Northwest, is actually not a direction point on the 32-wind compass, but the film contains a reference to Northwest Airlines."
It also turns out that the method used for this challenge is only consistent with traditional compass points up to and including the 16 point compass. The 32-wind compass described on that page is subtly different and I have conveniently overlooked its existence for this challenge.
Finally, for anyone who thinks I should use "Southeast" instead of "South East", it seems to be a regional difference.
WNNNSE
<= what would be the output for this example entry at start of your post ? it sounds invalid for me, but it's hard to tell. \$\endgroup\$WNNNSE
the output would be323.4375
. See the example section for a walkthrough that would apply in the same way to this case. \$\endgroup\$f(N,N,N,S,E)
okay? \$\endgroup\$