Skip to content
Open
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
2 changes: 1 addition & 1 deletion bin/lint
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ for exercise in "${exercises[@]}"; do
done
done

shellcheck "${files[@]}"
shellcheck "$@" "${files[@]}"
5 changes: 3 additions & 2 deletions exercises/practice/bottle-song/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ main() {
}

verse() {
local cur=$(bottles "$1")
local next=$(bottles "$(( $1 - 1 ))")
local cur next
cur=$(bottles "$1")
next=$(bottles "$(( $1 - 1 ))")
printf "%s hanging on the wall,\n" "${cur^}"
printf "%s hanging on the wall,\n" "${cur^}"
printf "And if one green bottle should accidentally fall,\n"
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/hamming/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ die() {
exit 1
}

if [ "$#" -ne 2 ]; then
if (( $# != 2 )); then
die "Usage: hamming.sh <string1> <string2>"
fi

Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/isbn-verifier/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ done <<< "$number"
(( sum += last )) # process the check digit

# true if divisible by 11
[[ $(( sum % 11 )) -eq 0 ]] && printf "%s\n" "true" && exit 0
(( sum % 11 == 0 )) && printf "%s\n" "true" && exit 0

printf "%s\n" "false"
6 changes: 4 additions & 2 deletions exercises/practice/perfect-numbers/.meta/example.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

declare -i num sum

aliquot_sum() {
local -i num=$1 sum

Expand All @@ -26,14 +28,14 @@ aliquot_sum() {
echo $sum
}

declare -i num=$1
num=$1

if ((num <= 0)); then
echo "Classification is only possible for positive integers." >&2
exit 1
fi

declare -i sum=$(aliquot_sum $num)
sum=$(aliquot_sum "$num")

if ((sum < num)); then echo deficient
elif ((sum > num)); then echo abundant
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/phone-number/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ usage() {
# echo "<phone-number>: [1]NXX-NXX-XXXX : N=[2-9], X=[0-9]"
}

if [ "$#" -ne 1 ]; then
if (( "$#" != 1 )); then
usage
exit 1
fi
Expand All @@ -24,4 +24,4 @@ if [[ ! $result =~ $correct_pattern ]]; then
fi

# Strip off leading 1 country code if exists
echo ${result: -10}
echo "${result: -10}"
4 changes: 2 additions & 2 deletions exercises/practice/prime-factors/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ p=2

while (( p * p <= n )); do
if (( n % p == 0 )); then
factors+=( $p )
factors+=( "$p" )
(( n /= p ))
else
(( p += 1 ))
fi
done

(( n > 1 )) && factors+=( $n )
(( n > 1 )) && factors+=( "$n" )

echo "${factors[*]}"
2 changes: 1 addition & 1 deletion exercises/practice/protein-translation/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ while [[ -n $codons ]]; do
exit 1
fi
[[ $protein == STOP ]] && break
proteins+=($protein)
proteins+=("$protein")
codons=${codons#???}
done

Expand Down
5 changes: 3 additions & 2 deletions exercises/practice/queen-attack/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ main() {
validate black $b_row $b_col
(( w_row == b_row && w_col == b_col )) && die "cannot occupy same position"

local -i d_row=$( abs $(( w_row - b_row )) )
local -i d_col=$( abs $(( w_col - b_col )) )
local -i d_row d_col
d_row=$( abs $(( w_row - b_row )) )
d_col=$( abs $(( w_col - b_col )) )

if (( d_row == 0 || d_col == 0 || d_row == d_col ))
then echo true
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/rail-fence-cipher/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ main() {
[[ -z $func ]] && die "specify -e or -d"
(( rails <= 0 )) && die "must have positive number of rails"
shift $((OPTIND - 1))
$func $rails "$1"
"$func" "$rails" "$1"
}

encode() {
Expand Down
14 changes: 7 additions & 7 deletions exercises/practice/raindrops/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ usage() {
}

# check argument is present
if [ ! "$#" -eq 1 ]; then
if (( $# != 1 )); then
usage
fi

Expand All @@ -17,20 +17,20 @@ fi

output=""

if [ $(($1 % 3)) -eq 0 ]; then
if (( $1 % 3 == 0 )); then
output="Pling"
fi

if [ $(($1 % 5)) -eq 0 ]; then
if (( $1 % 5 == 0 )); then
output=$output"Plang"
fi

if [ $(($1 % 7)) -eq 0 ]; then
if (( $1 % 7 == 0 )); then
output=$output"Plong"
fi

if [ -z $output ]; then
echo $1
if [[ -z $output ]]; then
echo "$1"
else
echo $output
echo "$output"
fi
7 changes: 3 additions & 4 deletions exercises/practice/rna-transcription/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ trans[C]='G'
trans[A]='U'
trans[T]='A'

for i in $(seq 1 ${#1}); do
for (( i = 1; i <= ${#1}; i++ )); do
nt="${1:$i-1:1}"
comp_nt="${trans[$nt]}"

if [ -z "$comp_nt" ]; then
if [[ -z "$comp_nt" ]]; then
echo 'Invalid nucleotide detected.'
exit 1
fi

complement="$complement$comp_nt"
done

echo $complement

echo "$complement"
4 changes: 2 additions & 2 deletions exercises/practice/sieve/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ done

primes=()
for ((p = 2; p <= limit; p++)); do
if ${is_prime[p]}; then
primes+=( $p )
if "${is_prime[p]}"; then
primes+=( "$p" )
fi
done

Expand Down
14 changes: 7 additions & 7 deletions exercises/practice/triangle/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ assert() {
# Takes a numerical inequality in a string.
# Pipes it through to bc
# Float-proof numeric comparison :
[[ $( echo $1 | bc -l ) -eq 1 ]]
(( "$( bc -l <<< "$1" )" == 1 ))
}

output() {
exit_code=$1

if [ $exit_code -eq 0 ]; then
if (( exit_code == 0 )); then
echo "true"
else
echo "false"
Expand All @@ -43,7 +43,7 @@ valid_triangle() {
return 0
}

if ! valid_triangle $s1 $s2 $s3; then
if ! valid_triangle "$s1" "$s2" "$s3"; then
# Sides do not meet triangle inequality requirement
# Given a <= b <= c and a, b, c != 0, a + b >= c
output 1
Expand All @@ -52,19 +52,19 @@ fi
equilateral() {
assert "$1 == $2" && assert "$1 == $3"

output $?
output "$?"
}

isosceles() {
assert "$1 == $2" || assert "$1 == $3" || assert "$2 == $3"

output $?
output "$?"
}

scalene() {
assert "$1 != $2" && assert "$1 != $3" && assert "$2 != $3"

output $?
output "$?"
}

# Bash Ternary Operator:
Expand All @@ -73,4 +73,4 @@ scalene() {
# If boolean clause is false, it doesn't evaluate the other side of &&
# If first thing is false, it evaluates the item after ||,
# but if first two are true, doesn't bother evaluating last part.
$triangle_type $s1 $s2 $s3 && exit 0 || exit 1
"$triangle_type" "$s1" "$s2" "$s3" && exit 0 || exit 1