Box the compass: Difference between revisions

→‎{{header|UNIX Shell}}: sed-based capitalize() doesn't work on BSD/OS X, which also doesn't have bash 4 and its ${param^}.
(→‎{{header|Nim}}: Solution that matches the task)
(→‎{{header|UNIX Shell}}: sed-based capitalize() doesn't work on BSD/OS X, which also doesn't have bash 4 and its ${param^}.)
Line 6,542:
{{trans|Logo}}
 
Requires the standard POSIX bc(1) andcommand sed(1)to commandsperform tofloating-point functionarithmetic.
 
<lang sh># List of abbreviated compass point labels
Line 6,557:
320.62 320.63 337.50 354.37 354.38 )
 
function capitalize() {
 
printf '%s%s\n' "$(tr a-z A-Z <<<"${1:0:1}")" "${1:1}"
# capitalize a string
function capitalize {
echo "$1" | sed 's/^./\U&/'
}
 
# convert compass point abbreviation to full text of label
function expand_point {
local label="$1"
set -- N north E east S south W west b " by "
while (( $# )); do
label="${label//$1/$2}"
shift 2
done
Line 6,581 ⟶ 6,579:
# convert a compass angle from degrees into a box index (1..32)
function compass_point {
# use bc or dc depending on what's on the system
#amod $(dc <<<"$1 5.625 + 11.25 / 1 + p") 32
amod $(bc <<<"($1 + 5.625) / 11.25 + 1") 32
1,480

edits