aboutsummaryrefslogtreecommitdiff
path: root/challenge-092
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@googlemail.com>2020-12-27 16:48:31 +0100
committerwanderdoc <wanderdoc@googlemail.com>2020-12-27 16:48:31 +0100
commit80d0275afdc698af5ed8888e9d94f94b058f9a1e (patch)
tree00d0fd5eba250eebe7c913fe9f5aa0cefb038efb /challenge-092
parent82534984de8058903af007fb76e8b1dbd7647fea (diff)
downloadperlweeklychallenge-club-80d0275afdc698af5ed8888e9d94f94b058f9a1e.tar.gz
perlweeklychallenge-club-80d0275afdc698af5ed8888e9d94f94b058f9a1e.tar.bz2
perlweeklychallenge-club-80d0275afdc698af5ed8888e9d94f94b058f9a1e.zip
Solutions to challenge-092.
Diffstat (limited to 'challenge-092')
-rw-r--r--challenge-092/wanderdoc/perl/ch-1.pl41
-rw-r--r--challenge-092/wanderdoc/perl/ch-2.pl75
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-092/wanderdoc/perl/ch-1.pl b/challenge-092/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..f3ff2501a0
--- /dev/null
+++ b/challenge-092/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given two strings $A and $B. Write a script to check if the given strings are Isomorphic. Print 1 if they are otherwise 0.
+Example 1: Input: $A = "abc"; $B = "xyz" Output: 1
+Example 2: Input: $A = "abb"; $B = "xyy" Output: 1
+Example 3: Input: $A = "sum"; $B = "add" Output: 0
+=cut
+
+
+
+
+
+
+use Test::More;
+
+sub is_isomorphic
+{
+ my ($str_1, $str_2) = @_;
+ return 0 if length($str_1) != length($str_2);
+ my %hash_1;
+
+ @hash_1{split(//,$str_1)} = split(//,$str_2);
+
+
+ my %hash_2 = reverse %hash_1;
+
+ return
+ scalar keys %hash_1 == scalar keys %hash_2 ? 1 : 0;
+}
+
+is_isomorphic(qw(abc xyz));
+
+is(is_isomorphic(qw(abc xyz)), 1, 'Example 1');
+is(is_isomorphic(qw(abb xyy)), 1, 'Example 2');
+is(is_isomorphic(qw(sum add)), 0, 'Example 3');
+
+
+done_testing(); \ No newline at end of file
diff --git a/challenge-092/wanderdoc/perl/ch-2.pl b/challenge-092/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..e31dbedc66
--- /dev/null
+++ b/challenge-092/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a set of sorted non-overlapping intervals and a new interval. Write a script to merge the new interval to the given set of intervals.
+Example 1: Input $S = (1,4), (8,10); $N = (2,6) Output: (1,6), (8,10)
+Example 2: Input $S = (1,2), (3,7), (8,10); $N = (5,8) Output: (1,2), (3,10)
+Example 3: Input $S = (1,5), (7,9); $N = (10,11) Output: (1,5), (7,9), (10,11)
+=cut
+
+
+
+
+
+
+
+
+use Bit::Vector;
+use List::Util qw(max);
+use Test::More;
+
+sub insert_interval
+{
+ my @intervals = @_;
+
+ @intervals = sort { $a->[0] <=> $b->[0] } @intervals;
+ my $max = max(map @$_, @intervals);
+
+ my @veclist = Bit::Vector->new($max + 1, scalar @intervals);
+ $veclist[$_]->Interval_Fill(@{$intervals[$_]}) for 0 .. $#veclist;
+
+ my @results;
+ my $res = $veclist[0]->Shadow();
+
+
+ for my $v ( @veclist )
+ {
+
+
+ if ($res->is_empty())
+ {
+ $res->Or($res, $v);
+ }
+ else
+ {
+ if ( $v->Min() <= $res->Max() )
+ {
+
+ $res->Or($res, $v);
+ }
+ else
+ {
+ my $res_p = $res->Clone();
+ push @results, [$res_p->Min(), $res_p->Max()];
+ $res->Empty();
+ $res->Or($res, $v);
+
+ }
+ }
+
+ }
+ push @results, [$res->Min(), $res->Max()];
+
+
+ return @results;
+
+}
+
+
+is_deeply([insert_interval([1,4], [8,10], [2,6])], [[1, 6], [8, 10]], 'Example 1');
+is_deeply([insert_interval([1,2], [3,7], [8,10], [5,8])], [[1, 2], [3, 10]], 'Example 2');
+is_deeply([insert_interval([1,5], [7,9], [10,11])], [[1,5], [7,9],[10,11]], 'Example 3');
+
+done_testing(); \ No newline at end of file