diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-21 23:14:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-21 23:14:09 +0000 |
| commit | b3bb15dc8d2ff9266267c325d77bedd9f9cf6d12 (patch) | |
| tree | 75de6a3e136c929ea3f44bc888361c14aa6262b8 | |
| parent | 8033ecc27e3fe7b5cc625851a1f64c87ed30e7aa (diff) | |
| parent | 8943cd3305ee9d3400427451d6a9d197e399c6d1 (diff) | |
| download | perlweeklychallenge-club-b3bb15dc8d2ff9266267c325d77bedd9f9cf6d12.tar.gz perlweeklychallenge-club-b3bb15dc8d2ff9266267c325d77bedd9f9cf6d12.tar.bz2 perlweeklychallenge-club-b3bb15dc8d2ff9266267c325d77bedd9f9cf6d12.zip | |
Merge pull request #3340 from nunovieira220/challenge-096
Add solution to challenge 096
| -rw-r--r-- | challenge-096/nunovieira220/js/ch-1.js | 9 | ||||
| -rw-r--r-- | challenge-096/nunovieira220/js/ch-2.js | 19 | ||||
| -rw-r--r-- | challenge-096/nunovieira220/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-096/nunovieira220/perl/ch-2.pl | 28 |
4 files changed, 73 insertions, 0 deletions
diff --git a/challenge-096/nunovieira220/js/ch-1.js b/challenge-096/nunovieira220/js/ch-1.js new file mode 100644 index 0000000000..5480a7f9ba --- /dev/null +++ b/challenge-096/nunovieira220/js/ch-1.js @@ -0,0 +1,9 @@ +// Input +const S = ' Perl and Raku are part of the same family '; + +// Reverse Words +const trimmed = S.trim().replace(/\s+/g, ' '); +const res = trimmed.split(' ').reverse().join(' '); + +// Output +console.log(res);
\ No newline at end of file diff --git a/challenge-096/nunovieira220/js/ch-2.js b/challenge-096/nunovieira220/js/ch-2.js new file mode 100644 index 0000000000..33eb9566f3 --- /dev/null +++ b/challenge-096/nunovieira220/js/ch-2.js @@ -0,0 +1,19 @@ +// Input +const s1 = 'kitten'; +const s2 = 'sitting'; + +// Edit Distance +const maxLength = Math.max(s1.length, s2.length); + +for(let i = 0; i < maxLength; i++) { + const char1 = s1.charAt(i); + const char2 = s2.charAt(i); + + if(char1 && char2) { + console.log(`Replace '${char1}' with '${char2}'`); + } else if (char1) { + console.log(`Remove '${char1}' at position ${$i + 1}`); + } else { + console.log(`Insert '${char2}' at the end`); + } +}
\ No newline at end of file diff --git a/challenge-096/nunovieira220/perl/ch-1.pl b/challenge-096/nunovieira220/perl/ch-1.pl new file mode 100644 index 0000000000..87cf5afacb --- /dev/null +++ b/challenge-096/nunovieira220/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw(say); + +# Input +my $S = " Perl and Raku are part of the same family "; + +# Reverse Words +$S =~ s/^\s+|\s+$//g; +$S =~ s/\s+/ /g; + +my $res = join(" ", reverse(split(" ", $S))); + +# Output +say($res);
\ No newline at end of file diff --git a/challenge-096/nunovieira220/perl/ch-2.pl b/challenge-096/nunovieira220/perl/ch-2.pl new file mode 100644 index 0000000000..7603957e12 --- /dev/null +++ b/challenge-096/nunovieira220/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw(say); +use List::Util qw[max]; + +# Input +my $S1 = "kitten"; +my $S2 = "sitting"; + +# Edit Distance +my $maxLength = max(length($S1), length($S2)); + +for(my $i = 0; $i < $maxLength; $i++) { + my $char1 = substr($S1, $i, 1); + my $char2 = substr($S2, $i, 1); + + if(notEmpty($char1) && notEmpty($char2)) { + say("Replace '$char1' with '$char2'"); + } elsif (notEmpty($char1)) { + say("Remove '$char1' at position ".($i + 1)); + } else { + say("Insert '$char2' at the end"); + } +} + +sub notEmpty { return defined $_[0] && !$_[0] eq '' ? 1 : 0 } |
