aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-23 21:42:03 +0000
committerGitHub <noreply@github.com>2021-01-23 21:42:03 +0000
commit371e2671b30590d4224f9675772261e68ae12b3a (patch)
tree0da18afa7ab027b4013aba710d2eb55780ffa545
parentfe4bd9f184f7a2c8dea2a446f2fd3c1f0a8c1f66 (diff)
parentaac88a90a9c288b85bd4dad14bc788d37bb934bb (diff)
downloadperlweeklychallenge-club-371e2671b30590d4224f9675772261e68ae12b3a.tar.gz
perlweeklychallenge-club-371e2671b30590d4224f9675772261e68ae12b3a.tar.bz2
perlweeklychallenge-club-371e2671b30590d4224f9675772261e68ae12b3a.zip
Merge pull request #3350 from gnustavo/096
Add Gustavo Chaves's solutions to challenge 096
-rwxr-xr-xchallenge-096/gustavo-chaves/perl/ch-1.pl13
-rwxr-xr-xchallenge-096/gustavo-chaves/perl/ch-2.pl36
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-096/gustavo-chaves/perl/ch-1.pl b/challenge-096/gustavo-chaves/perl/ch-1.pl
new file mode 100755
index 0000000000..36ee8b727a
--- /dev/null
+++ b/challenge-096/gustavo-chaves/perl/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/
+# TASK #1 › Reverse Words
+
+use 5.030;
+use warnings;
+
+my $S = shift;
+
+local $, = ' ';
+
+say reverse grep {length} split ' ', $S;
diff --git a/challenge-096/gustavo-chaves/perl/ch-2.pl b/challenge-096/gustavo-chaves/perl/ch-2.pl
new file mode 100755
index 0000000000..25a0701ced
--- /dev/null
+++ b/challenge-096/gustavo-chaves/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/
+# TASK #2 › Edit Distance
+
+use 5.030;
+use warnings;
+
+my $S1 = shift;
+my $S2 = shift;
+
+my @S1 = split '', $S1;
+my @S2 = split '', $S2;
+
+sub distance {
+ my ($i, $j) = @_;
+
+ if ($i < 0 && $j < 0) {
+ return;
+ } elsif ($j < 0) {
+ return map {"delete '$S1[$_]'"} 0 .. $i;
+ } elsif ($i < 0) {
+ return map {"delete '$S2[$_]'"} 0 .. $j;
+ } elsif ($S1[$i] eq $S2[$j]) {
+ return distance($i-1, $j-1);
+ } else {
+ my @del = (distance($i-1, $j), "delete '$S1[$i]' from $i");
+ my @ins = (distance($i, $j-1), "insert '$S2[$j]' at $j");
+ my @sub = (distance($i-1, $j-1), "replace '$S1[$i]' at $i with '$S2[$j]'");
+ return @{(sort {scalar(@$a) <=> scalar(@$b)} \@del, \@ins, \@sub)[0]};
+ }
+}
+
+my @ops = distance($#S1, $#S2);
+
+say "Operation $_: $ops[$_-1]" for 1 .. @ops;