diff options
| -rw-r--r-- | challenge-082/bob-lied/README | 4 | ||||
| -rwxr-xr-x | challenge-082/bob-lied/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-082/bob-lied/perl/ch-2.pl | 78 | ||||
| -rw-r--r-- | challenge-082/bob-lied/perl/t/input | 13 |
4 files changed, 145 insertions, 15 deletions
diff --git a/challenge-082/bob-lied/README b/challenge-082/bob-lied/README index e698fa656a..4c6cdb786b 100644 --- a/challenge-082/bob-lied/README +++ b/challenge-082/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 81 by Bob Lied. +Solutions to weekly challenge 82 by Bob Lied. -https://perlweeklychallenge.org/blog/perl-weekly-challenge-081/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/ diff --git a/challenge-082/bob-lied/perl/ch-1.pl b/challenge-082/bob-lied/perl/ch-1.pl new file mode 100755 index 0000000000..be31dbbd5f --- /dev/null +++ b/challenge-082/bob-lied/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2020, Bob Lied +#============================================================================= +# Perl Weekly Challenge 082 Task #1 > Common Factors +#============================================================================= +# Your are given two positive numbers, $M and $N. +# Write a script to list all common factors of the given numbers + +use strict; +use warnings; +use v5.30; + +use feature qw/ signatures /; +no warnings qw/ experimental::signatures /; + +use Getopt::Long; + +sub Usage { "Usage: $0 M N" }; + +my $Verbose = 0; +GetOptions('verbose' => \$Verbose); + +my ($M, $N) = @ARGV; + +die Usage() unless $M && $N; +die Usage() unless $M > 0 && $N > 0; + +sub factor($n) +{ + my $f = 2; + my %factor = (1 => 1, $n => 1); + my $sqrtf = int(sqrt($n)); + while ( $f <= $sqrtf ) + { + if ( ($n % $f) == 0 ) + { + my $otherf = $n / $f; + $factor{$f} = $factor{$otherf} = 1; + } + $f++; + } + + return sort { $a <=> $b } keys %factor; +} + +sub both($m, $n) +{ + my (%union, %intersection); + foreach my $e ( @$m, @$n) + { + $union{$e}++ && $intersection{$e}++; + } + return sort { $a <=> $b } keys %intersection; +} + +my @fM = factor($M); +my @fN = factor($N); + +my @same = both(\@fM, \@fN); + +say "(", join(", ", @same), ")"; diff --git a/challenge-082/bob-lied/perl/ch-2.pl b/challenge-082/bob-lied/perl/ch-2.pl new file mode 100755 index 0000000000..e42b4032eb --- /dev/null +++ b/challenge-082/bob-lied/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl +#============================================================================= +# Copyright (c) 2020, Bob Lied +#============================================================================= +# Perl Weekly Challenge 082 Task #2 > Interleave String +#============================================================================= +# 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 + +use strict; +use warnings; +use v5.30; + +use feature qw/ signatures /; +no warnings qw/ experimental::signatures /; + +use Getopt::Long; + +sub Usage { "Usage: $0 strA strB strC" }; + +my $Verbose = 0; +GetOptions('verbose' => \$Verbose); + +my ($A, $B, $C) = @ARGV; + +die Usage() unless $A && $B && $C; + +sub inter($s, $t, $c, $depth) +{ + #say "$depth: Enter [@$s] [@$t] [@$c]"; + # For each prefix of s that matches c + my $longestPrefix = 0; + my $lenS = $#$s; + my $lenC = $#$c; + for ( my $i = 0; $i <= $lenS && $i <= $lenC && $s->[$i] eq $c->[$i] ; $i++ ) + { + $longestPrefix++; + } + return 0 unless $longestPrefix; + my $lastS = $#{$s}; + my $lastC = $#{$c}; + for my $len ( 1 .. $longestPrefix ) + { + #say "depth: Try s[0..$len] = '", join("", @{$s}[0..$len-1]), "' against [@$c]"; + my @shorterS = ( @{$s}[ $len .. $lastS ] ); + my @shorterC = ( @{$c}[ $len .. $lastC ] ); + + return 1 if ( ! @shorterC ); + + # Swap strings to check for interleaving + return 1 if inter($t, \@shorterS, \@shorterC, $depth+1); + } + return 0; +} + +my @A = split "", $A; +my @B = split "", $B; +my @C = split "", $C; + +say inter(\@A, \@B, \@C, 0) || inter(\@B, \@A, \@C, 0); diff --git a/challenge-082/bob-lied/perl/t/input b/challenge-082/bob-lied/perl/t/input deleted file mode 100644 index 5905c36971..0000000000 --- a/challenge-082/bob-lied/perl/t/input +++ /dev/null @@ -1,13 +0,0 @@ -West Side Story - -The award-winning adaptation of the classic romantic tragedy "Romeo and -Juliet". The feuding families become two warring New York City gangs, the -white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred -escalates to a point where neither can coexist with any form of understanding. -But when Riff's best friend (and former Jet) Tony and Bernardo's younger -sister Maria meet at a dance, no one can do anything to stop their love. Maria -and Tony begin meeting in secret, planning to run away. Then the Sharks and -Jets plan a rumble under the highway--whoever wins gains control of the -streets. Maria sends Tony to stop it, hoping it can end the violence. It goes -terribly wrong, and before the lovers know what's happened, tragedy strikes -and doesn't stop until the climactic and heartbreaking ending. |
