Box the compass: Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
Line 3,189:
32 354.37° North by west 北微西
1 354.38° North 北 </pre>
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
# Input: the input heading given as a number in degrees.
# Output: the corresponding integer index (from 0 to 31 inclusive)
# into the table, compassPoint, of compass point names.
def cpx:
(((. / 11.25) + 0.5)|floor % 32)
| if . < 0 then . + 32
else .
end;
 
# Compass point names
def compassPoint: [
"North",
"North by east",
"North-northeast",
"Northeast by north",
"Northeast",
"Northeast by east",
"East-northeast",
"East by north",
"East",
"East by south",
"East-southeast",
"Southeast by east",
"Southeast",
"Southeast by south",
"South-southeast",
"South by east",
"South",
"South by west",
"South-southwest",
"Southwest by south",
"Southwest",
"Southwest by west",
"West-southwest",
"West by south",
"West",
"West by north",
"West-northwest",
"Northwest by west",
"Northwest",
"Northwest by north",
"North-northwest",
"North by west"
];
 
### The task
# Input: heading in degrees
def degreesToCompassPoint:
compassPoint[cpx];
 
def r: [
0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5,
84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75,
185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0,
286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38
];
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
"Index Compass point Heading",
(r as $r
| range(0; $r|length)
| $r[.] as $h
| ((.%32) + 1) as $index # index as per requirements
| ($h | degreesToCompassPoint) as $d
| "\($index|lpad(3)) \($d|lpad(20)) \($h)°"
)
</syntaxhighlight>
 
{{output}}
<pre>
Index Compass point Heading
1 North 0°
2 North by east 16.87°
3 North-northeast 16.88°
4 Northeast by north 33.75°
5 Northeast 50.62°
6 Northeast by east 50.63°
7 East-northeast 67.5°
8 East by north 84.37°
9 East 84.38°
10 East by south 101.25°
11 East-southeast 118.12°
12 Southeast by east 118.13°
13 Southeast 135°
14 Southeast by south 151.87°
15 South-southeast 151.88°
16 South by east 168.75°
17 South 185.62°
18 South by west 185.63°
19 South-southwest 202.5°
20 Southwest by south 219.37°
21 Southwest 219.38°
22 Southwest by west 236.25°
23 West-southwest 253.12°
24 West by south 253.13°
25 West 270°
26 West by north 286.87°
27 West-northwest 286.88°
28 Northwest by west 303.75°
29 Northwest 320.62°
30 Northwest by north 320.63°
31 North-northwest 337.5°
32 North by west 354.37°
1 North 354.38°
</pre>
 
=={{header|Julia}}==
{{works with|Julia|1.2}}
Line 3,216 ⟶ 3,329:
@printf("%2i %-17s %10.2f°\n", i % 32 + 1, degree2compasspoint(d), d)
end</syntaxhighlight>
 
{{out}}
<pre> 1 North 0.00°
Line 3,251 ⟶ 3,363:
32 North By West 354.37°
1 North 354.38°</pre>
 
=={{header|K}}==
The representation of the names was inspired by Tcl (etc.).
2,464

edits