Created
October 18, 2020 05:40
-
-
Save adamcrussell/8af21134e61cb0f3277019d228d519d0 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 082
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment