diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-24 11:02:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-24 11:02:36 +0000 |
| commit | afa40dbfc2207f879f70fd956461827f6f6cee5e (patch) | |
| tree | 663f47b882441245c61846a5fd394acbdae6fbe9 /challenge-096 | |
| parent | 006dcaeb789765cb612df36a5b13d3b02c243edb (diff) | |
| parent | cc20f4fb53e1a13cb74e16865ce18c857c47d539 (diff) | |
| download | perlweeklychallenge-club-afa40dbfc2207f879f70fd956461827f6f6cee5e.tar.gz perlweeklychallenge-club-afa40dbfc2207f879f70fd956461827f6f6cee5e.tar.bz2 perlweeklychallenge-club-afa40dbfc2207f879f70fd956461827f6f6cee5e.zip | |
Merge pull request #3364 from wanderdoc/master
Solutions to challenge-096.
Diffstat (limited to 'challenge-096')
| -rw-r--r-- | challenge-096/wanderdoc/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-096/wanderdoc/perl/ch-2.pl | 61 |
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-096/wanderdoc/perl/ch-1.pl b/challenge-096/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..f6ace07a30 --- /dev/null +++ b/challenge-096/wanderdoc/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string $S. Write a script to reverse the order of words in the given string. The string may contain leading/trailing spaces. The string may have more than one space between words in the string. Print the result without leading/trailing spaces and there should be only one space between words. +Example 1: Input: $S = "The Weekly Challenge" Output: "Challenge Weekly The" +Example 2: Input: $S = " Perl and Raku are part of the same family " +Output: "family same the of part are Raku and Perl" +=cut + + + + + + +use Test::More; + +sub reverse_words +{ + my $str = $_[0]; + my @words = grep length($_), split(/\s+/, $str); + return join(' ', reverse @words); + +} + +is(reverse_words('The Weekly Challenge'), 'Challenge Weekly The', 'Example 1'); +is(reverse_words(' Perl and Raku are part of the same family '), + 'family same the of part are Raku and Perl', 'Example 2'); + + +done_testing();
\ No newline at end of file diff --git a/challenge-096/wanderdoc/perl/ch-2.pl b/challenge-096/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..a79ff75a59 --- /dev/null +++ b/challenge-096/wanderdoc/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given two strings $S1 and $S2. Write a script to find out the minimum operations required to convert $S1 into $S2. The operations can be insert, remove or replace a character. Please check out Wikipedia page for more information. +Example 1: Input: $S1 = "kitten"; $S2 = "sitting" Output: 3 +Example 2: Input: $S1 = "sunday"; $S2 = "monday" Output: 2 +=cut + + + + + + + +use List::Util qw(min); +use Test::More; + + +# Wikipedia: https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance +sub LevenshteinDistance +{ + my ($str_1, $str_2) = @_; + my $mtr; + + + my @first = split(//,$str_1); + my @second = split(//,$str_2); + + + do { my $ch1 = $_; + do { $mtr->[$ch1][$_] = 0 } for 0 .. scalar @second } + for 0 .. scalar @first; + + + $mtr->[$_][0] = $_ for 0 .. scalar @first; + $mtr->[0][$_] = $_ for 0 .. scalar @second; + + + for my $i ( 1 .. scalar @first ) + { + for my $j ( 1 .. scalar @second ) + { + my $distance = $first[$i-1] eq $second[$j-1] ? 0 : 1; + $mtr->[$i][$j] = min + ( + $mtr->[$i-1][$j] + 1, # deletion. + + $mtr->[$i][$j - 1] + 1, # insertion. + $mtr->[$i-1][$j-1] + $distance # substitution. + ); + } + + } + return $mtr->[-1][-1]; +} + +is(LevenshteinDistance(qw(kitten sitting)), 3, 'Example 1'); +is(LevenshteinDistance(qw(sunday monday)), 2, 'Example 2'); +done_testing();
\ No newline at end of file |
