From fb0b052f476665a5378dc85348fe987f50267cc7 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Fri, 3 Dec 2021 08:11:35 +0000 Subject: ch-2.pl solution at last --- challenge-141/james-smith/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-141/james-smith/perl/ch-2.pl diff --git a/challenge-141/james-smith/perl/ch-2.pl b/challenge-141/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..5e4af7babb --- /dev/null +++ b/challenge-141/james-smith/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ [1234,2], 9 ], + [ [ 768,4], 3 ], +); + +is( like_numbers(@{$_->[0]}), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub like_numbers { + scalar grep { !($_%$_[1]) } get_nums( $_[0] ); +} + +sub get_nums { + my @nums = split //, my $m = shift; + return map { my $n=$_<<1; join '',grep{($n>>=1)&1} @nums } 1..(1<<@nums)-2; +} + -- cgit