diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-12 08:53:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-12 08:53:35 +0100 |
| commit | eeb4cf0caad65ceee5fdddb161be04383ad53428 (patch) | |
| tree | 3d9efe6123cd1535f8c09b522816e5e990ff6166 /challenge-107 | |
| parent | f3bf9e7ddbab83a6725915f676f9bf9de35bc01f (diff) | |
| parent | 3e91905341fbdd353f6f83cf77a863a06ad8be16 (diff) | |
| download | perlweeklychallenge-club-eeb4cf0caad65ceee5fdddb161be04383ad53428.tar.gz perlweeklychallenge-club-eeb4cf0caad65ceee5fdddb161be04383ad53428.tar.bz2 perlweeklychallenge-club-eeb4cf0caad65ceee5fdddb161be04383ad53428.zip | |
Merge pull request #3874 from drbaggy/master
Doc changes...
Diffstat (limited to 'challenge-107')
| -rw-r--r-- | challenge-107/james-smith/README.md | 50 | ||||
| -rw-r--r-- | challenge-107/james-smith/perl/ch-1.pl | 8 |
2 files changed, 29 insertions, 29 deletions
diff --git a/challenge-107/james-smith/README.md b/challenge-107/james-smith/README.md index ad96ffed8f..7c34021274 100644 --- a/challenge-107/james-smith/README.md +++ b/challenge-107/james-smith/README.md @@ -5,23 +5,25 @@ To describe a number we write down the number of 0s, 1s, 2s, 3s, etc A self-descriptive number is one of length `n` such that in base `n` the description above is the number itself. -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.... +Generating self descriptive numbers can be split between the case where `n >= 7` and `n < 7`... +As we are asked for the first three these all exist in the case where `n < 7`.... + +For `n >= 7` the self-descriptive numbers are of the form: -For n >= 7 the self-descriptive numbers are of the form `n-4`, `21`, `0` {n-7 times}, `1000`; In our solution 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 if we want something dynamic we can write this as +We can do this with a harcoded series of `tr///s` but 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... +we can do this in one line by using `@{[ ]}` to convert to the list of matches to an arrayand counting it by getting +the scalar of the array... Often in Perl lists and arrays are interchangeable - but here is one of those subtle distinctions +you have to be aware of. -The description is obtained by stitching those counts together... We can do this in the one-line join q(), map below.. +The description is obtained 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.... ``` @@ -60,13 +62,13 @@ This means we can make the statement inside the loop a single statement and post say "@res"; ``` -Note we have to wrap the "condition" in brackets to force it to be evaluated before the && as o/w the line ends in -0 .. -1 + length( $x && push @res, $c); +Note we have to wrap the "condition" in brackets to force it to be evaluated before the `&&` as otherwise the line ends in +`0 .. -1 + length( $x && push @res, $c)`; -This is why we right the "yoda" looking -1 + length $c as if you write length $c - 1 this evaluates to length($c-1); +This is why we write the "yoda" looking `-1 + length $c` as if you write `length $c - 1` this evaluates to `length($c-1)`; -I wouldn't do this in "normal" code as I think it can get confusing $x && f() is not obviously a piece of logic & -especially if f() has implicit side effects as here. +I wouldn't do this in "normal" code as I think it can get confusing `$x && f()` is not obviously a piece of logic, +especially if `f()` has implicit side effects as here. If we don't want to capture the values - but just display the results - we can drop this into a perl 1-liner on the command liner. @@ -84,11 +86,11 @@ perl -E '($c-join"",map{0+(@Q=$c=~/($_)/g)}0..-1+length$c)||++$n&&say$c while++$ You will notice we are using slightly different tricks here... (Mainly we can do these because we haven't enabled strict!! something you rarely do in Perl 1-liners...) - * We use -E (rather than -e) this enables more modern perl features - including usefully say! + * We use `-E` (rather than `-e`) this enables more modern perl features - including usefully say! * We don't collect results - and we just keep a counter - this time we use `||` and `&&` in the "logic"... - * We know ++$n is always going to be true (it starts of explicitly) and so we always run `say$c` if we get to the `++$n`.. + * We know `++$n` is always going to be true (it starts of explicitly) and so we always run `say$c` if we get to the `++$n`.. * Note here - this is a place where it is important to choose `++$n` rather than the more common `$n++`, as the first evaluates to `0` the first time it is invoked - meaning we would skip the first answer... @@ -96,9 +98,9 @@ you rarely do in Perl 1-liners...) * We can use another trick other than the scalar `@{[ ]}` trick to convert the list into an array. we store it in an array variable which makes it we can then get the length of the array (we just ignore the array!) - * As we are keeping the code short - we can replace the keyword scalar with a simple 0+ which forces the array to be converted into a scalar (and hence returns the length) + * As we are keeping the code short - we can replace the keyword scalar with a simple `0+` which forces the array to be converted into a scalar (and hence returns the length) - * To gain another character as the equality is numeric we can rewrite if($a==$b) { f() } as ($a-$b)||f(). + * To gain another character as the equality is numeric we can rewrite `if($a==$b) { f() }` as `($a-$b)||f()`. `$a-$b` is non-zero (true) if `$a!=$b` @@ -108,13 +110,13 @@ you rarely do in Perl 1-liners...) `if( $a == $b ) { f() }` as `unless( $a - $b ) { f() }` - which we know we mentioned we could rewrite as: + which we know we mentioned we could rewrite as: - `($a-$b) || f()` + `($a-$b) || f()` - The brackets are important o/w this evaluates to: + The brackets are important o/w this evaluates to: - `$a-($b||f())` + `$a-($b||f())` which isn't what we want... @@ -123,9 +125,9 @@ you rarely do in Perl 1-liners...) When you import a class `%{class}::` contains a list of the methods for the class, so we can just dump these (to make it deterministic) we sort first.... -We can "import" the classes in two ways... use & require. +We can "import" the classes in two ways... `use` and `require`. -By "require"ing the class - we don't get the additional import method added to the +By `require`ing the class - we don't get the additional import method added to the class... (and so get the same list of fns as in the question) ``` @@ -145,7 +147,7 @@ my $fn = $obj->can('action_'.$command); if( $fn ) { $obj->$fn( {'param'=>''} ); ``` -The grep is as followe: +The grep is as follows: ``` say join "\n ",'Calc_Require:', sort @@ -157,5 +159,3 @@ say join "\n ",'Calc_Use:', sort keys %Calc_Use::; say ''; ``` - - diff --git a/challenge-107/james-smith/perl/ch-1.pl b/challenge-107/james-smith/perl/ch-1.pl index dbec8818a5..2aabbee32c 100644 --- a/challenge-107/james-smith/perl/ch-1.pl +++ b/challenge-107/james-smith/perl/ch-1.pl @@ -10,8 +10,8 @@ use Test::More; ## 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 +## +## 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" ## @@ -20,12 +20,12 @@ use Test::More; ## 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 +## 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 +## list of matches to an array { subtle distinction here between ## lists and arrays! } and counting it by getting the scalar of ## the array... ## |
