aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-21 22:09:44 +0000
committerGitHub <noreply@github.com>2021-01-21 22:09:44 +0000
commit5a464e79e04b6bb9b30ab23c0e606d2f69e80952 (patch)
treec155c9b079480214b97f423c2be7a36c053a1b4b
parent63c0d699561885257c762e0e04c1a4e6ac3e4212 (diff)
parent3d571c2fc444fab3223114f3551fa1a9e95d2c0a (diff)
downloadperlweeklychallenge-club-5a464e79e04b6bb9b30ab23c0e606d2f69e80952.tar.gz
perlweeklychallenge-club-5a464e79e04b6bb9b30ab23c0e606d2f69e80952.tar.bz2
perlweeklychallenge-club-5a464e79e04b6bb9b30ab23c0e606d2f69e80952.zip
Merge pull request #3334 from gugod/gugod-096
@gugod's solution to pwc 096
-rw-r--r--challenge-096/gugod/blog.txt1
-rw-r--r--challenge-096/gugod/janet/ch-1.janet15
-rw-r--r--challenge-096/gugod/janet/ch-2.janet24
-rw-r--r--challenge-096/gugod/perl/ch-1.pl18
-rw-r--r--challenge-096/gugod/perl/ch-2.pl34
-rw-r--r--challenge-096/gugod/raku/ch-1.raku16
-rw-r--r--challenge-096/gugod/raku/ch-2.raku26
-rw-r--r--challenge-096/gugod/rust/.gitignore2
-rw-r--r--challenge-096/gugod/rust/Makefile10
-rw-r--r--challenge-096/gugod/rust/ch-1.rs19
-rw-r--r--challenge-096/gugod/rust/ch-2.rs35
11 files changed, 200 insertions, 0 deletions
diff --git a/challenge-096/gugod/blog.txt b/challenge-096/gugod/blog.txt
new file mode 100644
index 0000000000..2e424bf4e2
--- /dev/null
+++ b/challenge-096/gugod/blog.txt
@@ -0,0 +1 @@
+https://gugod.org/2021/01/pwc-096-en/
diff --git a/challenge-096/gugod/janet/ch-1.janet b/challenge-096/gugod/janet/ch-1.janet
new file mode 100644
index 0000000000..f3d0e18544
--- /dev/null
+++ b/challenge-096/gugod/janet/ch-1.janet
@@ -0,0 +1,15 @@
+
+(defn reverse-words
+ "Reverse the string word by word"
+ [S]
+ (string/join (reverse (filter (fn [x] (not (= x "")))
+ (string/split " " S)))
+ " "))
+
+# main
+(loop [x :in @["The Weekly Challenge"
+ " Perl and Raku are part of the same family "
+ "join reverse split $S"]]
+ (print (string
+ "Input: " x "\n"
+ "Output: " (reverse-words x))))
diff --git a/challenge-096/gugod/janet/ch-2.janet b/challenge-096/gugod/janet/ch-2.janet
new file mode 100644
index 0000000000..55887d1c4b
--- /dev/null
+++ b/challenge-096/gugod/janet/ch-2.janet
@@ -0,0 +1,24 @@
+
+(defn lev
+ "Compute the Levenshtein distance between string a and b"
+ [a b]
+ (cond (= 0 (length a)) (length b)
+ (= 0 (length b)) (length a)
+ (let [
+ a_head (string/slice a 0 1)
+ a_tail (string/slice a 1)
+ b_head (string/slice b 0 1)
+ b_tail (string/slice b 1)
+ levtail (lev a_tail b_tail)
+ ]
+ (if (= a_head b_head) levtail
+ (+ 1 (min
+ (lev a b_tail)
+ (lev a_tail b)
+ levtail))))))
+
+# main
+(loop [[s1 s2] :in [ ["kitten" "sitting"] ["sunday" "monday"] ]]
+ (print (string
+ "Input: S1=" s1 " S2=" s2 "\n"
+ "Output: " (lev s1 s2))))
diff --git a/challenge-096/gugod/perl/ch-1.pl b/challenge-096/gugod/perl/ch-1.pl
new file mode 100644
index 0000000000..7e7dfd792e
--- /dev/null
+++ b/challenge-096/gugod/perl/ch-1.pl
@@ -0,0 +1,18 @@
+use v5.30;
+use feature 'signatures';
+
+sub reverse_words ($S) {
+ return join " ", reverse split " ", $S;
+}
+
+## main
+my @examples = (
+ "The Weekly Challenge",
+ " Perl and Raku are part of the same family ",
+ "join reverse split \$S",
+);
+
+for my $S (@examples) {
+ say "Input: $S";
+ say "Output: " . reverse_words($S);
+}
diff --git a/challenge-096/gugod/perl/ch-2.pl b/challenge-096/gugod/perl/ch-2.pl
new file mode 100644
index 0000000000..869d980818
--- /dev/null
+++ b/challenge-096/gugod/perl/ch-2.pl
@@ -0,0 +1,34 @@
+use v5.18;
+use List::Util 'min';
+
+## main
+my @examples = (
+ [ 'kitten', 'sitting' ],
+ [ 'sunday', 'monday' ],
+);
+for my $x (@examples) {
+ my ($S1, $S2) = @$x;
+ say "Input: \$S1 = $S1, \$S2 = $S2";
+ say "Output: " . edit_distance($S1, $S2);
+}
+exit;
+
+sub edit_distance {
+ return lev(@_);
+}
+
+sub lev {
+ my ($s1, $s2) = @_;
+
+ return length($s1) if length($s2) == 0;
+ return length($s2) if length($s1) == 0;
+
+ my $s1_tail = substr($s1, 1);
+ my $s2_tail = substr($s2, 1);
+ return lev($s1_tail, $s2_tail) if substr($s1, 0, 1) eq substr($s2, 0, 1);
+ return 1 + min(
+ lev($s1, $s2_tail),
+ lev($s1_tail, $s2),
+ lev($s1_tail, $s2_tail),
+ );
+}
diff --git a/challenge-096/gugod/raku/ch-1.raku b/challenge-096/gugod/raku/ch-1.raku
new file mode 100644
index 0000000000..b8985dc559
--- /dev/null
+++ b/challenge-096/gugod/raku/ch-1.raku
@@ -0,0 +1,16 @@
+sub MAIN {
+ my @examples = (
+ "The Weekly Challenge",
+ " Perl and Raku are part of the same family ",
+ "join reverse split \$S",
+ );
+
+ for @examples -> $S {
+ say "Input: $S";
+ say "Output: " ~ reverse-words($S);
+ }
+}
+
+sub reverse-words (Str $S --> Str) {
+ $S.words.reverse.join(" ");
+}
diff --git a/challenge-096/gugod/raku/ch-2.raku b/challenge-096/gugod/raku/ch-2.raku
new file mode 100644
index 0000000000..bf2e2a1357
--- /dev/null
+++ b/challenge-096/gugod/raku/ch-2.raku
@@ -0,0 +1,26 @@
+sub MAIN {
+ my @examples = (
+ [ 'kitten', 'sitting' ],
+ [ 'sunday', 'monday' ],
+ );
+
+ for @examples -> ($S1, $S2) {
+ say "Input: \$S1 = $S1, \$S2 = $S2";
+ say "Output: " ~ edit-distance($S1, $S2);
+ }
+}
+
+sub edit-distance (Str $S1, Str $S2 --> Int) {
+ my sub lev ($a, $b) {
+ return $a.chars if $b.chars == 0;
+ return $b.chars if $a.chars == 0;
+ return lev( $a.substr(1), $b.substr(1) ) if $a.substr(0,1) eq $b.substr(0,1);
+ return 1 + (
+ lev($a, $b.substr(1)),
+ lev($a.substr(1), $b),
+ lev($a.substr(1), $b.substr(1)),
+ ).min;
+ };
+
+ return lev($S1, $S2);
+}
diff --git a/challenge-096/gugod/rust/.gitignore b/challenge-096/gugod/rust/.gitignore
new file mode 100644
index 0000000000..ac77297bfe
--- /dev/null
+++ b/challenge-096/gugod/rust/.gitignore
@@ -0,0 +1,2 @@
+ch-1
+ch-2
diff --git a/challenge-096/gugod/rust/Makefile b/challenge-096/gugod/rust/Makefile
new file mode 100644
index 0000000000..efa7c49e4e
--- /dev/null
+++ b/challenge-096/gugod/rust/Makefile
@@ -0,0 +1,10 @@
+all: ch-1 ch-2
+
+clean:
+ rm ch-1 ch-2
+
+ch-1: ch-1.rs
+ rustc $<
+
+ch-2: ch-2.rs
+ rustc $<
diff --git a/challenge-096/gugod/rust/ch-1.rs b/challenge-096/gugod/rust/ch-1.rs
new file mode 100644
index 0000000000..b32e0b0654
--- /dev/null
+++ b/challenge-096/gugod/rust/ch-1.rs
@@ -0,0 +1,19 @@
+
+fn reverse_words(x: String) -> String {
+ return x.split_whitespace().rev()
+ .collect::<Vec<&str>>()
+ .join(" ");
+}
+
+fn main() {
+ let examples: [&str; 3] = [
+ "The Weekly Challenge",
+ " Perl and Raku are part of the same family ",
+ "join reverse split $S"
+ ];
+
+ for x in examples.iter() {
+ println!("Input: {}", x);
+ println!("Output: {}", reverse_words(x.to_string()));
+ }
+}
diff --git a/challenge-096/gugod/rust/ch-2.rs b/challenge-096/gugod/rust/ch-2.rs
new file mode 100644
index 0000000000..7a6761191a
--- /dev/null
+++ b/challenge-096/gugod/rust/ch-2.rs
@@ -0,0 +1,35 @@
+
+fn main() {
+ let examples: [[&str; 2]; 2] = [
+ ["kitten", "sitting"],
+ ["sunday", "monday"],
+ ];
+ for x in examples.iter() {
+ println!("Input: S1={}, S2={}", x[0], x[1]);
+ println!("Output: {}", edit_distance(x[0], x[1]));
+ }
+}
+
+fn edit_distance(a: &str, b: &str) -> u32 {
+ let chars_a = a.chars().collect();
+ let chars_b = b.chars().collect();
+ return lev( &chars_a, 0, &chars_b, 0);
+}
+
+fn lev (a: &Vec<char>, offset_a: usize, b: &Vec<char>, offset_b: usize) -> u32 {
+ if offset_a == a.len() {
+ return (b.len() - offset_b) as u32;
+ }
+ if offset_b == b.len() {
+ return (a.len() - offset_a) as u32;
+ }
+
+ let n3 = lev( a, offset_a+1, b, offset_b+1 );
+ if a[offset_a] == b[offset_b] {
+ return n3;
+ } else {
+ let n1 = lev( a, offset_a+1, b, offset_b );
+ let n2 = lev( a, offset_a, b, offset_b+1 );
+ return [n1, n2, n3].iter().min().unwrap() + 1_u32;
+ }
+}