diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-08 19:02:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-08 19:02:18 +0000 |
| commit | 330ccbe7e5393aae00591e8ae087119d9b9f2c7b (patch) | |
| tree | de72013edc374c776716bbe99bcb988f08a06eb6 /challenge-142 | |
| parent | ef93fd69449a184b5681415853c4272bb85bf384 (diff) | |
| parent | 97280f04c1884f95c991a2d1190b1ddf6fdf9613 (diff) | |
| download | perlweeklychallenge-club-330ccbe7e5393aae00591e8ae087119d9b9f2c7b.tar.gz perlweeklychallenge-club-330ccbe7e5393aae00591e8ae087119d9b9f2c7b.tar.bz2 perlweeklychallenge-club-330ccbe7e5393aae00591e8ae087119d9b9f2c7b.zip | |
Merge pull request #5348 from ccntrq/challenge-142
Challenge 142
Diffstat (limited to 'challenge-142')
| -rw-r--r-- | challenge-142/alexander-pankoff/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-142/alexander-pankoff/perl/ch-2.pl | 34 |
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-142/alexander-pankoff/perl/ch-1.pl b/challenge-142/alexander-pankoff/perl/ch-1.pl new file mode 100644 index 0000000000..c801ae5691 --- /dev/null +++ b/challenge-142/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; +use feature qw'say signatures'; +no warnings 'experimental::signatures'; + +use FindBin; + +package Challenge141Task01 { + require $FindBin::RealBin + . '/../../../challenge-141/alexander-pankoff/perl/ch-1.pl'; +} + +package Challenge141Task02 { + require $FindBin::RealBin + . '/../../../challenge-141/alexander-pankoff/perl/ch-2.pl'; +} + +use constant DEBUG => $ENV{DEBUG} // 0; + +run() unless caller(); + +sub run() { + my $m = Challenge141Task02::prompt_for_integer('m'); + my $n; + do { + $n = Challenge141Task02::prompt_for_integer('n'); + } while ( length $n > 1 && say "Expect 'n' to be a single digit." ); + + # from the description of the task I infer, this week the number 'm' itself + # should not be considered a divisor of 'm'. This is different from last + # week, where we found numbers with exactly 8 divisors including the number + # itself + my @divisors = grep { $_ != $m } Challenge141Task01::find_divisors($m); + say join( ", ", sort { $a <=> $b } @divisors ) if DEBUG; + my @result = grep { substr( $_, -1 ) == $n } @divisors; + say join( ", ", sort { $a <=> $b } @result ) if DEBUG; + say scalar @result; +} diff --git a/challenge-142/alexander-pankoff/perl/ch-2.pl b/challenge-142/alexander-pankoff/perl/ch-2.pl new file mode 100644 index 0000000000..b2fc4dfa2d --- /dev/null +++ b/challenge-142/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +use feature qw'say signatures'; +no warnings 'experimental::signatures'; + +use threads; +use threads::shared; + +use constant DEBUG => $ENV{DEBUG} // 0; + +run() unless caller(); + +sub run() { + my @input = map { int( rand(30) ) } 0 .. 1000; + say join( ", ", sleep_sort(@input) ); +} + +sub sleep_sort(@xs) { + my @sorted; + share(@sorted); + my @threads = + map { + threads->create( + sub($n) { + sleep $n; + push @sorted, $n; + }, + $_ + ) + } @xs; + $_->join() for @threads; + return @sorted; +} + |
