diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2020-10-13 21:53:07 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2020-10-13 21:53:07 +0800 |
| commit | a3b7884792e5b7a8f147a294989fa49b65e9308f (patch) | |
| tree | 27bd5f483c68238e9708d20f5985e2c8a578a0f3 | |
| parent | fa2e628623cb8c8437ff27ffe231894c7593f777 (diff) | |
| parent | 73bd8af8d58941b8066141bc3fc64cf4165d80b5 (diff) | |
| download | perlweeklychallenge-club-a3b7884792e5b7a8f147a294989fa49b65e9308f.tar.gz perlweeklychallenge-club-a3b7884792e5b7a8f147a294989fa49b65e9308f.tar.bz2 perlweeklychallenge-club-a3b7884792e5b7a8f147a294989fa49b65e9308f.zip | |
Merge remote-tracking branch 'upstream/master'
44 files changed, 2555 insertions, 1612 deletions
diff --git a/challenge-082/andinus/README b/challenge-082/andinus/README index 13354ca364..2ad8a0f3af 100644 --- a/challenge-082/andinus/README +++ b/challenge-082/andinus/README @@ -1,5 +1,5 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 081 + CHALLENGE 082 Andinus ━━━━━━━━━━━━━━━ @@ -8,25 +8,19 @@ Table of Contents ───────────────── -1. Task 1 - Common Base String +1. Task 1 - Common Factors .. 1. Perl -2. Task 2 - Frequency Sort -.. 1. Perl - -1 Task 1 - Common Base String -═════════════════════════════ - You are given 2 strings, `$A' and `$B'. +1 Task 1 - Common Factors +═════════════════════════ - Write a script to find out common base strings in `$A' and `$B'. + You are given 2 positive numbers $M and $N. - A substring of a string $S is called base string if - repeated concatenation of the substring results in the - string. + Write a script to list all common factors of the given numbers. 1.1 Perl @@ -34,42 +28,16 @@ Table of Contents • Program: <file:perl/ch-1.pl> - We will break `$A' & check if any subset of `$A' join to make `$B'. To - speed up the process we only break `$A' by common divisors of both - `$A' & `$B'. - - I assume that the length of `$B' is greater than `$A' in later parts - so we make sure that it's true. + We loop over all the numbers from `1 ... $M' to get their factors & + then just compare it with factors of `$N'. I took this code from + Challenge 081's ch-1.pl. ┌──── │ my $A = shift @ARGV; │ my $B = shift @ARGV; │ - │ # We assume length($B) is greater than length($A). - │ unless (length($B) > length($A)) { - │ my $tmp = $A; - │ $A = $B; - │ $B = $tmp; - │ } - └──── - - If the strings have different sets of characters then common base - string cannot exists so we exit early. - ┌──── - │ # Check if common base string is even possible. - │ my (%chars_in_A, %chars_in_B); - │ $chars_in_A{$_} = 1 foreach split //, $A; - │ $chars_in_B{$_} = 1 foreach split //, $B; - │ foreach my $char (sort keys %chars_in_A) { - │ last if exists $chars_in_B{$char} ; - │ print "No common base string.\n" and exit 0 - │ } - └──── - - Get all the common divisors of `$A' & `$B'. - ┌──── │ # Get all common divisors. - │ my %divisors_of_A = divisors(length($A)); - │ my %divisors_of_B = divisors(length($B)); + │ my %divisors_of_A = divisors($A); + │ my %divisors_of_B = divisors($B); │ my @common_divisors; │ foreach my $num (sort { $a <=> $b } keys %divisors_of_A) { │ push @common_divisors, $num @@ -87,76 +55,6 @@ Table of Contents │ } │ return %divisors; │ } - └──── - - We check if any subset of `$A' joins to make `$B'. - ┌──── - │ my @common; │ - │ foreach my $num (@common_divisors){ - │ my $tmp; - │ my $base = substr($A, 0, $num); - │ foreach (1 ... length($B) / $num) { - │ $tmp .= $base; - │ } - │ push @common, $base if $tmp eq $B; - │ } - │ - │ print "No common base string.\n" and exit 0 - │ unless scalar @common; - │ print join(', ', @common), "\n"; - └──── - - -2 Task 2 - Frequency Sort -═════════════════════════ - - You are given file named input. - - Write a script to find the frequency of all the words. - - It should print the result as first column of each line should be the - frequency of the the word followed by all the words of that frequency - arranged in lexicographical order. Also sort the words in the - ascending order of frequency. - - For the sake of this task, please ignore the following in the input - file: `. " ( ) , 's --' - - -2.1 Perl -──────── - - • Program: <file:perl/ch-2.pl> - - Swap unwanted characters with a space. - ┌──── - │ my $file = path(shift @ARGV)->slurp; - │ - │ $file =~ s/(--|'s)/ /g; - │ $file =~ s/[."(),]+/ /g; - │ $file =~ s/ / /g; - │ $file =~ s/\n/ /g; - └──── - - Get frequency of each word. - ┌──── - │ my %words; - │ foreach my $word (split / /, $file) { - │ $words{$word} = 1 and next unless exists $words{$word}; - │ $words{$word}++; - │ } - └──── - - Format the output. - ┌──── - │ my %out; - │ foreach my $word (sort keys %words) { - │ my $freq = $words{$word}; - │ push @{$out{$freq}}, $word; - │ } - │ - │ foreach my $freq (sort { $a <=> $b} keys %out) { - │ print "$freq ", join(' ', @{$out{$freq}}, "\n"); - │ } + │ print join(', ', @common_divisors), "\n"; └──── diff --git a/challenge-082/andinus/blog-1.txt b/challenge-082/andinus/blog-1.txt new file mode 100644 index 0000000000..770aa38b8d --- /dev/null +++ b/challenge-082/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.tilde.institute/pwc/challenge-082/ diff --git a/challenge-082/andinus/perl/ch-1.pl b/challenge-082/andinus/perl/ch-1.pl new file mode 100755 index 0000000000..6dfa71fa0f --- /dev/null +++ b/challenge-082/andinus/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +die "usage: ./ch-1.pl <num> <num>" + unless scalar @ARGV == 2; + +my $A = shift @ARGV; +my $B = shift @ARGV; + +# Get all common divisors. +my %divisors_of_A = divisors($A); +my %divisors_of_B = divisors($B); +my @common_divisors; +foreach my $num (sort { $a <=> $b } keys %divisors_of_A) { + push @common_divisors, $num + if exists $divisors_of_B{$num}; +} + +# Returns all divisors of a number. +sub divisors { + my $n = shift @_; + my %divisors; + foreach my $i ( 1 ... $n){ + if ($n % $i == 0) { + $divisors{$i} = 1; + } + } + return %divisors; +} + +print join(', ', @common_divisors), "\n"; diff --git a/challenge-082/ash/raku/ch-1.raku b/challenge-082/ash/raku/ch-1.raku new file mode 100644 index 0000000000..f6aaef2abe --- /dev/null +++ b/challenge-082/ash/raku/ch-1.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +# +# Task 1 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/ + +# Test runs: +# $ raku ch-1.raku 12 18 +# (1 2 3 6) +# $ raku ch-1.raku 100 500 +# (1 2 4 5 10 20 25 50 100) +# $ raku ch-1.raku 18 23 +# (1) + +my ($a, $b) = @*ARGS; + +say ((1 .. ($a max $b)).grep: $a %% *).grep: $b %% *; diff --git a/challenge-082/ash/raku/ch-2.raku b/challenge-082/ash/raku/ch-2.raku new file mode 100644 index 0000000000..0c95ea66fb --- /dev/null +++ b/challenge-082/ash/raku/ch-2.raku @@ -0,0 +1,23 @@ +#!/usr/bin/env raku +# +# Task 2 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/ + +unit sub MAIN(Str $a, Str $b, Str $c); + +my @parts; +for ^$a.chars -> $pos { + say 1 and return if $c eq $a.substr(0, $pos) ~ $b ~ $a.substr($pos); +} + +# Not sure if the below tests agree with the idea of the task. + +say 2 and return if $c eq $a ~ $b; + +for ^$b.chars -> $pos { + say 3 and return if $c eq $b.substr(0, $pos) ~ $a ~ $b.substr($pos); +} + +say 4 and return if $c eq $b ~ $a; + +say 0; diff --git a/challenge-082/e-choroba/perl5/ch-1.pl b/challenge-082/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..bac0ff5500 --- /dev/null +++ b/challenge-082/e-choroba/perl5/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub common_factors { + my ($m, $n) = @_; + ($m, $n) = ($n, $m) if $n < $m; + my @r; + for my $i (1 .. $m) { + push @r, $i if 0 == $m % $i + && 0 == $n % $i; + } + return \@r +} + +use Test::More; + +is_deeply common_factors(12, 18), [1, 2, 3, 6], 'Example 1'; +is_deeply common_factors(18, 23), [1], 'Example 2'; +is_deeply common_factors(123_456_789, 987_654_321), [1, 3, 9], 'large numbers'; + +done_testing(); + diff --git a/challenge-082/e-choroba/perl5/ch-2.pl b/challenge-082/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..e61bbe3dd4 --- /dev/null +++ b/challenge-082/e-choroba/perl5/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub interleave_string { + my ($x, $y, $z) = @_; + return 0 if length($z) != length($x) + length($y); + + return 1 if "" eq $z; + + my ($first_x, $first_y, $first_z) = map { substr $_, 0, 1 } $x, $y, $z; + + my $maybe_x = $first_x eq $first_z; + my $maybe_y = $first_y eq $first_z; + + my $rest_x = $maybe_x ? substr $x, 1 : ""; + my $rest_y = $maybe_y ? substr $y, 1 : ""; + my $rest_z = substr $z, 1; + + if ($maybe_x && $maybe_y) { + return interleave_string($rest_x, $y, $rest_z) + || interleave_string($x, $rest_y, $rest_z) + } + + return interleave_string($rest_x, $y, $rest_z) if $maybe_x; + + return interleave_string($x, $rest_y, $rest_z) if $maybe_y; + + return 0 +} + +use Test::More; + +is interleave_string('XY', 'X', 'XXY'), 1, 'Example 1'; +is interleave_string('XXY', 'XXZ', 'XXXXZY'), 1, 'Example 2'; +is interleave_string('YX', 'X', 'XXY'), 0, 'Example 3'; + +is interleave_string('ABC', 'BD', $_), 1, $_ + for qw( ABDBC ABCBD ABBCD ABBDC BABCD BABDC BADBC BDABC ); + + +done_testing(); diff --git a/challenge-082/gugod/raku/ch-1.raku b/challenge-082/gugod/raku/ch-1.raku new file mode 100644 index 0000000000..f2d918b47b --- /dev/null +++ b/challenge-082/gugod/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku + +sub MAIN (Int $M, Int $N) { + my ($a, $b) = ($M, $N).sort; + say common-factors($a, $b); +} + +sub common-factors (Int $a, Int $b) { + (1, 2..$a/2, $a).flat.grep(-> $n { 0 == $a % $n == $b % $n }); +} diff --git a/challenge-082/gugod/raku/ch-2.raku b/challenge-082/gugod/raku/ch-2.raku new file mode 100644 index 0000000000..047c4e0c4d --- /dev/null +++ b/challenge-082/gugod/raku/ch-2.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +sub MAIN (Str $A, Str $B, Str $C) { + say interleaves($A, $B, $C) ?? 1 !! 0; +} + +sub interleaves (Str $A, Str $B, Str $C) { + my @stash; + @stash.push([-1, -1]); + + my $i = 0; + while $i < $C.chars && @stash.elems > 0 { + my $c = $C.substr($i++, 1); + my @stash2 = gather { + while @stash.elems > 0 { + my $it = @stash.pop(); + my $a = $A.substr($it[0]+1, 1); + my $b = $B.substr($it[1]+1, 1); + if $c eq $a { + take [$it[0]+1, $it[1]]; + } + if $c eq $b { + take [$it[0], $it[1]+1]; + } + } + }; + + @stash = @stash2.unique(:with(&[eqv])); + } + + return $i == $C.chars && @stash.elems > 0 && @stash[0][0].succ == $A.chars && @stash[0][1].succ == $B.chars; +} diff --git a/challenge-082/mark-anderson/raku/ch-1.p6 b/challenge-082/mark-anderson/raku/ch-1.p6 new file mode 100644 index 0000000000..42e1fe2b36 --- /dev/null +++ b/challenge-082/mark-anderson/raku/ch-1.p6 @@ -0,0 +1,12 @@ +unit sub MAIN(UInt $m, UInt $n); + +say common-factors($m, $n); + +sub common-factors($m, $n) { + + sub factor($i) { + (1..($i div 2)).grep($i %% *); + } + + (factor($m) (&) factor($n)).keys.sort.join(", ").List; +} diff --git a/challenge-082/mark-anderson/raku/ch-2.p6 b/challenge-082/mark-anderson/raku/ch-2.p6 new file mode 100644 index 0000000000..be03ab8fae --- /dev/null +++ b/challenge-082/mark-anderson/raku/ch-2.p6 @@ -0,0 +1,21 @@ +use JSON::Fast; + +subset Tiny-Str of Str where .chars < 10; + +#| A.chars < 10, B.chars < 10, C.chars == A.chars + B.chars +unit sub MAIN(Tiny-Str $A, Tiny-Str $B, $C where .chars == $A.chars + $B.chars); + +my @terms := |from-json "terms.json".IO.slurp; + +say interleaved($A, $B, $C); + +sub interleaved($A, $B, $C) { + for @terms[$A.chars].Array X @terms[$B.chars].Array -> (@A, @B) { + for ($A, $B, @A, @B), ($B, $A, @B, @A) -> ($S1, $S2, @A1, @A2) { + return 1 if roundrobin($S1.comb.rotor(@A1), $S2.comb.rotor(@A2)) + .flat.join eq $C; + } + } + + return 0; +} diff --git a/challenge-082/mark-anderson/raku/terms.json b/challenge-082/mark-anderson/raku/terms.json new file mode 100644 index 0000000000..ca62128af6 --- /dev/null +++ b/challenge-082/mark-anderson/raku/terms.json @@ -0,0 +1 @@ +[[[0]],[[1]],[[1,1],[2]],[[1,1,1],[3],[1,2],[2,1]],[[1,1,1,1],[2,2],[4],[1,3],[3,1],[1,1,2],[1,2,1],[2,1,1]],[[1,1,1,1,1],[5],[1,2,2],[2,1,2],[2,2,1],[1,4],[4,1],[1,1,3],[1,3,1],[3,1,1],[1,1,1,2],[1,1,2,1],[1,2,1,1],[2,1,1,1],[2,3],[3,2]],[[1,1,1,1,1,1],[2,2,2],[3,3],[6],[1,5],[5,1],[1,1,2,2],[1,2,1,2],[1,2,2,1],[2,1,1,2],[2,1,2,1],[2,2,1,1],[1,1,4],[1,4,1],[4,1,1],[1,1,1,3],[1,1,3,1],[1,3,1,1],[3,1,1,1],[1,1,1,1,2],[1,1,1,2,1],[1,1,2,1,1],[1,2,1,1,1],[2,1,1,1,1],[2,4],[4,2],[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],[[1,1,1,1,1,1,1],[7],[1,2,2,2],[2,1,2,2],[2,2,1,2],[2,2,2,1],[1,3,3],[3,1,3],[3,3,1],[1,6],[6,1],[1,1,5],[1,5,1],[5,1,1],[1,1,1,2,2],[1,1,2,1,2],[1,1,2,2,1],[1,2,1,1,2],[1,2,1,2,1],[1,2,2,1,1],[2,1,1,1,2],[2,1,1,2,1],[2,1,2,1,1],[2,2,1,1,1],[1,1,1,4],[1,1,4,1],[1,4,1,1],[4,1,1,1],[1,1,1,1,3],[1,1,1,3,1],[1,1,3,1,1],[1,3,1,1,1],[3,1,1,1,1],[1,1,1,1,1,2],[1,1,1,1,2,1],[1,1,1,2,1,1],[1,1,2,1,1,1],[1,2,1,1,1,1],[2,1,1,1,1,1],[2,5],[5,2],[2,2,3],[2,3,2],[3,2,2],[3,4],[4,3],[1,2,4],[1,4,2],[2,1,4],[2,4,1],[4,1,2],[4,2,1],[1,1,2,3],[1,1,3,2],[1,2,1,3],[1,2,3,1],[1,3,1,2],[1,3,2,1],[2,1,1,3],[2,1,3,1],[2,3,1,1],[3,1,1,2],[3,1,2,1],[3,2,1,1]],[[1,1,1,1,1,1,1,1],[2,2,2,2],[4,4],[8],[1,7],[7,1],[1,1,2,2,2],[1,2,1,2,2],[1,2,2,1,2],[1,2,2,2,1],[2,1,1,2,2],[2,1,2,1,2],[2,1,2,2,1],[2,2,1,1,2],[2,2,1,2,1],[2,2,2,1,1],[1,1,3,3],[1,3,1,3],[1,3,3,1],[3,1,1,3],[3,1,3,1],[3,3,1,1],[1,1,6],[1,6,1],[6,1,1],[1,1,1,5],[1,1,5,1],[1,5,1,1],[5,1,1,1],[1,1,1,1,2,2],[1,1,1,2,1,2],[1,1,1,2,2,1],[1,1,2,1,1,2],[1,1,2,1,2,1],[1,1,2,2,1,1],[1,2,1,1,1,2],[1,2,1,1,2,1],[1,2,1,2,1,1],[1,2,2,1,1,1],[2,1,1,1,1,2],[2,1,1,1,2,1],[2,1,1,2,1,1],[2,1,2,1,1,1],[2,2,1,1,1,1],[1,1,1,1,4],[1,1,1,4,1],[1,1,4,1,1],[1,4,1,1,1],[4,1,1,1,1],[1,1,1,1,1,3],[1,1,1,1,3,1],[1,1,1,3,1,1],[1,1,3,1,1,1],[1,3,1,1,1,1],[3,1,1,1,1,1],[1,1,1,1,1,1,2],[1,1,1,1,1,2,1],[1,1,1,1,2,1,1],[1,1,1,2,1,1,1],[1,1,2,1,1,1,1],[1,2,1,1,1,1,1],[2,1,1,1,1,1,1],[2,3,3],[3,2,3],[3,3,2],[2,6],[6,2],[2,2,4],[2,4,2],[4,2,2],[3,5],[5,3],[1,2,5],[1,5,2],[2,1,5],[2,5,1],[5,1,2],[5,2,1],[1,2,2,3],[1,2,3,2],[1,3,2,2],[2,1,2,3],[2,1,3,2],[2,2,1,3],[2,2,3,1],[2,3,1,2],[2,3,2,1],[3,1,2,2],[3,2,1,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,1,1,1],[2,3,1,1,1,1],[3,1,1,1,1,2],[3,1,1,1,2,1],[3,1,1,2,1,1],[3,1,2,1,1,1],[3,2,1,1,1,1],[2,3,4],[2,4,3],[3,2,4],[3,4,2],[4,2,3],[4,3,2]]] diff --git a/challenge-082/markus-holzer/raku/ch-1.raku b/challenge-082/markus-holzer/raku/ch-1.raku new file mode 100644 index 0000000000..4b5f234cda --- /dev/null +++ b/challenge-082/markus-holzer/raku/ch-1.raku @@ -0,0 +1,3 @@ +unit sub MAIN( Int $N, Int $M ); + +say "({ join ', ', grep all( $N, $M ) %% *, 1 ..^ max $N, $M })"
\ No newline at end of file diff --git a/challenge-082/markus-holzer/raku/ch-2.raku b/challenge-082/markus-holzer/raku/ch-2.raku new file mode 100644 index 0000000000..56d47ff51d --- /dev/null +++ b/challenge-082/markus-holzer/raku/ch-2.raku @@ -0,0 +1,3 @@ +unit sub MAIN( Str $A, Str $B, Str $C where $C.chars == $A.chars + $B.chars ); + +say +so grep { $C eq [~] $A.substr( 0, $^i ), $B, $A.substr( $^i ) }, ^$A.chars
\ No newline at end of file diff --git a/challenge-082/perlboy1967/perl/ch-1.pl b/challenge-082/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..43ffa86ce0 --- /dev/null +++ b/challenge-082/perlboy1967/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 082 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/ +# +# Task 1 - Common Factors +# +# Author: Niels 'PerlBoy' van Dijke + +use strict; +use warnings; + +@ARGV = qw(16 128) unless scalar @ARGV; + +my ($N, $M) = @ARGV; + +die '$N and $M must be positive integers and not equal' + unless (defined $N and $N =~ m#^[1-9][0-9]*$# and + defined $M and $M =~ m#^[1-9][0-9]*$#); + +my @n = factors($N); +my @m = factors($M); + +my @i = intersect(\@n, \@m); + +printf "\$N = %d\n", $N; +printf "\$M = %d\n", $M; +print "\n"; +printf "The %d factors of \$N (%d) %s: %s\n", + scalar(@n), $N, + (scalar @n > 1 ? 'are' : 'is'), + join(', ', @n); +printf "The %d factors of \$M (%d) %s: %s\n", + scalar(@m), $M, + (scalar @m > 1 ? 'are' : 'is'), + join(', ', @m); +print "\n"; +printf "The %d common factors of \$N (%d) and \$M (%d) %s: %s\n\n", + scalar(@i), $N, $M, + (scalar @i > 1 ? 'are' : 'is'), + join(", ", @i); + +sub factors { + my ($n) = @_; + + my @d; + + for my $i (1 .. $n) { + my $d = int($n/$i); + push(@d, $i) if ($d * $i == $n); + } + + return @d; +} + +sub intersect { + my ($ar1, $ar2) = @_; + + my %c = map { $_ => 1 } @$ar1; + + return grep { exists $c{$_} } @$ar2; +} + diff --git a/challenge-082/perlboy1967/perl/ch-2.pl b/challenge-082/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..e2756ba3f2 --- /dev/null +++ b/challenge-082/perlboy1967/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 082 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/ +# +# Task 2 - Interleave String +# +# Author: Niels 'PerlBoy' van Dijke + +use strict; +use warnings; + +use Data::Printer; + +@ARGV = qw(4X 123X678 1234XX678) + unless (scalar @ARGV >= 3); + +my ($A, $B, $C) = @ARGV; + +my $res = 'NONE'; + +if (length($A) + length($B) == length($C) and + $C =~ m#^((?<A1>.*?)$B(?<A2>.*)|(?<B1>.*?)$A(?<B2>.*))$#gc) { + if (($+{A1} // '').($+{A2} // '') eq $A) { + $res = "$+{A1}|$B|$+{A2}"; + } elsif (($+{B1} // '').($+{B2} // '') eq $B) { + $res = "$+{B1}|$A|$+{B2}"; + } +} + +printf qq{ +Input: + \$A = "$A" + \$B = "$B" + \$C = "$C" + +Output: %d + +Interleaving: $res +}, $res ne 'NONE'; + diff --git a/challenge-082/roger-bell-west/perl/ch-1.pl b/challenge-082/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..52d220c256 --- /dev/null +++ b/challenge-082/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,38 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 5; + +is_deeply(factor(12),{1 => 1, 2 => 1, 3 => 1, 4 => 1, 6 => 1, 12 => 1},'twelve'); +is_deeply(factor(18),{1 => 1, 2 => 1, 3 => 1, 6 => 1, 9 => 1, 18 => 1},'eighteen'); +is_deeply(factor(23),{1 => 1, 23 => 1},'twenty-three'); + +is_deeply(commonfactor(12,18),[1,2,3,6],'twelve-eighteen'); +is_deeply(commonfactor(18,23),[1],'twelve-twentythree'); + +sub factor { + my $n=shift; + my %o=map {$_ => 1} (1,$n); + foreach my $i (2..int(sqrt($n))) { + if ($n % $i == 0) { + $o{$n/$i}=$o{$i}=1; + } + } + return \%o; +} + +sub commonfactor { + my @f=map {factor($_)} @_; + my $s=shift @f; + while (@f) { + my $q=shift @f; + foreach my $f (keys %{$s}) { + unless (exists $q->{$f}) { + delete $s->{$f}; + } + } + } + return [sort keys %{$s}]; +} diff --git a/challenge-082/roger-bell-west/perl/ch-2.pl b/challenge-082/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..30b078022a --- /dev/null +++ b/challenge-082/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl + +use strict; +use |
