DEV Community

loading...

Discussion on: Perl Weekly Challenge week #1 challenge #2: FizzBuzz

Collapse
learnbyexample profile image
Sundeep

Here's a Perl5 one-liner:

$ perl -le 'print join " ", map { $_ % 15 ? $_ % 3 ? $_ % 5 ? $_ : "Fizz" : "Buzz" : "FizzBuzz" } (1..20)'
1 2 Buzz 4 Fizz Buzz 7 8 Buzz Fizz 11 Buzz 13 14 FizzBuzz 16 17 Buzz 19 Fizz

Note that your question states

any number divisible by 3 should be replaced by the word ‘fizz’

but the output shown has 3 and 5 interchanged

Collapse
jj profile image
Juan Julián Merelo Guervós Author

Ah.. Right. Thanks!