aboutsummaryrefslogtreecommitdiff
path: root/challenge-092
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
committer冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
commit8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch)
treeae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-092
parent865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff)
parentc9aec2da6bcb04b488183f09ca94bee488557aff (diff)
downloadperlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.gz
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.bz2
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.zip
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
Diffstat (limited to 'challenge-092')
-rw-r--r--challenge-092/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-092/jaldhar-h-vyas/perl/ch-1.pl53
-rwxr-xr-xchallenge-092/jaldhar-h-vyas/perl/ch-2.pl45
-rwxr-xr-xchallenge-092/jaldhar-h-vyas/raku/ch-1.raku34
-rwxr-xr-xchallenge-092/jaldhar-h-vyas/raku/ch-2.raku32
-rw-r--r--challenge-092/paulo-custodio/basic/ch-1.bas47
-rw-r--r--challenge-092/paulo-custodio/basic/ch-2.bas66
-rw-r--r--challenge-092/paulo-custodio/perl/ch-1.pl69
-rw-r--r--challenge-092/paulo-custodio/perl/ch-2.pl100
-rw-r--r--challenge-092/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-092/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-092/paulo-custodio/test.pl18
-rwxr-xr-xchallenge-092/wlmb/perl/ch-1.pl2
-rwxr-xr-xchallenge-092/wlmb/perl/ch-2.pl2
14 files changed, 396 insertions, 103 deletions
diff --git a/challenge-092/jaldhar-h-vyas/blog.txt b/challenge-092/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..bd2b694bdc
--- /dev/null
+++ b/challenge-092/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2021/02/perl_weekly_challenge_week_92.html
diff --git a/challenge-092/jaldhar-h-vyas/perl/ch-1.pl b/challenge-092/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..0d79b4e8a7
--- /dev/null
+++ b/challenge-092/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+use English qw/ -no_match_vars /;
+
+sub usage {
+ print<<"-USAGE-";
+ $PROGRAM_NAME <A> <B>
+
+ <A> a string
+ <B> another string
+-USAGE-
+ exit 0;
+}
+
+sub isIsomorphic {
+ my ($A, $B) = @_;
+
+ if (length $A != length $B) {
+ return undef;
+ }
+
+ my @A = split //, $A;
+ my @B = split //, $B;
+ my %seen;
+ my %isomorphs;
+
+ for my $i (0 .. scalar @A - 1) {
+ my $a = $A[$i];
+ my $b = $B[$i];
+
+ if (exists $isomorphs{$a}) {
+ unless ($b eq $isomorphs{$a}) {
+ return undef;
+ }
+ } else {
+ if (!grep /$b/, keys %seen) {
+ $isomorphs{$a} = $b;
+ $seen{$b} = 1;
+ } else {
+ return undef;
+ }
+ }
+ }
+
+ return 1;
+}
+
+if (scalar @ARGV != 2) {
+ usage;
+}
+
+say isIsomorphic($ARGV[0], $ARGV[1]) ? 1 : 0;
diff --git a/challenge-092/jaldhar-h-vyas/perl/ch-2.pl b/challenge-092/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..e1f8940cc1
--- /dev/null
+++ b/challenge-092/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+use English qw/ -no_match_vars /;
+
+sub usage {
+ print<<"-USAGE-";
+ $PROGRAM_NAME [<S> ...]
+
+ [<S> ...] A set of sorted non-overlapping intervals enclosed in
+ parentheses and separated by commas. The last pair will
+ be merged into the rest.
+-USAGE-
+ exit 0;
+}
+
+sub toArray {
+ my ($arg) = @_;
+ $arg =~ /\( (\d+) , (\d+) \) /gmx;
+ return [$1, $2];
+}
+
+if (scalar @ARGV < 2) {
+ usage;
+}
+
+my @intervals = sort { $a->[0] <=> $b->[0] } map { toArray($_); } @ARGV;
+
+my $size = scalar @intervals;
+my @merged;
+
+for (my $i = 0; $i < $size; $i++) {
+ my $start = $intervals[$i]->[0];
+ my $end = $intervals[$i]->[1];
+
+ while ($i < $size - 1 &&
+ $end >= $intervals[$i + 1]->[0] && $end <= $intervals[$i + 1]->[1]) {
+ $end = $intervals[$i + 1]->[1];
+ $i++;
+ }
+
+ push @merged, [$start, $end];
+}
+
+say join ', ', map { "($_->[0],$_->[1])" } @merged; \ No newline at end of file
diff --git a/challenge-092/jaldhar-h-vyas/raku/ch-1.raku b/challenge-092/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..58808310a6
--- /dev/null
+++ b/challenge-092/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,34 @@
+#!/usr/bin/raku
+
+sub isIsomorphic(Str $A, Str $B) {
+ my SetHash of Str $seen;
+ my %isomorphs;
+
+ if ($A.chars != $B.chars) {
+ return False;
+ }
+
+ for $A.comb Z $B.comb -> ($a, $b) {
+ if %isomorphs{$a}:exists {
+ unless $b eq %isomorphs{$a} {
+ return False;
+ }
+ } else {
+ if $b ∉ $seen {
+ %isomorphs{$a} = $b;
+ $seen{$b}++;
+ } else {
+ return False;
+ }
+ }
+ }
+
+ return True;
+}
+
+sub MAIN(
+ Str $A, #= a string
+ Str $B #= another string
+ ) {
+ say isIsomorphic($A, $B) ?? 1 !! 0;
+} \ No newline at end of file
diff --git a/challenge-092/jaldhar-h-vyas/raku/ch-2.raku b/challenge-092/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..c00dad0973
--- /dev/null
+++ b/challenge-092/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/raku
+
+sub toArray(Str $arg) {
+ $arg ~~ m/ \( (\d+) \, (\d+) \) /;
+ return [$0.Int, $1.Int];
+}
+
+sub MAIN(
+ *@S #= A set of sorted non-overlapping intervals enclosed in
+ #= parentheses and separated by commas. The last pair will
+ #= be merged into the rest.
+ where { @S.elems > 1 }
+) {
+ my @intervals = @S.map( { toArray($_) } ).sort({@^a[0] <=> @^b[0]});
+
+ my $size = @intervals.elems;
+ my @merged;
+
+ loop (my $i = 0; $i < $size; $i++) {
+ my $start = @intervals[$i][0];
+ my $end = @intervals[$i][1];
+
+ while $i < $size - 1 && $end ~~ @intervals[$i + 1].minmax {
+ $end = @intervals[$i + 1][1];
+ $i++;
+ }
+
+ push @merged, [$start, $end];
+ }
+
+ @merged.map({ "[$_[0],$_[1]]"; }).join(q{, }).say;
+} \ No newline at end of file
diff --git a/challenge-092/paulo-custodio/basic/ch-1.bas b/challenge-092/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..1e042022ff
--- /dev/null
+++ b/challenge-092/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,47 @@
+' Challenge 092
+'
+' TASK #1 › Isomorphic Strings
+' Submitted by: Mohammad S Anwar
+' 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
+
+function isomorphic(a as string, b as string) as integer
+ dim mapping(256) as integer, mapped(256) as integer
+ dim i as integer, ac as integer, bc as integer
+
+ if a="" or len(a)<>len(b) then
+ isomorphic = 0: exit function
+ end if
+
+ for i=1 to len(a)
+ ac = asc(mid(a,i,1))
+ bc = asc(mid(b,i,1))
+ if mapping(ac)=0 then ' a is new
+ if mapped(bc)<>0 then ' b already mapped to some other a
+ isomorphic = 0: exit function
+ else ' store mapping
+ mapping(ac) = bc
+ mapped(bc) = 1
+ end if
+ else ' a already occurred
+ if mapping(ac)<>bc then ' previous mapping is different
+ isomorphic = 0: exit function
+ end if
+ end if
+ next
+ isomorphic = 1
+end function
+
+print trim(str(isomorphic(command(1), command(2)))) \ No newline at end of file
diff --git a/challenge-092/paulo-custodio/basic/ch-2.bas b/challenge-092/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..a974817a64
--- /dev/null
+++ b/challenge-092/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,66 @@
+' Challenge 092
+'
+' TASK #2 › Insert Interval
+' Submitted by: Mohammad S Anwar
+' 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)
+
+redim shared timeline(1) as Boolean
+
+sub fill_timeline()
+ dim i as integer, j as integer, p as integer, bg as integer, ed as integer
+ i=1
+ do while command(i)<>""
+ ' parse begin,end
+ p=instr(command(i),",")
+ if p=0 then error 5
+ bg=val(command(i))
+ ed=val(mid(command(i),p+1))
+ ' resize timeline if needed
+ if 2*ed>ubound(timeline) then
+ redim preserve timeline(2*ed+2)
+ end if
+
+ ' fill interval
+ for j=2*bg to 2*ed
+ timeline(j)=true
+ next
+ i=i+1
+ loop
+end sub
+
+sub print_timeline()
+ dim i as integer
+ redim intervals(0) as integer
+
+ for i=lbound(timeline) to ubound(timeline)-1
+ if timeline(i)=false and timeline(i+1)=true then
+ redim preserve intervals(ubound(intervals)+1)
+ intervals(ubound(intervals))=int(i/2)+1
+ elseif timeline(i)=true and timeline(i+1)=false then
+ redim preserve intervals(ubound(intervals)+1)
+ intervals(ubound(intervals))=int(i/2)
+ end if
+ next
+
+ for i=1 to ubound(intervals) step 2
+ print "(";trim(str(intervals(i)));",";trim(str(intervals(i+1)));")";
+ if i+2<ubound(intervals) then print ", ";
+ next
+ print
+end sub
+
+' main
+fill_timeline
+print_timeline
diff --git a/challenge-092/paulo-custodio/perl/ch-1.pl b/challenge-092/paulo-custodio/perl/ch-1.pl
index 163e7d530b..49a153e10b 100644
--- a/challenge-092/paulo-custodio/perl/ch-1.pl
+++ b/challenge-092/paulo-custodio/perl/ch-1.pl
@@ -1,13 +1,14 @@
#!/usr/bin/perl
# Challenge 092
-#
+#
# TASK #1 › Isomorphic Strings
# Submitted by: Mohammad S Anwar
# 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.
-#
+#
+# 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
@@ -25,34 +26,34 @@ use 5.030;
say isomorphic(@ARGV);
sub isomorphic {
- my($a, $b) = @_;
-
- # both strings must be the same size
- if (!defined($a) || !defined($b) || length($a) != length($b)) {
- return 0;
- }
-
- # convert each string to a list and check the mapping
- my(%mapping, %mapped);
- my @a = split(//, $a);
- my @b = split(//, $b);
- while (@a) {
- my $a = shift @a;
- my $b = shift @b;
- if (!$mapping{$a}) { # a is new
- if ($mapped{$b}) { # b already mapped to some other a
- return 0;
- }
- else { # store mapping
- $mapping{$a} = $b;
- $mapped{$b} = 1;
- }
- } # a already occurred
- else {
- if ($mapping{$a} ne $b) { # previous mapping is different
- return 0;
- }
- }
- }
- return 1;
+ my($a, $b) = @_;
+
+ # both strings must be the same size
+ if (!defined($a) || !defined($b) || length($a) != length($b)) {
+ return 0;
+ }
+
+ # convert each string to a list and check the mapping
+ my(%mapping, %mapped);
+ my @a = split(//, $a);
+ my @b = split(//, $b);
+ while (@a) {
+ my $a = shift @a;
+ my $b = shift @b;
+ if (!$mapping{$a}) { # a is new
+ if ($mapped{$b}) { # b already mapped to some other a
+ return 0;
+ }
+ else { # store mapping
+ $mapping{$a} = $b;
+ $mapped{$b} = 1;
+ }
+ } # a already occurred
+ else {
+ if ($mapping{$a} ne $b) { # previous mapping is different
+ return 0;
+ }
+ }
+ }
+ return 1;
}
diff --git a/challenge-092/paulo-custodio/perl/ch-2.pl b/challenge-092/paulo-custodio/perl/ch-2.pl
index bfbdd28249..c21ff45f55 100644
--- a/challenge-092/paulo-custodio/perl/ch-2.pl
+++ b/challenge-092/paulo-custodio/perl/ch-2.pl
@@ -1,13 +1,13 @@
#!/usr/bin/perl
# Challenge 092
-#
+#
# TASK #2 › Insert Interval
# Submitted by: Mohammad S Anwar
# 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)
@@ -22,69 +22,69 @@ use strict;
use warnings;
use 5.030;
-my @intervals; # set of all intervals
+my @intervals; # set of all intervals
add_interval(parse($_)) for @ARGV;
print_intervals();
# convert a string "(a,b)" into [a,b]
sub parse {
- my($text) = @_;
- my($a, $b) = ($text =~ /(\d+)\D+(\d+)/) or die "invalid interval: $text\n";
- return ($a, $b);
+ my($text) = @_;
+ my($a, $b) = ($text =~ /(\d+)\D+(\d+)/) or die "invalid interval: $text\n";
+ return ($a, $b);
}
# add a new interval in order, merge if overlapping
sub add_interval {
- my($s, $e) = @_;
- add_interval_1(sort {$a <=> $b} ($s, $e));
- merge_intervals();
+ my($s, $e) = @_;
+ add_interval_1(sort {$a <=> $b} ($s, $e));
+ merge_intervals();
}
sub add_interval_1 {
- my($a, $b) = @_;
- if (!@intervals) { # first interval
- push @intervals, [$a, $b];
- }
- else {
- for (my $i = 0; $i < @intervals; $i++) {
- my $this = $intervals[$i];
- if ($b < $this->[0]) { # before, not overlapping
- splice(@intervals, $i, 0, [$a, $b]);
- return;
- }
- elsif ($b >= $this->[0] && $b < $this->[1]) { # end within this interval
- if ($a < $this->[0]) { # merge start
- $this->[0] = $a;
- }
- return;
- }
- elsif ($b >= $this->[1] && $a < $this->[1]) { # end after inteval, start within
- $this->[1] = $b;
- if ($a < $this->[0]) { # merge start
- $this->[0] = $a;
- }
- return;
- }
- }
- push @intervals, [$a, $b]; # append to end
- }
+ my($a, $b) = @_;
+ if (!@intervals) { # first interval
+ push @intervals, [$a, $b];
+ }
+ else {
+ for (my $i = 0; $i < @intervals; $i++) {
+ my $this = $intervals[$i];
+ if ($b < $this->[0]) { # before, not overlapping
+ splice(@intervals, $i, 0, [$a, $b]);
+ return;
+ }
+ elsif ($b >= $this->[0] && $b < $this->[1]) { # end within this interval
+ if ($a < $this->[0]) { # merge start
+ $this->[0] = $a;
+ }
+ return;
+ }
+ elsif ($b >= $this->[1] && $a < $this->[1]) { # end after inteval, start within
+ $this->[1] = $b;
+ if ($a < $this->[0]) { # merge start
+ $this->[0] = $a;
+ }
+ return;
+ }
+ }
+ push @intervals, [$a, $b]; # append to end
+ }
}
sub merge_intervals {
- for (my $i = 0; $i+1 < @intervals; $i++) {
- while ($i+1 < @intervals) {
- my $this = $intervals[$i];
- my $next = $intervals[$i+1];
- if ($this->[1] < $next->[0]) { # not overlapping
- last; # next interval
- }
- else {
- splice(@intervals, $i, 2, [$this->[0], $next->[1]]); # merge and test again
- }
- }
- }
+ for (my $i = 0; $i+1 < @intervals; $i++) {
+ while ($i+1 < @intervals) {
+ my $this = $intervals[$i];
+ my $next = $intervals[$i+1];
+ if ($this->[1] < $next->[0]) { # not overlapping
+ last; # next interval
+ }
+ else {
+ splice(@intervals, $i, 2, [$this->[0], $next->[1]]); # merge and test again
+ }
+ }
+ }
}
sub print_intervals {
- say join(", ", map {"(".$_->[0].",".$_->[1].")"} @intervals);
+ say join(", ", map {"(".$_->[0].",".$_->[1].")"} @intervals);
}
diff --git a/challenge-092/paulo-custodio/t/test-1.yaml b/challenge-092/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..fadc975fa6
--- /dev/null
+++ b/challenge-092/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: abc xyz
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: abb xyy
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: sum add
+ input:
+ output: 0
diff --git a/challenge-092/paulo-custodio/t/test-2.yaml b/challenge-092/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..97da8a27bb
--- /dev/null
+++ b/challenge-092/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1,4 8,10 2,6
+ input:
+ output: (1,6), (8,10)
+- setup:
+ cleanup:
+ args: 1,2 3,7 8,10 5,8
+ input:
+ output: (1,2), (3,10)
+- setup:
+ cleanup:
+ args: 1,5 7,9 10,11
+ input:
+ output: (1,5), (7,9), (10,11)
diff --git a/challenge-092/paulo-custodio/test.pl b/challenge-092/paulo-custodio/test.pl
index 51e9f3e6e9..01ed2b83cd 100644
--- a/challenge-092/paulo-custodio/test.pl
+++ b/challenge-092/paulo-custodio/test.pl
@@ -2,22 +2,6 @@
use strict;
use warnings;
-use Test::More;
use 5.030;
-is capture("perl perl/ch-1.pl abc xyz"), "1\n";
-is capture("perl perl/ch-1.pl abb xyy"), "1\n";
-is capture("perl perl/ch-1.pl sum add"), "0\n";
-
-is capture("perl perl/ch-2.pl 1,4 8,10 2,6"), "(1,6), (8,10)\n";
-is capture("perl perl/ch-2.pl 1,2 3,7 8,10 5,8"), "(1,2), (3,10)\n";
-is capture("perl perl/ch-2.pl 1,5 7,9 10,11"), "(1,5), (7,9), (10,11)\n";
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-092/wlmb/perl/ch-1.pl b/challenge-092/wlmb/perl/ch-1.pl
index 7b716718e8..8e7e9b9d66 100755
--- a/challenge-092/wlmb/perl/ch-1.pl
+++ b/challenge-092/wlmb/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Perl weekly challenge 092
# Task 1: Isomorphic strings.
# Test if two or more strings are isomorphic
-# See https:/wlmb.github.io/2020/12/22/PWC92/#task-1-isomorphic-strings
+# See https://wlmb.github.io/2020/12/22/PWC92/#task-1-isomorphic-strings
use warnings;
use strict;
use v5.10;
diff --git a/challenge-092/wlmb/perl/ch-2.pl b/challenge-092/wlmb/perl/ch-2.pl
index 4b87b09940..1b4d75f50b 100755
--- a/challenge-092/wlmb/perl/ch-2.pl
+++ b/challenge-092/wlmb/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Perl weekly challenge 092
# Task 2: Insert interval.
# Make a sorted list of non-overlapping intervals by adding or merging new intervals.
-# See https:/wlmb.github.io/2020/12/22/PWC92/#task-2-insert-interval
+# See https://wlmb.github.io/2020/12/22/PWC92/#task-2-insert-interval
use warnings;
use strict;
use v5.10;