diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-04-05 14:13:23 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-04-05 14:13:23 +0100 |
| commit | a3af256668c8a6d723e5596ec1f7c6d15c87904f (patch) | |
| tree | b7193da98aeee5a63759a4e4b96c3a027b35255e | |
| parent | c9e37ef67aeb0087844fc0bae50114732214039b (diff) | |
| download | perlweeklychallenge-club-a3af256668c8a6d723e5596ec1f7c6d15c87904f.tar.gz perlweeklychallenge-club-a3af256668c8a6d723e5596ec1f7c6d15c87904f.tar.bz2 perlweeklychallenge-club-a3af256668c8a6d723e5596ec1f7c6d15c87904f.zip | |
Comments...
| -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::; |
