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 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