DEV Community

Perl Weekly Challenge week #1 challenge #2: FizzBuzz

Juan Julián Merelo Guervós on March 29, 2019

Here goes the second first-week challenge. Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. However, any numbe...
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

Ah.. Right. Thanks!