Bash Week β FizzBuzz but Cursed (PlingPlangPlong Edition)
This is an advanced FizzBuzz-style exercise, adapted for Bash with O(1) performance. No loops. No Python crutches. Just raw shell logic.
Description:
For a given input, if it's divisible by:
- 3 β output "Pling"
- 5 β output "Plang"
- 7 β output "Plong"
If none of the above, print the number itself.
Initial logic:
This simple program checks if the input number is equal to a modulo of either 3, 5 or 7. This operation however does not take the case where there's several true cases.
#!/usr/bin/env bash
if [ $(("$1" % 3)) -eq 0 ]; then
echo "Pling"
elif [ $(("$1" % 5)) -eq 0 ]; then
echo "Plang"
elif [ $(("$1" % 7)) -eq 0 ]; then
echo "Plong"
else
echo "$1"
fi
New Version:
#!/usr/bin/env bash
sound=""
(($1 % 3 == 0)) && sound+="Pling"
(($1 % 5 == 0)) && sound+="Plang"
(($1 % 7 == 0)) && sound+="Plong"
echo "${sound:-$1}
Notes:
- Uses string concatenation to combine results from multiple modulo checks.
- Uses Bash parameter expansion ${sound:-$1} to fallback to the number if sound is empty.
echo "${sound: -$1}"
-
Itβs Bash's way of saying: βIf sound is unset or null, use $1 instead.β
-
Itβs lazy evaluation like Pythonβs x if x else y, but uglier and more prone to being misread after midnight.
-
C equivalent:
x ? x : y