diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-05 16:25:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-05 16:25:54 +0100 |
| commit | b5238a291beb4e9079964591337f2dd606d6c3de (patch) | |
| tree | a290c4eb5863b78dfab8eb75af0f58d0fea4513f /challenge-107 | |
| parent | 174e32ff17cddabf5bf94483a07c0b774393a288 (diff) | |
| parent | a3af256668c8a6d723e5596ec1f7c6d15c87904f (diff) | |
| download | perlweeklychallenge-club-b5238a291beb4e9079964591337f2dd606d6c3de.tar.gz perlweeklychallenge-club-b5238a291beb4e9079964591337f2dd606d6c3de.tar.bz2 perlweeklychallenge-club-b5238a291beb4e9079964591337f2dd606d6c3de.zip | |
Merge pull request #3834 from drbaggy/master
Added some comments...
Diffstat (limited to 'challenge-107')
| -rw-r--r-- | challenge-107/james-smith/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-107/james-smith/perl/ch-2.pl | 4 |
2 files changed, 31 insertions, 3 deletions
diff --git a/challenge-107/james-smith/perl/ch-1.pl b/challenge-107/james-smith/perl/ch-1.pl index ea513d6508..753fe48f64 100644 --- a/challenge-107/james-smith/perl/ch-1.pl +++ b/challenge-107/james-smith/perl/ch-1.pl @@ -6,12 +6,36 @@ use warnings; use feature qw(say); use Test::More; -## Generate the list of self descritpive numbers for base -## < 7 - these are the ones that don't! follow the rules.. +## Generating self descriptive numbers can be split between +## the case where n >= 7 and n < 7... +## As we are asked for the first 3 these all exist in the case +## where n<7.... +## +## To get the next we can write them (in base "n") as ## All other numbers can be generated by the rule -## n-4, 2, 1, 0 (n-7 times), 1, 0, 0, 0 +## "n-4", "2", "1", "0" (n-7 times), "1", "0", "0", "0" +## +## We loop through numbers starting 1 to see if they are self +## descriptive. +## To get the desciption of a number of length k, we loop from +## 0..(k-1) counting the number of each digit in the string... +## We can do this with a harcoded series of tr///s but in if +## we want something dynamic we can write this as +## +## @Q = m{($_)}g; $count = scalar @Q; +## +## we can do this in one line by using @{[ ]} to convert to the +## list of matches to an array { subtle distinction here between +## lists and arrays! } and counting it by getting the scalar of +## the array... +## +## The description is got by stitching those counts together... +## We can do this in the one-line join q(), map below.. +## We just store it the list if the description and the number are +## the same.... my ($c,@res) = 0; + while( ++$c && @res<3 ) { push @res, $c if $c == join q(), map { scalar @{[ $c=~m{($_)}g ]} } 0 .. -1 + length $c; diff --git a/challenge-107/james-smith/perl/ch-2.pl b/challenge-107/james-smith/perl/ch-2.pl index b9aa2cf69e..7895f22840 100644 --- a/challenge-107/james-smith/perl/ch-2.pl +++ b/challenge-107/james-smith/perl/ch-2.pl @@ -8,5 +8,9 @@ use feature qw(say); use lib '.'; use Calc; +## %{class}:: contains a list of the methods for the class, +## so we can just dump these (to make it deterministic) we +## sort first.... + say join "\n", sort keys %Calc::; |
