aboutsummaryrefslogtreecommitdiff
path: root/challenge-082
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-10-19 16:57:36 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-10-19 16:57:36 +1100
commitc1c56909fc0b2ccf5fbcc7363b3378470a73d783 (patch)
treed460185b78d3ad9c3180b2ca79c5aff292155317 /challenge-082
parentc83ed6980345c9941b75df3c2884c3007befb2e3 (diff)
parentd7cc7d8fd62bb12e0129ac28c7c0bc211f560397 (diff)
downloadperlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.tar.gz
perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.tar.bz2
perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-082')
-rw-r--r--challenge-082/adam-russell/blog.txt1
-rw-r--r--challenge-082/adam-russell/perl/ch-1.pl30
-rw-r--r--challenge-082/adam-russell/perl/ch-2.pl34
-rw-r--r--challenge-082/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-082/arne-sommer/raku/ch-1.p616
-rwxr-xr-xchallenge-082/arne-sommer/raku/ch-2.p629
-rwxr-xr-xchallenge-082/arne-sommer/raku/common-factors16
-rwxr-xr-xchallenge-082/arne-sommer/raku/interleave-string29
-rw-r--r--challenge-082/athanasius/perl/ch-1.pl161
-rw-r--r--challenge-082/athanasius/perl/ch-2.pl217
-rw-r--r--challenge-082/athanasius/raku/ch-1.raku145
-rw-r--r--challenge-082/athanasius/raku/ch-2.raku214
-rw-r--r--challenge-082/bob-lied/README4
-rwxr-xr-xchallenge-082/bob-lied/perl/ch-1.pl65
-rwxr-xr-xchallenge-082/bob-lied/perl/ch-2.pl78
-rw-r--r--challenge-082/bob-lied/perl/t/input13
-rw-r--r--challenge-082/cheok-yin-fung/perl/ch-1.pl23
-rw-r--r--challenge-082/cheok-yin-fung/perl/ch-2.pl55
-rw-r--r--challenge-082/colin-crain/blog.txt1
-rw-r--r--challenge-082/colin-crain/perl/ch-1.pl86
-rw-r--r--challenge-082/colin-crain/perl/ch-2.pl124
-rw-r--r--challenge-082/colin-crain/raku/ch-1.raku77
-rw-r--r--challenge-082/colin-crain/raku/ch-2.raku75
-rwxr-xr-xchallenge-082/cristian-heredia/perl/ch-1.pl78
-rw-r--r--challenge-082/dave-cross/perl/ch-1.pl19
-rw-r--r--challenge-082/dave-cross/perl/ch-2.pl22
-rw-r--r--challenge-082/ddobbelaere/README1
-rw-r--r--challenge-082/ddobbelaere/perl/ch-1.pl68
-rw-r--r--challenge-082/ddobbelaere/perl/ch-2.pl78
-rw-r--r--challenge-082/duncan-c-white/README101
-rwxr-xr-xchallenge-082/duncan-c-white/perl/ch-1.pl66
-rwxr-xr-xchallenge-082/duncan-c-white/perl/ch-2.pl86
-rwxr-xr-xchallenge-082/e-choroba/perl5/ch-1.pl15
-rw-r--r--challenge-082/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-082/jaldhar-h-vyas/perl/ch-1.pl31
-rwxr-xr-xchallenge-082/jaldhar-h-vyas/perl/ch-2.pl37
-rwxr-xr-xchallenge-082/jaldhar-h-vyas/raku/ch-1.p69
-rwxr-xr-xchallenge-082/jaldhar-h-vyas/raku/ch-2.p622
-rwxr-xr-xchallenge-082/jluis/perl/ch-1.pl65
-rwxr-xr-xchallenge-082/jluis/perl/ch-2.pl42
-rwxr-xr-xchallenge-082/jo-37/perl/ch-1.pl36
-rwxr-xr-xchallenge-082/jo-37/perl/ch-2.pl77
-rw-r--r--challenge-082/juliodcs/perl/ch-2.pl37
-rw-r--r--challenge-082/juliodcs/raku/ch-2.raku52
-rw-r--r--challenge-082/lars-thegler/perl/ch-1.pl58
-rw-r--r--challenge-082/lars-thegler/perl/ch-2.pl57
-rw-r--r--challenge-082/lars-thegler/perl/input3
-rw-r--r--challenge-082/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-082/laurent-rosenfeld/perl/ch-1.pl21
-rw-r--r--challenge-082/laurent-rosenfeld/raku/ch-1.raku8
-rw-r--r--challenge-082/lubos-kolouch/perl/ch-2.pl57
-rw-r--r--challenge-082/lubos-kolouch/python/ch-2.py47
-rw-r--r--challenge-082/mark-anderson/raku/ch-1.p66
-rw-r--r--challenge-082/nunovieira220/js/ch-1.js13
-rw-r--r--challenge-082/nunovieira220/js/ch-2.js38
-rw-r--r--challenge-082/nunovieira220/perl/ch-1.pl18
-rw-r--r--challenge-082/nunovieira220/perl/ch-2.pl39
-rw-r--r--challenge-082/pete-houston/perl/ch-1.pl38
-rw-r--r--challenge-082/pete-houston/perl/ch-2.pl50
-rw-r--r--challenge-082/pkmnx/README1
-rw-r--r--challenge-082/pkmnx/gnat/ch_1.adb28
-rwxr-xr-xchallenge-082/pkmnx/raku/ch-1.raku9
-rwxr-xr-xchallenge-082/pkmnx/raku/ch-2.raku30
-rw-r--r--challenge-082/polettix/blog2.txt1
-rw-r--r--challenge-082/polettix/perl/ch-2.pl29
-rw-r--r--challenge-082/richard-park/apl/CommonFactors.aplf1
-rw-r--r--challenge-082/richard-park/apl/InterleaveString.aplf6
-rw-r--r--challenge-082/richard-park/apl/ch-1.aplf1
-rw-r--r--challenge-082/richard-park/apl/ch-2.aplf6
-rw-r--r--challenge-082/walt-mankowski/blog.txt1
-rw-r--r--challenge-082/walt-mankowski/perl/ch-1.pl29
-rw-r--r--challenge-082/walt-mankowski/perl/ch-2.pl32
-rw-r--r--challenge-082/walt-mankowski/python/ch-1.py13
-rw-r--r--challenge-082/walt-mankowski/python/ch-2.py23
-rw-r--r--challenge-082/wambash/raku/ch-1.raku19
-rw-r--r--challenge-082/wambash/raku/ch-2.raku44
-rw-r--r--challenge-082/wanderdoc/perl/ch-1.pl48
77 files changed, 3111 insertions, 101 deletions
diff --git a/challenge-082/adam-russell/blog.txt b/challenge-082/adam-russell/blog.txt
new file mode 100644
index 0000000000..2c0a23f34b
--- /dev/null
+++ b/challenge-082/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/2020/10/18#pwc082
diff --git a/challenge-082/adam-russell/perl/ch-1.pl b/challenge-082/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..a9bdce7ab6
--- /dev/null
+++ b/challenge-082/adam-russell/perl/ch-1.pl
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+##
+# You are given 2 positive numbers $M and $N.
+# Write a script to list all common factors of the given numbers.
+##
+sub factor{
+ my($n) = @_;
+ my @factors = (1);
+ foreach my $j (2..sqrt($n)){
+ push @factors, $j if $n % $j == 0;
+ push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n;
+ }
+ return @factors;
+}
+
+sub common_factors{
+ my($m, $n) = @_;
+ my @common_factors = grep { my $f = $_; grep { $f == $_ } @{$n}} @{$m};
+ return @common_factors;
+}
+
+
+MAIN:{
+ my $M = 12;
+ my $N = 18;
+ my @m_factors = factor($M);
+ my @n_factors = factor($N);
+ print "(" . join(",", common_factors(\@m_factors, \@n_factors)) . ")\n";
+} \ No newline at end of file
diff --git a/challenge-082/adam-russell/perl/ch-2.pl b/challenge-082/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..9a728fc056
--- /dev/null
+++ b/challenge-082/adam-russell/perl/ch-2.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+##
+# You are given 3 strings; $A, $B and $C.
+# Write a script to check if $C is created by interleave $A and $B.
+# Print 1 if check is success otherwise 0.
+##
+sub find_remove{
+ my($s, $x) = @_;
+ my $i = index($s, $x);
+ if($i != -1){
+ substr $s, $i, length($x), "";
+ return $s;
+ }
+ return undef;
+}
+MAIN:{
+ my $A = "XY";
+ my $B = "X";
+ my $C = "XXY";
+ my $s = find_remove($C, $A);
+ if($s && $s eq $B){
+ print "1\n";
+ exit;
+ }
+ else{
+ $s = find_remove($C, $B);
+ if($s && $s eq $A){
+ print "1\n";
+ exit;
+ }
+ }
+ print "0\n";
+} \ No newline at end of file
diff --git a/challenge-082/arne-sommer/blog.txt b/challenge-082/arne-sommer/blog.txt
new file mode 100644
index 0000000000..d001e4c296
--- /dev/null
+++ b/challenge-082/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/interleaved-factors.html
diff --git a/challenge-082/arne-sommer/raku/ch-1.p6 b/challenge-082/arne-sommer/raku/ch-1.p6
new file mode 100755
index 0000000000..99d637795d
--- /dev/null
+++ b/challenge-082/arne-sommer/raku/ch-1.p6
@@ -0,0 +1,16 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $M where $M > 0, Int $N where $N > 0, :i(:$include-self), :v(:$verbose));
+
+my @M-factors = $include-self ?? (1..$M).grep({ $M %% $_ }) !! (1..$M/2).grep({ $M %% $_ });
+my @N-factors = $include-self ?? (1..$N).grep({ $N %% $_ }) !! (1..$N/2).grep({ $N %% $_ });
+
+if $verbose
+{
+ say "Factors of $M: { @M-factors.join(", ") }";
+ say "Factors of $N: { @N-factors.join(", ") }";
+}
+
+my %common = @M-factors (&) @N-factors;
+
+say "(" ~ %common.keys.sort.join(", ") ~ ")"; \ No newline at end of file
diff --git a/challenge-082/arne-sommer/raku/ch-2.p6 b/challenge-082/arne-sommer/raku/ch-2.p6
new file mode 100755
index 0000000000..8bf24a3abd
--- /dev/null
+++ b/challenge-082/arne-sommer/raku/ch-2.p6
@@ -0,0 +1,29 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Str $A where $A.chars > 0,
+ Str $B where $B.chars > 0,
+ Str $C where $C.chars > 0,
+ :v(:$verbose));
+
+say do_check($A, $B, $C) ?? '1' !! '0';
+
+sub do_check ($A, $B, $C)
+{
+ say ": Checking A:$A | B:$B | C:$C" if $verbose;
+
+ return 1 if $C eq "";
+
+ if $C.contains($A)
+ {
+ my $c = $C.subst($A);
+ return do_check($A, $B, $c);
+ }
+
+ if $C.contains($B)
+ {
+ my $c = $C.subst($B);
+ return do_check($A, $B, $c);
+ }
+}
+
+
diff --git a/challenge-082/arne-sommer/raku/common-factors b/challenge-082/arne-sommer/raku/common-factors
new file mode 100755
index 0000000000..99d637795d
--- /dev/null
+++ b/challenge-082/arne-sommer/raku/common-factors
@@ -0,0 +1,16 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $M where $M > 0, Int $N where $N > 0, :i(:$include-self), :v(:$verbose));
+
+my @M-factors = $include-self ?? (1..$M).grep({ $M %% $_ }) !! (1..$M/2).grep({ $M %% $_ });
+my @N-factors = $include-self ?? (1..$N).grep({ $N %% $_ }) !! (1..$N/2).grep({ $N %% $_ });
+
+if $verbose
+{
+ say "Factors of $M: { @M-factors.join(", ") }";
+ say "Factors of $N: { @N-factors.join(", ") }";
+}
+
+my %common = @M-factors (&) @N-factors;
+
+say "(" ~ %common.keys.sort.join(", ") ~ ")"; \ No newline at end of file
diff --git a/challenge-082/arne-sommer/raku/interleave-string b/challenge-082/arne-sommer/raku/interleave-string
new file mode 100755
index 0000000000..8bf24a3abd
--- /dev/null
+++ b/challenge-082/arne-sommer/raku/interleave-string
@@ -0,0 +1,29 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Str $A where $A.chars > 0,
+ Str $B where $B.chars > 0,
+ Str $C where $C.chars > 0,
+ :v(:$verbose));
+
+say do_check($A, $B, $C) ?? '1' !! '0';
+
+sub do_check ($A, $B, $C)
+{
+ say ": Checking A:$A | B:$B | C:$C" if $verbose;
+
+ return 1 if $C eq "";
+
+ if $C.contains($A)
+ {
+ my $c = $C.subst($A);
+ return do_check($A, $B, $c);
+ }
+
+ if $C.contains($B)
+ {
+ my $c = $C.subst($B);
+ return do_check($A, $B, $c);
+ }
+}
+
+
diff --git a/challenge-082/athanasius/perl/ch-1.pl b/challenge-082/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..da267889a4
--- /dev/null
+++ b/challenge-082/athanasius/perl/ch-1.pl
@@ -0,0 +1,161 @@
+#!perl
+
+###############################################################################
+=comment
+
+Perl Weekly Challenge 082
+=========================
+
+Task #1
+-------
+*Common Factors*
+
+Submitted by: Niels van Dijke
+
+You are given 2 positive numbers $M and $N.
+
+Write a script to list all common factors of the given numbers.
+
+Example 1:
+
+ Input:
+ $M = 12
+ $N = 18
+
+ Output:
+ (1, 2, 3, 6)
+
+ Explanation:
+ Factors of 12: 1, 2, 3, 4, 6
+ Factors of 18: 1, 2, 3, 6, 9
+
+Example 2:
+
+ Input:
+ $M = 18
+ $N = 23
+
+ Output:
+ (1)
+
+ Explanation:
+ Factors of 18: 1, 2, 3, 6, 9
+ Factors of 23: 1
+
+=cut
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+#==============================================================================
+=comment
+
+Is X a factor of X? In other words, is the "divides" relation reflexive? It is
+usually thought so -- see https://en.wikipedia.org/wiki/Divisor -- but the
+Examples in the Task description imply not. This leads to some anomalies, e.g.,
+if 17 is not a factor of 17, then the only factor common to 17 and 34 is 1 and
+17 itself is excluded.
+
+In the solution given below, it is assumed that the divides relation IS reflex-
+ive; but this can be changed by setting the constant "REFLEXIVE" to zero.
+
+=cut
+#==============================================================================
+
+ # Exports:
+use strict;
+use warnings;
+use Const::Fast; # const()
+use Math::Prime::Util qw( divisors );
+use Regexp::Common qw( number ); # %RE{num}
+use Set::Scalar; # infix "*" (overloaded for set inter-
+ # section), members(), new()
+use constant
+{
+ REFLEXIVE => 1,
+ VERBOSE => 1,
+};
+
+const my $USAGE =>
+"Usage:
+ perl $0 <M> <N>
+
+ <M> First positive integer
+ <N> Second positive integer\n";
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 082, Task #1: Common Factors (Perl)\n\n";
+}
+
+#==============================================================================
+MAIN:
+#==============================================================================
+{
+ my ($M, $N) = parse_command_line();
+
+ printf "Input:\n \$M = %d\n \$N = %d\n\n", $M, $N;
+
+ my $M_factors = Set::Scalar->new( divisors($M) );
+ $M_factors->delete($M) unless REFLEXIVE;
+
+ my $N_factors = Set::Scalar->new( divisors($N) );
+ $N_factors->delete($N) unless REFLEXIVE;
+
+ my @common = sort { $a <=> $b } ($M_factors * $N_factors)->members;
+
+ printf "Output:\n (%s)\n", join ', ', @common;
+
+ explain($M, $N, $M_factors, $N_factors) if VERBOSE;
+}
+
+if (VERBOSE)
+{
+ #--------------------------------------------------------------------------
+ sub explain
+ #--------------------------------------------------------------------------
+ {
+ my ($M, $N, $M_factors, $N_factors) = @_;
+
+ my @M_factors = sort { $a <=> $b } @$M_factors;
+ my @N_factors = sort { $a <=> $b } @$N_factors;
+
+ my $w = length($M) >= length($N) ? length($M) : length($N);
+
+ print "\nExplanation:\n";
+ printf " Factors of %*d: %s\n", $w, $M, join(', ', @M_factors);
+ printf " Factors of %*d: %s\n\n", $w, $N, join(', ', @N_factors);
+
+ printf qq[ Note: the "is a factor of" (or "divides", or "|") ] .
+ qq[relation is here assumed\n %sto be reflexive\n],
+ REFLEXIVE ? '' : 'NOT ';
+ }
+}
+
+#------------------------------------------------------------------------------
+sub parse_command_line
+#------------------------------------------------------------------------------
+{
+ my $args = scalar @ARGV;
+ $args == 2 or die "ERROR: Expected 2 command-line arguments, found " .
+ "$args\n$USAGE";
+
+ for (@ARGV)
+ {
+ / \A $RE{num}{int} \z /x
+ or die "ERROR: Non-integer '$_'\n$USAGE";
+
+ $_ < 0 and die "ERROR: Negative integer '$_'\n$USAGE";
+
+ $_ == 0 and die "ERROR: Zero is not a \"positive\" integer\n$USAGE";
+ }
+
+ return @ARGV;
+}
+
+###############################################################################
diff --git a/challenge-082/athanasius/perl/ch-2.pl b/challenge-082/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..5b1f6440b7
--- /dev/null
+++ b/challenge-082/athanasius/perl/ch-2.pl
@@ -0,0 +1,217 @@
+#!perl
+
+###############################################################################
+=comment
+
+Perl Weekly Challenge 082
+=========================
+
+Task #2
+-------
+*Interleave String*
+
+Submitted by: Mohammad S Anwar
+
+You are given 3 strings; $A, $B and $C.
+
+Write a script to check if $C is created by interleave $A and $B.
+
+Print 1 if check is success otherwise 0.
+
+Example 1:
+
+ Input:
+ $A = "XY"
+ $B = "X"
+ $C = "XXY"
+
+ Output: 1
+
+EXPLANATION
+
+ "X" (from $B) + "XY" (from $A) = $C
+
+Example 2:
+
+ Input:
+ $A = "XXY"
+ $B = "XXZ"
+ $C = "XXXXZY"
+
+ Output: 1
+
+EXPLANATION
+
+ "XX" (from $A) + "XXZ" (from $B) + "Y" (from $A) = $C
+
+Example 3:
+
+ Input:
+ $A = "YX"
+ $B = "X"
+ $C = "XXY"
+
+ Output: 0
+
+=cut
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+#==============================================================================
+=comment
+