diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-19 16:57:36 +1100 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-19 16:57:36 +1100 |
| commit | c1c56909fc0b2ccf5fbcc7363b3378470a73d783 (patch) | |
| tree | d460185b78d3ad9c3180b2ca79c5aff292155317 /challenge-082 | |
| parent | c83ed6980345c9941b75df3c2884c3007befb2e3 (diff) | |
| parent | d7cc7d8fd62bb12e0129ac28c7c0bc211f560397 (diff) | |
| download | perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.tar.gz perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.tar.bz2 perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-082')
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 + |
