This one only took four hours to get to the first working version. The reason it took me so long is that I wanted to try to do it without doing the same conditional test more than once (also, I'm not good at this). It isn't very obfuscaturated:
#!/usr/bin/perl -w use strict; for my $num (1 .. 100) { my $mod3num = $num % 3; my $mod5num = $num % 5; my $result = ""; if ( $mod3num == 0 ) { $result .= "fizz"; } if ( $mod5num == 0 ) { $result .= "buzz"; } if ( $result =~ /z/ ) { print "[$num] $result\n"; } else { print "[$num] $num\n"; } }
Here's a different one I spent some of that four hours trying to do, then completed in 15 minutes the next day. It seems pretty annoying but I think it could be better:
#!/usr/bin/perl -w use strict; for my $n (1 .. 100) { my $nm3 = $n % 3; my $nm5 = $n % 5; print((!$nm3 && !$nm5) ? "[$n] fizzbuzz\n" : (!$nm5? "[$n] buzz\n" : (!$nm3? "[$n] fizz\n" : "[$n] $n\n"))) }
Questions:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Rate my fizzbuzz
by tybalt89 (Monsignor) on Oct 12, 2025 at 15:16 UTC | |
|
Re: Rate my fizzbuzz
by LanX (Saint) on Oct 12, 2025 at 13:37 UTC | |
|
Re: Rate my fizzbuzz
by ysth (Canon) on Oct 13, 2025 at 00:32 UTC | |
by LanX (Saint) on Oct 13, 2025 at 17:54 UTC | |
by ysth (Canon) on Oct 29, 2025 at 21:50 UTC | |
|
Re: Rate my fizzbuzz
by CountZero (Bishop) on Oct 16, 2025 at 12:49 UTC |