Live data from Hacker News

I can code that FizzBuzz function with only two tests

boston.conman.org

1–4 of 4 posts

Re: I can code that FizzBuzz function with only two tests

#3
post #2

Or with AWK: echo 15 | awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} END{print s}'

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=""}'