aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-01-24 00:29:29 -0500
committerAdam Russell <ac.russell@live.com>2021-01-24 00:29:29 -0500
commitb7f871dd00678751b00bfddf71ab2eb05dd81a2c (patch)
tree38c0f024952b84ad97f07de8690605aedc486c0f
parent8ce3fe6c38204b377e4389bafb3cbc744c1ad945 (diff)
downloadperlweeklychallenge-club-b7f871dd00678751b00bfddf71ab2eb05dd81a2c.tar.gz
perlweeklychallenge-club-b7f871dd00678751b00bfddf71ab2eb05dd81a2c.tar.bz2
perlweeklychallenge-club-b7f871dd00678751b00bfddf71ab2eb05dd81a2c.zip
Perl solutions for challenge 096
-rw-r--r--challenge-096/adam-russell/perl/ch-1.pl28
-rw-r--r--challenge-096/adam-russell/perl/ch-2.pl37
-rw-r--r--challenge-096/adam-russell/prolog/ch-1.p0
-rw-r--r--challenge-096/adam-russell/prolog/ch-2.p0
4 files changed, 65 insertions, 0 deletions
diff --git a/challenge-096/adam-russell/perl/ch-1.pl b/challenge-096/adam-russell/perl/ch-1.pl
index e69de29bb2..a89690ca4f 100644
--- a/challenge-096/adam-russell/perl/ch-1.pl
+++ 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)]]);
+ push @{$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
index e69de29bb2..6b8172ea02 100644
--- a/challenge-096/adam-russell/perl/ch-2.pl
+++ b/challenge-096/adam-russell/perl/ch-2.pl
@@ -0,0 +1,37 @@
+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.
+##
+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
diff --git a/challenge-096/adam-russell/prolog/ch-1.p b/challenge-096/adam-russell/prolog/ch-1.p
deleted file mode 100644
index e69de29bb2..0000000000
--- a/challenge-096/adam-russell/prolog/ch-1.p
+++ /dev/null
diff --git a/challenge-096/adam-russell/prolog/ch-2.p b/challenge-096/adam-russell/prolog/ch-2.p
deleted file mode 100644
index e69de29bb2..0000000000
--- a/challenge-096/adam-russell/prolog/ch-2.p
+++ /dev/null