diff options
| -rw-r--r-- | challenge-082/cheok-yin-fung/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-082/cheok-yin-fung/perl/ch-2.pl | 55 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-082/cheok-yin-fung/perl/ch-1.pl b/challenge-082/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..793642231d --- /dev/null +++ b/challenge-082/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use strict; +use warnings; + +die "I need two positive integers!" if !$ARGV[1]; + +sub gcd{ #Euclidean Algorithm + my $a = $_[0]; + my $b = $_[1]; + + my $c = $a % $b; + + if ($c == 0) { + return $b; + } + else { + gcd($b,$c); + } +} + +#all common factors of $a, $b are factors of its greatest common factor +print join " ", grep { gcd($ARGV[0],$ARGV[1]) % $_ == 0} 1..$ARGV[0]; +print "\n"; diff --git a/challenge-082/cheok-yin-fung/perl/ch-2.pl b/challenge-082/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..5d2f8cc70b --- /dev/null +++ b/challenge-082/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More tests => 6; + +sub interleave { + return + interleave_one_side($_[0], $_[1], $_[2]) + || + interleave_one_side($_[1], $_[0], $_[2]) +} + +sub interleave_one_side { + my $A = $_[0]; + my $B = $_[1]; + my $C; + my $possibleinterleave = undef; + my $pos_AinC = 0; + do { + $C = $_[2]; #effect of substr modify $C + $pos_AinC = index($C,$A,$pos_AinC); + if ($pos_AinC != -1) { + substr($C, $pos_AinC, length $A, ""); + if ($C eq $B) { + $possibleinterleave = 1; + } + else { + $pos_AinC++; + } + } + } while ($pos_AinC != -1 and + $pos_AinC < length($C) and + !$possibleinterleave); + return $possibleinterleave; +} + +if ($ARGV[2]) { + if (interleave($ARGV[0], $ARGV[1], $ARGV[2])) { + print 1, "\n"; + } + else { + print -1, "\n"; + } +} +else { + die "I need three input parameters." +} + + +ok interleave("XY", "X", "XXY"), "example 1"; +ok interleave("XXY", "XXZ", "XXXXZY"), "example 2"; +ok !interleave("YX", "X", "XXY"), "example 3"; +ok interleave("alpha", "beta", "alphabeta"), "alphabeta"; +ok interleave("alphabeta", "alpha", "alphabealphata"), "alphabealphata"; +ok interleave("abcd", "ab", "abcabd"), "a..b"; |
