diff options
| -rw-r--r-- | challenge-096/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-096/adam-russell/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-096/adam-russell/perl/ch-2.pl | 40 |
3 files changed, 69 insertions, 0 deletions
diff --git a/challenge-096/adam-russell/blog.txt b/challenge-096/adam-russell/blog.txt new file mode 100644 index 0000000000..a2d5c8a65b --- /dev/null +++ b/challenge-096/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/01/24 diff --git a/challenge-096/adam-russell/perl/ch-1.pl b/challenge-096/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..e1d4c4bdf0 --- /dev/null +++ b/challenge-096/adam-russell/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +## +# You are given a string $S. +# Write a script to reverse the +# order of words in the given string. +## +sub reverse_words{ + my($words) = @_; + if(@{$words}){ + my $word = $words->[0]; + my $a = reverse_words([@{$words}[1 .. (@{$words} - 1)]]); + $a->[@{$a}] = $word; + return $a; + } + return []; +} + +MAIN:{ + my($S, $reversed); + $S = "The Weekly Challenge"; + $reversed = reverse_words([split(/\s+/, $S)]); + print join(" ", @{$reversed}) . "\n"; + + $S = " Perl and Raku are part of the same family "; + $reversed = reverse_words([split(/\s+/, $S)]); + print join(" ", @{$reversed}) . "\n"; +}
\ No newline at end of file diff --git a/challenge-096/adam-russell/perl/ch-2.pl b/challenge-096/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..9cd50fe006 --- /dev/null +++ b/challenge-096/adam-russell/perl/ch-2.pl @@ -0,0 +1,40 @@ +use strict; +use warnings; +## +# 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. +## +use Memoize; +memoize("edit_distance"); + +sub edit_distance{ + my($s, $t) = @_; + if(length($s) == 0){ + return length($t); + } + if(length($t) == 0){ + return length($s); + } + my($s0, $t0) = (substr($s, 0, 1), substr($t, 0, 1)); + if($s0 eq $t0){ + return edit_distance(substr($s, 1), substr($t, 1)); + } + my @sorted_distances = sort {$a <=> $b} ( + edit_distance($s, substr($t, 1)), + edit_distance(substr($s, 1), $t), + edit_distance(substr($s, 1), substr($t, 1)), + ); + return 1 + $sorted_distances[0]; +} + +MAIN:{ + my $distance; + + $distance = edit_distance("kitten", "sitting"); + print "$distance\n"; + + $distance = edit_distance("sunday", "monday"); + print "$distance\n"; +}
\ No newline at end of file |
