Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions exercises/practice/bowling/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ main() {
(( frame > 10 )) && die "Cannot roll after game is over"
(( pins < 0 )) && die "Negative roll is invalid"
(( pins > 10 )) && die "Pin count exceeds pins on the lane"
too_many $pins && die "Pin count exceeds pins on the lane"
add_score $pins
handle_frame $pins
too_many "$pins" && die "Pin count exceeds pins on the lane"
add_score "$pins"
handle_frame "$pins"
done
(( frame <= 10 )) && die "Score cannot be taken until the end of the game"
echo $score
echo "$score"
}

die() {
Expand All @@ -32,7 +32,7 @@ too_many() {
if (( ${#current_frame[@]} == 0 )); then
return 1
elif (( frame == 10 )); then
too_many_tenth $pins
too_many_tenth "$pins"
else
(( current_frame[0] + pins > 10 ))
fi
Expand All @@ -41,8 +41,8 @@ too_many() {
too_many_tenth() {
local pins=$1
local non_strikes=()
for roll in ${current_frame[@]}; do
(( roll < 10 )) && non_strikes+=( $roll )
for roll in "${current_frame[@]}"; do
(( roll < 10 )) && non_strikes+=( "$roll" )
done
local num=${#non_strikes[@]}
if (( num == 0 )) ||
Expand All @@ -57,7 +57,7 @@ too_many_tenth() {
add_score() {
local pins=$1
(( score += pins ))
for idx in ${!bonuses[@]}; do
for idx in "${!bonuses[@]}"; do
if (( bonuses[idx] > 0 )); then
(( score += pins ))
(( bonuses[idx] -= 1 ))
Expand All @@ -67,9 +67,9 @@ add_score() {

handle_frame() {
if (( frame == 10 )); then
handle_tenth_frame $1
handle_tenth_frame "$1"
else
handle_nth_frame $1
handle_nth_frame "$1"
fi
}

Expand All @@ -84,7 +84,7 @@ handle_nth_frame() {
(( frame += 1 ))
elif (( ${#current_frame[@]} == 0 )); then
# first ball of frame
current_frame+=( $pins )
current_frame+=( "$pins" )
else
# second ball of frame
if (( current_frame[0] + pins == 10 )); then
Expand All @@ -97,7 +97,7 @@ handle_nth_frame() {

handle_tenth_frame() {
local pins=$1
current_frame+=( $pins )
current_frame+=( "$pins" )
local num=${#current_frame[@]}
if (( num == 3 )) ||
(( num == 2 && $(sum current_frame) < 10 ))
Expand All @@ -110,8 +110,8 @@ sum() {
# sum the contents of an array
local -n ary=$1
local -i sum=0
for val in ${ary[@]}; do (( sum += val )); done
echo $sum
for val in "${ary[@]}"; do (( sum += val )); done
echo "$sum"
}

main "$@"
7 changes: 5 additions & 2 deletions exercises/practice/change/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

main() {
local -i amount=$1; shift
# shellcheck disable=SC2034 # passed by name
local -a denonimations=( "$@" )
# shellcheck disable=SC2034 # passed by name
local -a firstCoinForValue=()

if (( amount < 0 )); then
Expand Down Expand Up @@ -41,7 +43,7 @@ change() {
C=(0)
S=(0)
for ((i = 1; i <= amount; i++)); do
C+=( $max )
C+=( "$max" )
S+=( "" )
done

Expand All @@ -64,6 +66,7 @@ change() {
make_change() {
local -i amount=$1
local -n coins=$2
# shellcheck disable=SC2178 # $3 references an array, not a string
local -n S=$3
local -a change=()
local coin idx
Expand All @@ -76,7 +79,7 @@ make_change() {
while (( amount > 0 )); do
idx=${S[amount]}
coin=${coins[idx]}
change+=( $coin )
change+=( "$coin" )
(( amount -= coin ))
done

Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/clock/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ main() {
[+-])
(( $# == 4 )) || die "invalid arguments"
isnumeric "$4" || die "non-numeric argument"
minutes=$(( minutes $3 $4 ))
[[ $3 == "+" ]] && minutes=$(( minutes + $4 )) || minutes=$(( minutes - $4 ))
;;
"") : ;;
*) die "invalid arguments" ;;
Expand Down
13 changes: 6 additions & 7 deletions exercises/practice/collatz-conjecture/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ fi

STEP=0

while [ "$N" -ne "1" ]
do
if [ "$(($N % 2))" -eq "0" ]; then
N=$(($N / 2))
while (( N != 1 )); do
if (( N % 2 == 0 )); then
N=$(( N / 2 ))
else
N=$((($N * 3) + 1))
N=$(( N * 3 + 1))
fi
STEP=$(($STEP + 1))
(( ++STEP ))
done

echo $STEP
echo "$STEP"
1 change: 1 addition & 0 deletions exercises/practice/darts/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ shopt -s extglob

main() {
(( $# == 2 )) || die "wrong number of arguments"
# shellcheck disable=SC2015
isnumeric "$1" && isnumeric "$2" || die "arguments must be numeric"
score_throw "$@"
}
Expand Down
12 changes: 6 additions & 6 deletions exercises/practice/diamond/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ error () {
}

check_args () { # validate the arguments
[[ -z $1 || ${#1} -ne 1 || $# -ne 1 || $1 != ${1//[![:upper:]]/} ]] && error
[[ -z $1 || ${#1} -ne 1 || $# -ne 1 || $1 != "${1//[![:upper:]]/}" ]] && error
}

pad () {
Expand All @@ -20,9 +20,9 @@ pad () {

line_a () { # print the first or last line
padding=$(( line_width / 2 ))
pad $padding
pad "$padding"
printf 'A'
pad $padding
pad "$padding"
printf "\n" # get a new line
}

Expand All @@ -31,11 +31,11 @@ up_to_max () {
int_padding=1 ext_padding=$(( line_width / 2 - 1 ))
while [[ $level -le $max_level ]]; do
letter="${from_idx_map[$level]}"
pad $ext_padding # external
pad "$ext_padding" # external
printf "%s" "$letter"
pad $int_padding # internal
pad "$int_padding" # internal
printf "%s" "$letter"
pad $ext_padding # external
pad "$ext_padding" # external
printf "\n"
((level++)); ((ext_padding--))
int_padding=$(( int_padding + 2 ))
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/difference-of-squares/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sum_of_squares () {
for ((i=1; i<=n; i++)); do
squares_summed=$((squares_summed + i ** 2))
done
echo $squares_summed
echo "$squares_summed"
}

square_of_sum () {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/diffie-hellman/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

main() {
case $1 in
privateKey) rand 2 $2 ;;
privateKey) rand 2 "$2" ;;
publicKey|secret) "$@" ;;
*) echo "unknown subcommand" >&2; exit 1 ;;
esac
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/dnd-character/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ generate() {
for c in "${characteristics[@]}"; do
a=$(ability)
printf "%s %d\n" "$c" "$a"
[[ $c == constitution ]] && hp=$(( 10 + $(modifier $a) ))
[[ $c == constitution ]] && hp=$(( 10 + $(modifier "$a") ))
done
echo "hitpoints $hp"
}

ability() {
set -- $( { d6; d6; d6; d6; } | sort -nr )
echo $(( $1 + $2 + $3 ))
local -a dice
readarray -t dice < <( { d6; d6; d6; d6; } | sort -nr )
echo $(( dice[0] + dice[1] + dice[2] ))
}

# roll a 6-sided die
Expand Down
40 changes: 21 additions & 19 deletions exercises/practice/forth/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -f # disable path exansion
shopt -s extglob # enable extended patterns

# I'm going to use the stack and the macros as
# I'm going to use the stack and the macros as
# global variables: Not best practice.
declare -a stack # indexed array
declare -A macros # associative array
Expand Down Expand Up @@ -37,8 +37,8 @@ main() {
word=$1
shift

if is_number $word; then
stack::push $word
if is_number "$word"; then
stack::push "$word"
elif [[ -n ${macros[$word]} ]]; then
set -- ${macros[$word]} $*
else
Expand All @@ -50,11 +50,11 @@ main() {
;;
[-+*/])
# Arithmetic operation
binary_op $word
binary_op "$word"
;;
dup|drop|swap|over)
# Invoke this word as a command
$word
"$word"
;;
*)
echo "undefined operation" >&2
Expand All @@ -65,12 +65,12 @@ main() {
done
done

echo ${stack[*]}
echo "${stack[*]}"
}

record_macro() {
local macro_name=$1
if is_number $macro_name; then
if is_number "$macro_name"; then
die "illegal operation: cannot redefine number"
fi
shift
Expand Down Expand Up @@ -104,21 +104,21 @@ need() {
}

binary_op() {
local op=$1
local op=$1 a b

need 2
local b=$(stack::tail)
b=$(stack::tail)
stack::pop
local a=$(stack::tail)
a=$(stack::tail)
stack::pop

[[ $op == "/" ]] && (( b == 0 )) && die "divide by zero"
stack::push $(( a $op b ))
stack::push $(( a "$op" b ))
}

dup() {
need 1
stack::push $(stack::tail)
stack::push "$(stack::tail)"
}

drop() {
Expand All @@ -127,20 +127,22 @@ drop() {
}

swap() {
local a b
need 2
local b=$(stack::tail)
b=$(stack::tail)
stack::pop
local a=$(stack::tail)
a=$(stack::tail)
stack::pop
stack::push $b $a
stack::push "$b" "$a"
}

over() {
local a b
need 2
local b=$(stack::tail)
b=$(stack::tail)
stack::pop
local a=$(stack::tail)
stack::push $b $a
a=$(stack::tail)
stack::push "$b" "$a"
}

main
7 changes: 5 additions & 2 deletions exercises/practice/game-of-life/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ create_matrix() {
}

tick() {
# shellcheck disable=SC2034 # passed by name
local -A next_matrix
local -i row col countLiveCell

Expand All @@ -27,9 +28,11 @@ tick() {
countLiveCell=$(check_neighborhood "$col" "$row")

if (( countLiveCell == 3 || matrix[${col},${row}] == 1 && countLiveCell == 2 )); then
next_matrix[${col},${row}]=1
# shellcheck disable=SC2034 # passed by name
next_matrix["${col},${row}"]=1
else
next_matrix[${col},${row}]=0
# shellcheck disable=SC2034 # passed by name
next_matrix["${col},${row}"]=0
fi
done
done
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/grep/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ process_file() {
printf "%s\n" "$file"
break
fi
# shellcheck disable=SC2094
output "$num_files" "$file" "$lineno" "$line"
fi
done < "$file"
done < "$file" # shellcheck disable=SC2094
}

output() {
Expand Down
Loading