Sinan Ünür wrote about some click bait that claimed Perl programmers were worse than programmers in a fictional language named Quorum. His post goes through all the experimental and analytic errors, as many of his posts do.

One of the tasks in this pseudoscientific research was described as:

This code will count the number of values that are and are not divisible by c and lie between a and b. It then compares the number of values that are and are not divisible by c and makes the greater of them available to the user.

The solutions the participants submitted in the seven minutes they were allotted were fine. I’m not too concerned about that. People who don’t know a language muddle their way through, and we have a way to deal with that. We create an interface and hide the details behind a subroutine.

David Farrell (publisher of PerlTricks and organizer of Mini-Conf) and I were talking about this challenge. How would be do it? When I gave it a whack, I came up with a not-so-great solution my self on my first go, but it worked. If it was important, I could go back and redo it. Too often we show the result which is much less illuminating than the path to the result.

I’ll show my tries after next week, but I’m going to give my readers a chance to come up with their own solutions first. But, I’m going to do it with these honor-system rules:

Complete the task in five minutes. Your first idea is more important than the best idea. Save that program.

Stop working, but think about it for a day. Come back the next day and start over with what you thought about. Do it in five minutes this time too.

Post both solutions. You can do that here, or in some other blog. Explain what you were thinking on the first go, what you thought about away from the keyboard, and why you went with the new code. I’m interested in how you think more than the best solution more than the solution.

For my solutions, I’ll apply my normal constraints. I’ll only use what I’ve presented in Learning Perl.

Here’s a stub test file to help you. Even though we don’t cover testing until Intermediate Perl, that’s not a necessary part of this challenge. You only have to fill in the body for the count subroutine. In this stub I use a couple of v5.20 features: subroutine signatures and postfix dereferencing.

use v5.20; use feature qw(postderef signatures); no warnings qw(experimental::postderef experimental::signatures); use Test::More; my @tests = ( # start end divisor answer [ 0, 10, 3, 7 ], [ 0, 20, 3, 14 ], [ 5, 100, 37, 94 ], [ -9, 9, 4, 14 ], ); foreach my $row ( @tests ) { is( count( $row->@[0..2] ), $row->[-1] ); } done_testing(); sub count ( $start, $end, $divisor ) { ... # fill in here }

If you don’t like the new features, you can use this stub instead:

use Test::More; my @tests = ( # start end divisor answer [ 0, 10, 3, 7 ], [ 0, 20, 3, 14 ], [ 5, 100, 37, 94 ], [ -9, 9, 4, 14 ], ); foreach my $row ( @tests ) { is( count( @{$row}[0..2] ), $row->[-1] ); } done_testing(); sub count { ... # fill in here }