Last active
August 29, 2015 14:11
-
-
Save Genki-S/39759b6186d27a70a361 to your computer and use it in GitHub Desktop.
fizzbuzz bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
number_to_fizzbuzz() | |
{ | |
if [ $# -ne 1 ]; then | |
echo "Usage: $FUNCNAME NUMBER" | |
return 1 | |
fi | |
local output="" | |
local number=$1 | |
if [ $(($number % 3)) -eq 0 ]; then | |
output="${output}fizz" | |
fi | |
if [ $(($number % 5)) -eq 0 ]; then | |
output="${output}buzz" | |
fi | |
if [ -z "$output" ]; then | |
output="$number" | |
fi | |
echo $output | |
} | |
fizzbuzz() | |
{ | |
if [ $# -ne 2 ]; then | |
echo "Usage: $FUNCNAME START END" | |
return 1 | |
fi | |
local start=$1 | |
local end=$2 | |
for i in $(seq $start $end); do | |
number_to_fizzbuzz $i | |
done | |
} | |
fizzbuzz 1 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment