From 1440839d153c33ae9216bef959c38affab2d32b0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 21 Jan 2021 16:48:08 +0100 Subject: Add some standard comments and hash bang lines. --- challenge-096/abigail/awk/ch-1.awk | 10 ++++++++++ challenge-096/abigail/awk/ch-2.awk | 10 ++++++++++ challenge-096/abigail/bash/ch-1.sh | 10 ++++++++++ challenge-096/abigail/c/ch-1.c | 8 ++++++++ challenge-096/abigail/c/ch-2.c | 23 +++++++++-------------- challenge-096/abigail/lua/ch-1.lua | 10 ++++++++++ challenge-096/abigail/lua/ch-2.lua | 17 +++++++++++------ challenge-096/abigail/node/ch-1.js | 10 ++++++++++ challenge-096/abigail/node/ch-2.js | 9 ++++++++- challenge-096/abigail/perl/ch-1.pl | 8 ++++++++ challenge-096/abigail/perl/ch-2.pl | 12 +++++++++--- challenge-096/abigail/python/ch-1.py | 10 ++++++++++ challenge-096/abigail/python/ch-2.py | 10 ++++++++++ challenge-096/abigail/ruby/ch-1.rb | 10 ++++++++++ challenge-096/abigail/ruby/ch-2.rb | 14 ++++++++++---- 15 files changed, 143 insertions(+), 28 deletions(-) mode change 100644 => 100755 challenge-096/abigail/awk/ch-1.awk mode change 100644 => 100755 challenge-096/abigail/awk/ch-2.awk mode change 100644 => 100755 challenge-096/abigail/bash/ch-1.sh mode change 100644 => 100755 challenge-096/abigail/lua/ch-1.lua mode change 100644 => 100755 challenge-096/abigail/lua/ch-2.lua mode change 100644 => 100755 challenge-096/abigail/node/ch-1.js mode change 100644 => 100755 challenge-096/abigail/node/ch-2.js mode change 100644 => 100755 challenge-096/abigail/perl/ch-1.pl mode change 100644 => 100755 challenge-096/abigail/perl/ch-2.pl mode change 100644 => 100755 challenge-096/abigail/python/ch-1.py mode change 100644 => 100755 challenge-096/abigail/python/ch-2.py mode change 100644 => 100755 challenge-096/abigail/ruby/ch-1.rb mode change 100644 => 100755 challenge-096/abigail/ruby/ch-2.rb (limited to 'challenge-096') diff --git a/challenge-096/abigail/awk/ch-1.awk b/challenge-096/abigail/awk/ch-1.awk old mode 100644 new mode 100755 index 744c154648..dae5328b5f --- a/challenge-096/abigail/awk/ch-1.awk +++ b/challenge-096/abigail/awk/ch-1.awk @@ -1,3 +1,13 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + # # AWK splits lines on whitespace, making each field available # in $1, $2, ..., etc. So, we just print the fields in reverse, diff --git a/challenge-096/abigail/awk/ch-2.awk b/challenge-096/abigail/awk/ch-2.awk old mode 100644 new mode 100755 index 08cebccb4e..9d3c9fb492 --- a/challenge-096/abigail/awk/ch-2.awk +++ b/challenge-096/abigail/awk/ch-2.awk @@ -1,3 +1,13 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + # # Return the minimum of 3 values # diff --git a/challenge-096/abigail/bash/ch-1.sh b/challenge-096/abigail/bash/ch-1.sh old mode 100644 new mode 100755 index 9122fae4e0..f4d4de34cb --- a/challenge-096/abigail/bash/ch-1.sh +++ b/challenge-096/abigail/bash/ch-1.sh @@ -1,3 +1,13 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + # # Read in a line from STDIN, split it on whitespace, # put the result into an array 'words' diff --git a/challenge-096/abigail/c/ch-1.c b/challenge-096/abigail/c/ch-1.c index 22ebafd404..36ec37d5fb 100644 --- a/challenge-096/abigail/c/ch-1.c +++ b/challenge-096/abigail/c/ch-1.c @@ -1,3 +1,11 @@ +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ch-1.o < input-file + */ + # include # include # include diff --git a/challenge-096/abigail/c/ch-2.c b/challenge-096/abigail/c/ch-2.c index 73f35b1c0a..0c88bff827 100644 --- a/challenge-096/abigail/c/ch-2.c +++ b/challenge-096/abigail/c/ch-2.c @@ -1,3 +1,11 @@ +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ch-2.o < input-file + */ + # include # include # include @@ -20,19 +28,6 @@ size_t min3 (size_t a, size_t b, size_t c) { size_t LevenshteinDistance (char * first, size_t n, char * second, size_t m) { - /* - * If 'first' is the shorter string, swap the two strings. - * This reduces the memory usage. - */ - if (n < m) { - char * tmp = first; - size_t t = n; - first = second; - second = tmp; - n = m; - m = t; - } - size_t ** distance; if ((distance = malloc ((n + 1) * sizeof (size_t *))) == NULL) { fprintf (stderr, "Out of memory\n"); @@ -54,7 +49,7 @@ size_t LevenshteinDistance (char * first, size_t n, /* * We only need the previous row; freeing the memory of rows * we no longer need, reduces the memory usage from - * Theta (n * m) to O (min (n, m)) + * Theta (n * m) to O (n + m) */ free (distance [i - 1]); } diff --git a/challenge-096/abigail/lua/ch-1.lua b/challenge-096/abigail/lua/ch-1.lua old mode 100644 new mode 100755 index 9c4b1de913..7ff3b94814 --- a/challenge-096/abigail/lua/ch-1.lua +++ b/challenge-096/abigail/lua/ch-1.lua @@ -1,3 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + for line in io . lines () do -- -- Extract words and put them into an array, in reverse. diff --git a/challenge-096/abigail/lua/ch-2.lua b/challenge-096/abigail/lua/ch-2.lua old mode 100644 new mode 100755 index 162f06eef0..ef1b039579 --- a/challenge-096/abigail/lua/ch-2.lua +++ b/challenge-096/abigail/lua/ch-2.lua @@ -1,3 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + -- -- This is an implementation of the Wagner Fischer algorithm, which -- calculates the Levenshtein distance. @@ -7,11 +17,6 @@ function LevenshteinDistance (first, second) local n = first : len () local m = second : len () - if n < m - then - first, second = second, first - n, m = m, n - end distances = {} for i = 0, n do distances [i] = {} @@ -37,7 +42,7 @@ function LevenshteinDistance (first, second) -- -- We only need the previous row, so we can return the -- memory of rows before that. This reduces the memory - -- usage from O (n * m) to O (min (n, m)) + -- usage from Theta (n * m) to O (n + m) -- if i > 0 then diff --git a/challenge-096/abigail/node/ch-1.js b/challenge-096/abigail/node/ch-1.js old mode 100644 new mode 100755 index b31524a686..16d4f3eada --- a/challenge-096/abigail/node/ch-1.js +++ b/challenge-096/abigail/node/ch-1.js @@ -1,3 +1,13 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + // // Read STDIN. Split on newlines, filter out empty lines, then call "main". // diff --git a/challenge-096/abigail/node/ch-2.js b/challenge-096/abigail/node/ch-2.js old mode 100644 new mode 100755 index 8423ab96b8..a9618414a3 --- a/challenge-096/abigail/node/ch-2.js +++ b/challenge-096/abigail/node/ch-2.js @@ -1,6 +1,13 @@ +#!/usr/local/bin/node + +// +// See ../README.md // -// Read STDIN. Split on newlines, filter out empty lines, then call "main". + // +// Run as: node ch-2.js < input-file +// + require ("fs") . readFileSync (0) // Read all. . toString () // Turn it into a string. diff --git a/challenge-096/abigail/perl/ch-1.pl b/challenge-096/abigail/perl/ch-1.pl old mode 100644 new mode 100755 index ddca656054..ccd4146db5 --- a/challenge-096/abigail/perl/ch-1.pl +++ b/challenge-096/abigail/perl/ch-1.pl @@ -1,5 +1,13 @@ #!/opt/perl/bin/perl +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + use 5.032; use strict; diff --git a/challenge-096/abigail/perl/ch-2.pl b/challenge-096/abigail/perl/ch-2.pl old mode 100644 new mode 100755 index bb58c84569..45aa08416b --- a/challenge-096/abigail/perl/ch-2.pl +++ b/challenge-096/abigail/perl/ch-2.pl @@ -1,4 +1,12 @@ +#!/opt/perl/bin/perl +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# use 5.032; @@ -18,8 +26,6 @@ use List::Util 'min'; # See https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm # sub LevenshteinDistance ($first, $second) { - ($first, $second) = ($second, $first) if length ($first) < - length ($second); my $distance; for (my $i = 0; $i <= length ($first); $i ++) { for (my $j = 0; $j <= length ($second); $j ++) { @@ -33,7 +39,7 @@ sub LevenshteinDistance ($first, $second) { } # # We only need the previous row; this reduces the memory - # from O (N * M) to O (min (N, M)), where N and M are the + # from Theta (N * M) to O (N + M), where N and M are the # lengths of the input strings. # undef $$distance [$i - 1] if $i; diff --git a/challenge-096/abigail/python/ch-1.py b/challenge-096/abigail/python/ch-1.py old mode 100644 new mode 100755 index ddc34e2633..84a1c2618b --- a/challenge-096/abigail/python/ch-1.py +++ b/challenge-096/abigail/python/ch-1.py @@ -1,3 +1,13 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + import fileinput for line in fileinput . input (): diff --git a/challenge-096/abigail/python/ch-2.py b/challenge-096/abigail/python/ch-2.py old mode 100644 new mode 100755 index deaeff14cf..2e24e442f8 --- a/challenge-096/abigail/python/ch-2.py +++ b/challenge-096/abigail/python/ch-2.py @@ -1,3 +1,13 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + import fileinput def LevenshteinDistance (first, second): diff --git a/challenge-096/abigail/ruby/ch-1.rb b/challenge-096/abigail/ruby/ch-1.rb old mode 100644 new mode 100755 index 064f780345..9bef80402c --- a/challenge-096/abigail/ruby/ch-1.rb +++ b/challenge-096/abigail/ruby/ch-1.rb @@ -1,3 +1,13 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + ARGF . each_line do |_| puts ((_ . split (/\s+/)) . grep (/\S/)) . reverse . join (" ") end diff --git a/challenge-096/abigail/ruby/ch-2.rb b/challenge-096/abigail/ruby/ch-2.rb old mode 100644 new mode 100755 index 65a57bd6d3..931de4f2d7 --- a/challenge-096/abigail/ruby/ch-2.rb +++ b/challenge-096/abigail/ruby/ch-2.rb @@ -1,10 +1,16 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + def LevenshteinDistance (first, second) n = first . length m = second . length - if n < m - first, second = second, first - n, m = m, n - end distances = [] for i in 0 .. n do distances [i] = [] -- cgit From 0a3ffcf16f7278c15d13d9c0c6e90de653a811e5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 21 Jan 2021 22:02:59 +0100 Subject: Layout --- challenge-096/abigail/bash/ch-1.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/bash/ch-1.sh b/challenge-096/abigail/bash/ch-1.sh index f4d4de34cb..65c6e20bdb 100755 --- a/challenge-096/abigail/bash/ch-1.sh +++ b/challenge-096/abigail/bash/ch-1.sh @@ -17,7 +17,7 @@ do # # Iterate over the words backwards, and print them. # - for ((i = ${#words[@]} - 1; i >= 0; i --)); + for ((i = ${#words[@]} - 1; i >= 0; i --)); do printf "%s" ${words[$i]} # # Print a newline after the final word; otherwise, -- cgit From 5c43277e4f2fd1b90d3a51f140f3f4be04e5a06c Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 21 Jan 2021 22:20:20 +0100 Subject: Skip any whitespace, not just spaces --- challenge-096/abigail/lua/ch-1.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/lua/ch-1.lua b/challenge-096/abigail/lua/ch-1.lua index 7ff3b94814..c2de0434f6 100755 --- a/challenge-096/abigail/lua/ch-1.lua +++ b/challenge-096/abigail/lua/ch-1.lua @@ -13,7 +13,7 @@ for line in io . lines () do -- Extract words and put them into an array, in reverse. -- local words = {} - for str in string . gmatch (line, "[^ ]+") do + for str in string . gmatch (line, "[^%s]+") do table . insert (words, 1, str) end -- cgit From 4e4ef99b1f7257e4fc10075bdc03196ed31cbdee Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 22 Jan 2021 12:56:35 +0100 Subject: We only need one pointer. There's no need for a different pointer when searching for the beginning of the word, nor do we need strcpy to extract words before printing it. Instead, we set the first white space after a word to '\0', walk the pointer to the beginning of the word, then print it. --- challenge-096/abigail/c/ch-1.c | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/c/ch-1.c b/challenge-096/abigail/c/ch-1.c index 36ec37d5fb..b01e726173 100644 --- a/challenge-096/abigail/c/ch-1.c +++ b/challenge-096/abigail/c/ch-1.c @@ -8,63 +8,49 @@ # include # include -# include # include int main (void) { char * line = NULL; size_t len = 0; - size_t strlen; + size_t ptr; - while ((strlen = getline (&line, &len, stdin)) != -1) { - char * line_ptr = line; - size_t end = strlen; - size_t begin; + while ((ptr = getline (&line, &len, stdin)) != -1) { size_t output = 0; - while (end > 0) { + while (ptr > 0) { /* * Skip tailing whitespace */ - while (end > 0 && isspace (line [end - 1])) { - end --; + while (ptr > 0 && isspace (line [ptr - 1])) { + ptr --; } /* - * 'end' now just after the end of a word, or + * 'ptr' is now just after the end of a word, or * at the beginning of the string; if the latter, * there is nothing left to print. */ - - if (end <= 0) { + if (ptr <= 0) { break; } /* - * Find the beginning of a word + * Terminate the string just after the newly found word */ - begin = end - 1; - while (begin > 0 && !isspace (line [begin - 1])) { - begin --; - } + line [ptr] = '\0'; /* - * Extract the word out of the string: allocate memory, - * and copy the right part of the input. + * Find the beginning of that word */ - char * word; - if ((word = malloc ((end - begin + 1) * sizeof (char))) == NULL) { - fprintf (stderr, "Out of memory\n"); - exit (1); + while (ptr > 0 && !isspace (line [ptr - 1])) { + ptr --; } - stpncpy (word, line + begin, end - begin); /* - * Print the string, prepended (except the first printed word) + * Print the word, prepended (except the first printed word) * by a space. */ - printf ("%s%s", output ++ ? " " : "", word); - - end = begin; + printf ("%s%s", output ++ ? " " : "", &line [ptr]); } printf ("\n"); } -- cgit From f099a71ae4c4ded3d6abc979eb852ca6b1910983 Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 22 Jan 2021 14:47:19 +0100 Subject: Capitalize Lua and Bash --- challenge-096/abigail/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/README.md b/challenge-096/abigail/README.md index d715618651..7619e7c1f2 100644 --- a/challenge-096/abigail/README.md +++ b/challenge-096/abigail/README.md @@ -21,9 +21,9 @@ Output: "family same the of part are Raku and Perl" ### Solutions * [AWK](awk/ch-1.awk) -* [bash](sh/ch-1.sh) +* [Bash](sh/ch-1.sh) * [C](c/ch-1.c) -* [lua](lua/ch-1.lua) +* [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) @@ -62,7 +62,7 @@ Operation 2: replace 'u' with 'o' ### Solutions * [AWK](awk/ch-2.awk) * [C](c/ch-2.c) -* [lua](lua/ch-2.lua) +* [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) -- cgit From 674232e2c4cc94045c30259726508a1296976f5e Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 22 Jan 2021 15:56:52 +0100 Subject: Remove redundant comment --- challenge-096/abigail/node/ch-1.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/node/ch-1.js b/challenge-096/abigail/node/ch-1.js index 16d4f3eada..15aa190086 100755 --- a/challenge-096/abigail/node/ch-1.js +++ b/challenge-096/abigail/node/ch-1.js @@ -8,9 +8,6 @@ // Run as: node ch-1.js < input-file // -// -// Read STDIN. Split on newlines, filter out empty lines, then call "main". -// require ("fs") . readFileSync (0) // Read all. . toString () // Turn it into a string. -- cgit From d3b7070aceb66209460753b95438b7e53f07bd61 Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 22 Jan 2021 20:28:00 +0100 Subject: Use the 'readline' module instead of the 'fs' module to read lines. --- challenge-096/abigail/node/ch-1.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/node/ch-1.js b/challenge-096/abigail/node/ch-1.js index 15aa190086..994c1e1971 100755 --- a/challenge-096/abigail/node/ch-1.js +++ b/challenge-096/abigail/node/ch-1.js @@ -8,15 +8,10 @@ // Run as: node ch-1.js < input-file // - require ("fs") -. readFileSync (0) // Read all. -. toString () // Turn it into a string. -. split ("\n") // Split on newlines. -. filter (_ => _ . length) // Filter out empty lines. -. map (_ => console . log (_ . trim () // Remove leading - // and trailing - // whitespace. - . split (/\s+/) // split ... - . reverse () // reverse ... - . join (" "))) // and recombine. -; +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => + console . log (_ . trim () // Remove leading/trailing spaces + . split (/\s+/) // Split on white space + . reverse () // Reverse the words + . join (" "))) // And join them again. -- cgit From 75371beffba48cc30eae84cedb56041e9cb9c535 Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 22 Jan 2021 20:30:30 +0100 Subject: Use strip to get rid of white space --- challenge-096/abigail/ruby/ch-1.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/ruby/ch-1.rb b/challenge-096/abigail/ruby/ch-1.rb index 9bef80402c..588ac76d7c 100755 --- a/challenge-096/abigail/ruby/ch-1.rb +++ b/challenge-096/abigail/ruby/ch-1.rb @@ -9,5 +9,5 @@ # ARGF . each_line do |_| - puts ((_ . split (/\s+/)) . grep (/\S/)) . reverse . join (" ") + puts (_ . strip . split (/\s+/)) . reverse . join (" ") end -- cgit From 5a768f7b81cf1aed0f8ced5bb4bfe6f909bdad1b Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 22 Jan 2021 20:42:40 +0100 Subject: Link to blog post --- challenge-096/abigail/README.md | 1 + challenge-096/abigail/blog.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-096/abigail/blog.txt (limited to 'challenge-096') diff --git a/challenge-096/abigail/README.md b/challenge-096/abigail/README.md index 7619e7c1f2..8e3135aefb 100644 --- a/challenge-096/abigail/README.md +++ b/challenge-096/abigail/README.md @@ -30,6 +30,7 @@ Output: "family same the of part are Raku and Perl" * [Ruby](ruby/ch-1.rb) ### Blog +[Perl Weekly Challenge 96, Reverse Words](https://wp.me/pcxd30-mj) ## [Edit Distance](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK2) diff --git a/challenge-096/abigail/blog.txt b/challenge-096/abigail/blog.txt new file mode 100644 index 0000000000..2559e67c32 --- /dev/null +++ b/challenge-096/abigail/blog.txt @@ -0,0 +1 @@ +https://wp.me/pcxd30-mj -- cgit From 9be6fbdcfa9db0de76be5cc263ef5e20e56ac3e5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 23 Jan 2021 02:21:56 +0100 Subject: Link to second blog --- challenge-096/abigail/README.md | 3 ++- challenge-096/abigail/blog1.txt | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 challenge-096/abigail/blog1.txt (limited to 'challenge-096') diff --git a/challenge-096/abigail/README.md b/challenge-096/abigail/README.md index 8e3135aefb..5c7ebe0027 100644 --- a/challenge-096/abigail/README.md +++ b/challenge-096/abigail/README.md @@ -30,7 +30,7 @@ Output: "family same the of part are Raku and Perl" * [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 96, Reverse Words](https://wp.me/pcxd30-mj) +[Perl Weekly Challenge 96; Reverse Words](https://wp.me/pcxd30-mj) ## [Edit Distance](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK2) @@ -70,3 +70,4 @@ Operation 2: replace 'u' with 'o' * [Ruby](ruby/ch-2.rb) ### Blog +[Perl Weekly Challenge 96: Edit Distance](https://wp.me/pcxd30-n7) diff --git a/challenge-096/abigail/blog1.txt b/challenge-096/abigail/blog1.txt new file mode 100644 index 0000000000..231a6fbbc0 --- /dev/null +++ b/challenge-096/abigail/blog1.txt @@ -0,0 +1 @@ +https://wp.me/pcxd30-n7 -- cgit From 6795ffaa08f0c0b1a7e1a533c93a5c03e57cdcd0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 23 Jan 2021 02:31:18 +0100 Subject: Clearify we only output a number, not a sequence of operations. --- challenge-096/abigail/README.md | 11 +++++++++++ challenge-096/abigail/perl/ch-2.pl | 11 +++++++++++ 2 files changed, 22 insertions(+) (limited to 'challenge-096') diff --git a/challenge-096/abigail/README.md b/challenge-096/abigail/README.md index 5c7ebe0027..ff3190658c 100644 --- a/challenge-096/abigail/README.md +++ b/challenge-096/abigail/README.md @@ -19,6 +19,17 @@ Input: $S = " Perl and Raku are part of the same family " Output: "family same the of part are Raku and Perl" ~~~~ +### Notes + +The challenge isn't quite clear on whether we should output a number +(the minimal number of operations required), or the actual operations. +The examples show both -- but separated by a blank line. Previous +challenges typically use a blank line to separate the required output +from the explaination on why that it is the correct answer. + +We're opting to only print the number of operations, not the actual +operations. + ### Solutions * [AWK](awk/ch-1.awk) * [Bash](sh/ch-1.sh) diff --git a/challenge-096/abigail/perl/ch-2.pl b/challenge-096/abigail/perl/ch-2.pl index 45aa08416b..c84dec7ad1 100755 --- a/challenge-096/abigail/perl/ch-2.pl +++ b/challenge-096/abigail/perl/ch-2.pl @@ -19,6 +19,17 @@ use experimental 'lexical_subs'; use List::Util 'min'; +# +# The challenge isn't quite clear on whether we should output a number +# (the minimal number of operations required), or the actual operations. +# The examples show both -- but separated by a blank line. Previous +# challenges typically use a blank line to separate the required output +# from the explaination on why that it is the correct answer. +# +# We're opting to only print the number of operations, not the actual +# operations. +# + # # This is an implementation of the Wagner Fischer algorithm, which # calculates the Levenshtein distance. -- cgit From 64f66b887a3ce5af2fecbfd49d7107bed36e4c71 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 23 Jan 2021 02:34:39 +0100 Subject: Typo fix --- challenge-096/abigail/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/README.md b/challenge-096/abigail/README.md index ff3190658c..9e5afd71ff 100644 --- a/challenge-096/abigail/README.md +++ b/challenge-096/abigail/README.md @@ -41,7 +41,7 @@ operations. * [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 96; Reverse Words](https://wp.me/pcxd30-mj) +[Perl Weekly Challenge 96: Reverse Words](https://wp.me/pcxd30-mj) ## [Edit Distance](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK2) -- cgit From 1c9975a4c79a02569f92310cf5e30ccd2b542033 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 23 Jan 2021 21:44:49 +0100 Subject: Use 'distance' instead of 'distances' for the array name. --- challenge-096/abigail/lua/ch-2.lua | 18 +++++++++--------- challenge-096/abigail/ruby/ch-2.rb | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'challenge-096') diff --git a/challenge-096/abigail/lua/ch-2.lua b/challenge-096/abigail/lua/ch-2.lua index ef1b039579..99058213ca 100755 --- a/challenge-096/abigail/lua/ch-2.lua +++ b/challenge-096/abigail/lua/ch-2.lua @@ -17,13 +17,13 @@ function LevenshteinDistance (first, second) local n = first : len () local m = second : len () - distances = {} + distance = {} for i = 0, n do - distances [i] = {} + distance [i] = {} for j = 0, m do if i == 0 or j == 0 then - distances [i] [j] = i + j + distance [i] [j] = i + j else local cost = 1 if string . sub (first, i, i) == @@ -31,10 +31,10 @@ function LevenshteinDistance (first, second) then cost = 0 end - distances [i] [j] = math . min ( - distances [i - 1] [j] + 1, - distances [i] [j - 1] + 1, - distances [i - 1] [j - 1] + cost + distance [i] [j] = math . min ( + distance [i - 1] [j] + 1, + distance [i] [j - 1] + 1, + distance [i - 1] [j - 1] + cost ) end end @@ -46,10 +46,10 @@ function LevenshteinDistance (first, second) -- if i > 0 then - distances [i - 1] = nil + distance [i - 1] = nil end end - return distances [n] [m] + return distance [n] [m] end diff --git a/challenge-096/abigail/ruby/ch-2.rb b/challenge-096/abigail/ruby/ch-2.rb index 931de4f2d7..b7ea2a3f6b 100755 --- a/challenge-096/abigail/ruby/ch-2.rb +++ b/challenge-096/abigail/ruby/ch-2.rb @@ -11,24 +11,24 @@ def LevenshteinDistance (first, second) n = first . length m = second . length - distances = [] + distance = [] for i in 0 .. n do - distances [i] = [] + distance [i] = [] for j in 0 .. m do - distances [i] [j] = i == 0 || j == 0 ? i + j - : [distances [i - 1] [j] + 1, - distances [i] [j - 1] + 1, - distances [i - 1] [j - 1] + + distance [i] [j] = i == 0 || j == 0 ? i + j + : [distance [i - 1] [j] + 1, + distance [i] [j - 1] + 1, + distance [i - 1] [j - 1] + (first [i - 1] == second [j - 1] ? 0 : 1)] . min end # # Release memory # if i > 1 - distances [i - 1] = nil + distance [i - 1] = nil end end - return distances [n] [m] + return distance [n] [m] end ARGF . each_line do |_| -- cgit