I can code that FizzBuzz function with only two tests (boston.conman.org) 2 points by plastic_teeth 6y ago ↗ HN
[–] rethab 6y ago ↗ Or with AWK: echo 15 | awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} END{print s}' [–] eesmith 6y ago ↗ Not that it affects your overall point, but you probably want: awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} {print s;s=""}' Otherwise, consider: % printf "3\n5\n9\n15\n16\n" | awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} END{print s}' FizzBuzzFizzFizzBuzz For bonus points, the following won't treat non-numerics as FizzBuzz: awk '$1+0\!=$1 {print"";next} $1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} {print s;s=""}'
[–] eesmith 6y ago ↗ Not that it affects your overall point, but you probably want: awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} {print s;s=""}' Otherwise, consider: % printf "3\n5\n9\n15\n16\n" | awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} END{print s}' FizzBuzzFizzFizzBuzz For bonus points, the following won't treat non-numerics as FizzBuzz: awk '$1+0\!=$1 {print"";next} $1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} {print s;s=""}'
3 comments
[ 2.3 ms ] story [ 15.4 ms ] thread