diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-03-09 16:29:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-09 16:29:34 +0000 |
| commit | 6c13e46302c45df850efbd78080ba2f2dd9eb7c8 (patch) | |
| tree | 8a22016cfc7ffa32aab3d6dfda3cc94e865624ea /challenge-154 | |
| parent | da6bb77132c98480bb2eccd9f885f5df5000dfb0 (diff) | |
| parent | ec35f0f52efb79198e2febfbf20d953769c19178 (diff) | |
| download | perlweeklychallenge-club-6c13e46302c45df850efbd78080ba2f2dd9eb7c8.tar.gz perlweeklychallenge-club-6c13e46302c45df850efbd78080ba2f2dd9eb7c8.tar.bz2 perlweeklychallenge-club-6c13e46302c45df850efbd78080ba2f2dd9eb7c8.zip | |
Merge pull request #5756 from drbaggy/master
NEW
Diffstat (limited to 'challenge-154')
| -rw-r--r-- | challenge-154/james-smith/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-154/james-smith/perl/ch-2.pl | 9 |
2 files changed, 12 insertions, 7 deletions
diff --git a/challenge-154/james-smith/perl/ch-1.pl b/challenge-154/james-smith/perl/ch-1.pl index 96867f6e76..a74a072467 100644 --- a/challenge-154/james-smith/perl/ch-1.pl +++ b/challenge-154/james-smith/perl/ch-1.pl @@ -46,12 +46,20 @@ say ''; my $w = join '', @s = sort split //, 'PERL'; my %check = map { $_=>1 } @words; +do { exists $check{ $w } || say " * $w"; } while $w = next_perm; # We have to use "do/while" here rather than just "while" to # make sure we include the first term, o/w we would miss it # out with the update $_ = next_perm happening first -do { exists $check{$w} || say " * $w" } while $w = next_perm; +$w = join '', @s = sort split //, 'PERL'; +my @sorted = sort @words; + +say ''; +say 'Solution - with no assumptions about # missing - sorted strings (unique)'; +say ''; + +do { $w lt $sorted[0] ? say " * $w" : shift @sorted } while $w = next_perm; ## "Hack" solution diff --git a/challenge-154/james-smith/perl/ch-2.pl b/challenge-154/james-smith/perl/ch-2.pl index c3ac18bb11..7e36e0b7d4 100644 --- a/challenge-154/james-smith/perl/ch-2.pl +++ b/challenge-154/james-smith/perl/ch-2.pl @@ -4,14 +4,11 @@ use strict; use warnings; use feature qw(say); -use Time::HiRes qw(time); use Math::Prime::Util qw(is_prime); # We only keep the last 3 Padovan numbers as that is all we need # to generate the next one... We use the variables $p,$q,$r for # this.. - - # We use a little used perl construct here - `redo` which restarts # the loop everytime we have a Padovan number which is composite # with updating "$_" - so we get 10 results. @@ -19,16 +16,16 @@ use Math::Prime::Util qw(is_prime); # "1-liner" solution... my$p=my$q=my$r=1; -($p,$q,$r)=($q,$r,$p+$q),$r!=$q&&is_prime($r)?say$r:redo for 1..10; +($p,$q,$r)=($q,$r,$p+$q),$r!=$q&&is_prime($r)?say$r:redo for 0..9; # "expanded" solution... $p=$q=$r=1; -for (1..10) { +for (0..9) { ($p,$q,$r) = ($q,$r,$p+$q); redo if $r == $q; ## skip (redo loop) if same as previous value redo unless is_prime($r); ## skip (redo loop) if not prime - say $r; ## output if we get here! + say $r; ## output if we get here, $_ will get updated } |
