diff options
36 files changed, 2656 insertions, 2023 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/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,2],[3,2,2,1],[1,3,4],[1,4,3],[3,1,4],[3,4,1],[4,1,3],[4,3,1],[1,1,2,4],[1,1,4,2],[1,2,1,4],[1,2,4,1],[1,4,1,2],[1,4,2,1],[2,1,1,4],[2,1,4,1],[2,4,1,1],[4,1,1,2],[4,1,2,1],[4,2,1,1],[1,1,1,2,3],[1,1,1,3,2],[1,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,2,2,1],[2,1,1,2,3],[2,1,1,3,2],[2,1,2,1,3],[2,1,2,3,1],[2,1,3,1,2],[2,1,3,2,1],[2,2,1,1,3],[2,2,1,3,1],[2,2,3,1,1],[2,3,1,1,2],[2,3,1,2,1],[2,3,2,1,1],[3,1,1,2,2],[3,1,2,1,2],[3,1,2,2,1],[3,2,1,1,2],[3,2,1,2,1],[3,2,2,1,1],[1,1,3,4],[1,1,4,3],[1,3,1,4],[1,3,4,1],[1,4,1,3],[1,4,3,1],[3,1,1,4],[3,1,4,1],[3,4,1,1],[4,1,1,3],[4,1,3,1],[4,3,1,1],[1,1,1,2,4],[1,1,1,4,2],[1,1,2,1,4],[1,1,2,4,1],[1,1,4,1,2],[1,1,4,2,1],[1,2,1,1,4],[1,2,1,4,1],[1,2,4,1,1],[1,4,1,1,2],[1,4,1,2,1],[1,4,2,1,1],[2,1,1,1,4],[2,1,1,4,1],[2,1,4,1,1],[2,4,1,1,1],[4,1,1,1,2],[4,1,1,2,1],[4,1,2,1,1],[4,2,1,1,1],[1,1,1,1,2,3],[1,1,1,1,3,2],[1,1,1,2,1,3],[1,1,1,2,3,1],[1,1,1,3,1,2],[1,1,1,3,2,1],[1,1,2,1,1,3],[1,1,2,1,3,1],[1,1,2,3,1,1],[1,1,3,1,1,2],[1,1,3,1,2,1],[1,1,3,2,1,1],[1,2,1,1,1,3],[1,2,1,1,3,1],[1,2,1,3,1,1],[1,2,3,1,1,1],[1,3,1,1,1,2],[1,3,1,1,2,1],[1,3,1,2,1,1],[1,3,2,1,1,1],[2,1,1,1,1,3],[2,1,1,1,3,1],[2,1,1,3,1,1],[2,1,3,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/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 warnings; + +use Test::More tests => 5; + +is(isinterleave(qw(XY X XXY)),1,'example 1'); +is(isinterleave(qw(XXY XXZ XXXXZY)),1,'example 2'); +is(isinterleave(qw(YX X XXY)),0,'example 3'); +is(isinterleave(qw(ACF BDEG ABCDEFG)),1,'example 4'); +is(isinterleave(qw(ACF BDGE ABCDEFG)),0,'example 5'); + +sub isinterleave { + my ($a,$b,$c)=@_; + my @s=(map {[split '',$_]} ($a,$b,$c)); + my @l=map {$#{$_}} @s; + my @buf=([0,0,0]); + while (@buf) { + my $n=shift @buf; + if ($n->[2] > $l[2]) { + return 1; + } + if ($n->[0] <= $l[0] && $s[0][$n->[0]] eq $s[2][$n->[2]]) { + push @buf,[$n->[0]+1,$n->[1],$n->[2]+1]; + } + if ($n->[1] <= $l[1] && $s[1][$n->[1]] eq $s[2][$n->[2]]) { + push @buf,[$n->[0],$n->[1]+1,$n->[2]+1]; + } + } + return 0; +} diff --git a/challenge-082/roger-bell-west/python/ch-1.py b/challenge-082/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..88e8cc4399 --- /dev/null +++ b/challenge-082/roger-bell-west/python/ch-1.py @@ -0,0 +1,40 @@ +#! /usr/bin/python3 + +import unittest +from math import sqrt + +def factor(n): + o=set() + o.add(1) + o.add(n) + for i in range(2,int(sqrt(n))+1): + if (n % i == 0): + o.add(int(n/i)) + o.add(i) + return o + +def commonfactor(*fs): + f=list(map(factor,fs)) + s=f.pop(); + while (f): + s = s & f.pop() + return sorted(s) + +class TestCf(unittest.TestCase): + + def test_f1(self): + self.assertEqual(factor(12),set((1,2,3,4,6,12)),'twelve') + + def test_f2(self): + self.assertEqual(factor(18),set((1,2,3,6,9,18)),'eighteen') + + def test_f3(self): + self.assertEqual(factor(23),set((1,23)),'twenty-three') + + def test_cf1(self): + self.assertEqual(commonfactor(12,18),[1,2,3,6],'twelve-eighteen') + + def test_cf2(self): + self.assertEqual(commonfactor(18,23),[1],'twelve-twentythree') + +unittest.main() diff --git a/challenge-082/roger-bell-west/python/ch-2.py b/challenge-082/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4b88c1acf0 --- /dev/null +++ b/challenge-082/roger-bell-west/python/ch-2.py @@ -0,0 +1,37 @@ +#! /usr/bin/python3 + +import unittest +from collections import deque + +def isinterleave(a,b,c): + l=list(map(len,(a,b,c))) + buf=deque() + buf.append([0,0,0]) + while (buf): + n=buf.popleft() + if (n[2] >= l[2]): + return 1 + if ((n[0] < l[0]) and (a[n[0]] == c[n[2]])): + buf.append([n[0]+1,n[1],n[2]+1]) + if ((n[1] < l[1]) and (b[n[1]] == c[n[2]])): + buf.append([n[0],n[1]+1,n[2]+1]) + return 0 + +class TestInterleave(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(isinterleave('XY','X','XXY'),1,'example 1') + + def test_ex2(self): + self.assertEqual(isinterleave('XXY','XXZ','XXXXZY'),1,'example 2') + + def test_ex3(self): + self.assertEqual(isinterleave('YX','X','XXY'),0,'example 3') + + def test_ex4(self): + self.assertEqual(isinterleave('ACF','BDEG','ABCDEFG'),1,'example 4') + + def test_ex5(self): + self.assertEqual(isinterleave('ACF','BDGE','ABCDEFG'),0,'example 5') + +unittest.main() diff --git a/challenge-082/roger-bell-west/raku/ch-1.p6 b/challenge-082/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..01207a3e42 --- /dev/null +++ b/challenge-082/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,32 @@ +#! /usr/bin/perl6 + +use Test; + +plan 5; + +is-deeply(factor(12),SetHash.new(1,2,3,4,6,12),'twelve'); +is-deeply(factor(18),SetHash.new(1,2,3,6,9,18),'eighteen'); +is-deeply(factor(23),SetHash.new(1,23),'twenty-three'); + +is-deeply(commonfactor(12,18),(1,2,3,6),'twelve-eighteen'); +is-deeply(commonfactor(18,23),(1,),'twelve-twentythree'); + +sub factor($n) { + my $o=SetHash.new(1,$n); + for 2..floor(sqrt($n)) -> $i { + if ($n % $i == 0) { + $o{Int.new($n/$i)}++; + $o{$i}++; + } + } + return $o; +} + +sub commonfactor { + my @f=map {factor($_)},@_; + my $s=shift @f; + while (@f) { + $s (&)= shift @f; + } + return $s.keys.sort; +} diff --git a/challenge-082/roger-bell-west/raku/ch-2.p6 b/challenge-082/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..8d468d02a3 --- /dev/null +++ b/challenge-082/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,31 @@ +#! /usr/bin/perl6 + +use Test; + +plan 5; + +is(isinterleave('XY','X','XXY'),1,'example 1'); +is(isinterleave('XXY','XXZ','XXXXZY'),1,'example 2'); +is(isinterleave('YX','X','XXY'),0,'example 3'); +is(isinterleave('ACF','BDEG','ABCDEFG'),1,'example 4'); +is(isinterleave('ACF','BDGE','ABCDEFG'),0,'example 5'); + +sub isinterleave { + my ($a,$b,$c)=@_; + my @s=(map {[$_.comb]}, ($a,$b,$c)); + my @l=map {$_.end},@s; + my @buf=([0,0,0],); + while (@buf) { + my $n=shift @buf; + if ($n.[2] > @l[2]) { + return 1; + } + if ($n.[0] <= @l[0] && @s[0][$n.[0]] eq @s[2][$n.[2]]) { + push @buf,[$n.[0]+1,$n.[1],$n.[2]+1]; + } + if ($n.[1] <= @l[1] && @s[1][$n.[1]] eq @s[2][$n.[2]]) { + push @buf,[$n.[0],$n.[1]+1,$n.[2]+1]; + } + } + return 0; +} diff --git a/challenge-082/roger-bell-west/ruby/ch-1.rb b/challenge-082/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..309176d51c --- /dev/null +++ b/challenge-082/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,49 @@ +#! /usr/bin/ruby + +require 'set' + +def factor(n) + o=Set.new([1,n]) + 2.upto(Math.sqrt(n).floor) do |i| + if (n % i == 0) + o.add(n/i) + o.add(i) + end + end + return o +end + +def commonfactor(*fs) + f=fs.map{|x| factor(x)} + s=f.pop + while (!f.empty?) + s = s & f.pop + end + return s.sort +end + +require 'test/unit' + +class TestCf < Test::Unit::TestCase + + def test_f1 + assert_equal(Set.new([1,2,3,4,6,12]),factor(12)) + end + + def test_f2 + assert_equal(Set.new([1,2,3,6,9,18]),factor(18)) + end + + def test_f3 + assert_equal(Set.new([1,23]),factor(23)) + end + + def test_cf1 + assert_equal([1,2,3,6],commonfactor(12,18)) + end + + def test_cf2 + assert_equal([1],commonfactor(18,23)) + end + +end diff --git a/challenge-082/roger-bell-west/ruby/ch-2.rb b/challenge-082/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..067bddb305 --- /dev/null +++ b/challenge-082/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,45 @@ +#! /usr/bin/ruby + +def isinterleave(a,b,c) + l=[a,b,c].map{|x| x.length} + buf=[[0,0,0]] + while (!buf.empty?) + n=buf.pop + if (n[2] >= l[2]) + return 1 + end + if ((n[0] < l[0]) and (a[n[0]] == c[n[2]])) + buf.push([n[0]+1,n[1],n[2]+1]) + end + if ((n[1] < l[1]) and (b[n[1]] == c[n[2]])) + buf.push([n[0],n[1]+1,n[2]+1]) + end + end + return 0 +end + +require 'test/unit' + +class TestInterleave < Test::Unit::TestCase + + def test_ex1 + assert_equal(isinterleave('XY','X','XXY'),1) + end + + def test_ex2 + assert_equal(isinterleave('XXY','XXZ','XXXXZY'),1) + end + + def test_ex3 + assert_equal(isinterleave('YX','X','XXY'),0) + end + + def test_ex4 + assert_equal(isinterleave('ACF','BDEG','ABCDEFG'),1) + end + + def test_ex5 + assert_equal(isinterleave('ACF','BDGE','ABCDEFG'),0) + end + +end diff --git a/challenge-082/sgreen/README.md b/challenge-082/sgreen/README.md index fde0cedfbd..669247f1fc 100644 --- a/challenge-082/sgreen/README.md +++ b/challenge-082/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 081 +# The Weekly Challenge 082 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/the-weekly-challenge-081-1jje) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-082-3a9d) diff --git a/challenge-082/sgreen/blog.txt b/challenge-082/sgreen/blog.txt new file mode 100644 index 0000000000..1d83fd2f39 --- /dev/null +++ b/challenge-082/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-082-3a9d |
