diff options
| author | Abigail <abigail@abigail.be> | 2021-01-21 16:48:08 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-21 16:48:08 +0100 |
| commit | 1440839d153c33ae9216bef959c38affab2d32b0 (patch) | |
| tree | bddaa0442088ad035a58f81a2fc9e39bffddd379 | |
| parent | 053b0ef31a9cddae0cbd792ad539b2d68876538c (diff) | |
| download | perlweeklychallenge-club-1440839d153c33ae9216bef959c38affab2d32b0.tar.gz perlweeklychallenge-club-1440839d153c33ae9216bef959c38affab2d32b0.tar.bz2 perlweeklychallenge-club-1440839d153c33ae9216bef959c38affab2d32b0.zip | |
Add some standard comments and hash bang lines.
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/awk/ch-1.awk | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/awk/ch-2.awk | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/bash/ch-1.sh | 10 | ||||
| -rw-r--r-- | challenge-096/abigail/c/ch-1.c | 8 | ||||
| -rw-r--r-- | challenge-096/abigail/c/ch-2.c | 23 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/lua/ch-1.lua | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/lua/ch-2.lua | 17 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/node/ch-1.js | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/node/ch-2.js | 9 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/perl/ch-1.pl | 8 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/perl/ch-2.pl | 12 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/python/ch-1.py | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/python/ch-2.py | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/ruby/ch-1.rb | 10 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-096/abigail/ruby/ch-2.rb | 14 |
15 files changed, 143 insertions, 28 deletions
diff --git a/challenge-096/abigail/awk/ch-1.awk b/challenge-096/abigail/awk/ch-1.awk index 744c154648..dae5328b5f 100644..100755 --- 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 index 08cebccb4e..9d3c9fb492 100644..100755 --- 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 index 9122fae4e0..f4d4de34cb 100644..100755 --- 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 <stdlib.h> # include <stdio.h> # include <string.h> 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 <stdlib.h> # include <stdio.h> # include <string.h> @@ -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 index 9c4b1de913..7ff3b94814 100644..100755 --- 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 index 162f06eef0..ef1b039579 100644..100755 --- 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 index b31524a686..16d4f3eada 100644..100755 --- 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 index 8423ab96b8..a9618414a3 100644..100755 --- 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 index ddca656054..ccd4146db5 100644..100755 --- 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 index bb58c84569..45aa08416b 100644..100755 --- 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 index ddc34e2633..84a1c2618b 100644..100755 --- 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 index deaeff14cf..2e24e442f8 100644..100755 --- 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 index 064f780345..9bef80402c 100644..100755 --- 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 index 65a57bd6d3..931de4f2d7 100644..100755 --- 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] = [] |
