Anonymous user
Box the compass: Difference between revisions
→{{header|Nim}}: Solution that matches the task
(Add Factor) |
(→{{header|Nim}}: Solution that matches the task) |
||
Line 4,316:
=={{header|Nim}}==
<lang nim>import
const
headingNames: array[1..32, string] = [
"
"
"
"
"
"
"
"Northwest", "Northwest by north", "North-northwest", "North by west"]
maxNameLength = headingNames.mapIt(it.len).max
degreesPerHeading = 360 / 32
func toCompassIndex(degree: float): 1..32 =
var degree = (degree + degreesPerHeading / 2).floorMod 360
int(degree / degreesPerHeading) + 1
func toCompassHeading(degree: float): string = headingNames[degree.toCompassIndex]
for i in 0..32:
let
else: 0)
index = heading.toCompassIndex
compassHeading = heading.toCompassHeading.alignLeft(maxNameLength)
echo fmt"{index:>2} {compassHeading} {heading:6.2f}"</lang>
Output:
<pre> 1 North 0.00
2 North by east 16.87
3 North-northeast 16.88
|