From c5b4f3e08800aba3233684a79a18c84821b25a2b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 16:14:23 +0200 Subject: README file for week 111 --- challenge-111/abigail/README.md | 91 +++++++---------------------------------- 1 file changed, 15 insertions(+), 76 deletions(-) diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index f73efb55cd..def177b2a6 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -1,91 +1,30 @@ -# Solutions by Asbigail -## [Valid Phone Number Formats](https://perlweeklychallenge.org/blog/perl-weekly-challenge-110/#TASK1) +# Solutions by Abigail +## [Search Matrix](https://perlweeklychallenge.org/blog/perl-weekly-challenge-111/#TASK1) -> You are given a text file. -> -> Write a script to display all valid phone numbers in the given text file. - -### Acceptable Phone Number Formats - -~~~~ -+nn nnnnnnnnnn -(nn) nnnnnnnnnn -nnnn nnnnnnnnnn -~~~~ - -### Input File -~~~~ -0044 1148820341 - +44 1148820341 - 44-11-4882-0341 -(44) 1148820341 - 00 1148820341 -~~~~ - -### Output -~~~~ -0044 1148820341 - +44 1148820341 -(44) 1148820341 -~~~~ +> You are given 5x5 matrix filled with integers such that each row is +> sorted from left to right and the first integer of each row is greater +> than the last integer of the previous row. +> +> Write a script to find a given integer in the matrix using an +> efficient search algorithm. ### Notes -The examples show we should not take the specification as a specification; -just a rough guideline. According to the specification, -" +44 1148820341" fails the criteria not once, but twice: it contains -a leading space (not allowed in the specification), and it has only a -single space between the '+44' part and the rest, where the specification -requires two. -We therefore conclude the examples just contain random spaces, and we -can completly ignore any white space in the input. ### Solutions -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [C](c/ch-1.c) -* [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) -* [Perl](perl/ch-1.pl) -* [Python](python/ch-1.py) -* [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 110: Valid Phone Numbers](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-110-1.html) - -## [Transpose File](https://perlweeklychallenge.org/blog/perl-weekly-challenge-110/#TASK2) - -> You are given a text file. -> -> Write a script to transpose the contents of the given file. - -### Input File -~~~~ -name,age,sex -Mohammad,45,m -Joe,20,m -Julie,35,f -Cristina,10,f -~~~~ -### Output -~~~~ -name,Mohammad,Joe,Julie,Cristina -age,45,20,35,10 -sex,m,m,f,f -~~~~ +## [Ordered Letters](https://perlweeklychallenge.org/blog/perl-weekly-challenge-111/#TASK2) +> Given a word, you can sort its letters alphabetically (case insensitive). +> For example, 'beekeeper' becomes 'beeeeekpr' and 'dictionary' becomes +> 'acdiinorty'. +> +> Write a script to find the longest English words that don't change when +> their letters are sorted. ### Solutions -* [AWK](awk/ch-2.awk) -* [Bash](bash/ch-2.sh) -* [C](c/ch-2.c) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) -* [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) -* [Ruby](ruby/ch-2.rb) ### Blog -[Perl Weekly Challenge 110: Transpose File](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-110-2.html) -- cgit From 00a6aa4bd996590102bf471f77da0908f118bbe0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 16:22:45 +0200 Subject: First tests for week 111 --- challenge-111/abigail/t/ctest.ini | 18 ++++++++++++++++++ challenge-111/abigail/t/input-2-1 | 0 challenge-111/abigail/t/input-2-2 | 0 challenge-111/abigail/t/input-2-3 | 0 challenge-111/abigail/t/output-2-1.exp | 1 + challenge-111/abigail/t/output-2-2.exp | 1 + challenge-111/abigail/t/output-2-3.exp | 1 + 7 files changed, 21 insertions(+) create mode 100644 challenge-111/abigail/t/ctest.ini create mode 100644 challenge-111/abigail/t/input-2-1 create mode 100644 challenge-111/abigail/t/input-2-2 create mode 100644 challenge-111/abigail/t/input-2-3 create mode 100644 challenge-111/abigail/t/output-2-1.exp create mode 100644 challenge-111/abigail/t/output-2-2.exp create mode 100644 challenge-111/abigail/t/output-2-3.exp diff --git a/challenge-111/abigail/t/ctest.ini b/challenge-111/abigail/t/ctest.ini new file mode 100644 index 0000000000..0c67225c11 --- /dev/null +++ b/challenge-111/abigail/t/ctest.ini @@ -0,0 +1,18 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +2-1 = Enable.lst +2-2 = /usr/share/dict/words +2-3 = infochimps.com + +[2-1] +input_file = /Users/abigail/Words/enable.lst + +[2-2] +input_file = /usr/share/dict/words + +[2-3] +input_file = /Users/abigail/Words/infochimps.txt diff --git a/challenge-111/abigail/t/input-2-1 b/challenge-111/abigail/t/input-2-1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-111/abigail/t/input-2-2 b/challenge-111/abigail/t/input-2-2 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-111/abigail/t/input-2-3 b/challenge-111/abigail/t/input-2-3 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-111/abigail/t/output-2-1.exp b/challenge-111/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..b5029dd205 --- /dev/null +++ b/challenge-111/abigail/t/output-2-1.exp @@ -0,0 +1 @@ +beefily diff --git a/challenge-111/abigail/t/output-2-2.exp b/challenge-111/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..fd3ba3ff6c --- /dev/null +++ b/challenge-111/abigail/t/output-2-2.exp @@ -0,0 +1 @@ +Adelops diff --git a/challenge-111/abigail/t/output-2-3.exp b/challenge-111/abigail/t/output-2-3.exp new file mode 100644 index 0000000000..8dbf15984e --- /dev/null +++ b/challenge-111/abigail/t/output-2-3.exp @@ -0,0 +1 @@ +aegilops -- cgit From 9dd85135a86d3baa87f1858f272a2a47efbfd159 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 17:40:10 +0200 Subject: Perl solution for week 111, part 2 --- challenge-111/abigail/README.md | 28 ++++++++++++++++++- challenge-111/abigail/perl/ch-2.pl | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 challenge-111/abigail/perl/ch-2.pl diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index def177b2a6..5f00924966 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -10,7 +10,6 @@ ### Notes - ### Solutions ### Blog @@ -24,7 +23,34 @@ > Write a script to find the longest English words that don't change when > their letters are sorted. +### Notes +We will grep the words from standard input which don't change +if they are sorted; these are the words which match the pattern +/^a*b*c*...z*$/i. We keep track of the longest word. + +If course, there does not have to be a unique word. It will depend +on the word list used to search in. For instance, three different +word list I used give different results: + + infochimps.com /usr/share/dict/words enable.lst + -------------- --------------------- ---------- + Adelops + aegilops + alloquy + beefily beefily + begorry + billowy billowy + egilops + +Only one of them has a unique longest word. + +We will be reading a word list from standard input, and write +the longest word where the letters are in alphabetical order +to standard output. In case of ties, we print the first one found. + + ### Solutions +* [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-111/abigail/perl/ch-2.pl b/challenge-111/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..5f4041c69c --- /dev/null +++ b/challenge-111/abigail/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < word-list +# + +# +# We will grep the words from standard input which don't change +# if they are sorted; these are the words which match the pattern +# /^a*b*c*...z*$/i. We keep track of the longest word. +# +# If course, there does not have to be a unique word. It will depend +# on the word list used to search in. For instance, three different +# word list I used give different results: +# +# infochimps.com /usr/share/dict/words enable.lst +# -------------- --------------------- ---------- +# Adelops +# aegilops +# alloquy +# beefily beefily +# begorry +# billowy billowy +# egilops +# +# Only one of them has a unique longest word. +# + +# +# We will be reading a word list from standard input, and write +# the longest word where the letters are in alphabetical order +# to standard output. In case of ties, we print the first one found. +# + + +my $pat = join "" => map {"$_*"} 'a' .. 'z'; + +my $longest = ""; + +while (<>) { + $longest = $_ if /^$pat$/i && length ($_) > length ($longest) +} + +print $longest; -- cgit From 2a9661ee4386ae9a9e7caa8a0dcee83ce94b40fd Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 17:56:41 +0200 Subject: GNU AWK solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/awk/ch-2.gawk | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge-111/abigail/awk/ch-2.gawk diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 5f00924966..53215ce77d 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -50,6 +50,7 @@ to standard output. In case of ties, we print the first one found. ### Solutions +* [GNU AWK](awk/ch-2.gawk) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-111/abigail/awk/ch-2.gawk b/challenge-111/abigail/awk/ch-2.gawk new file mode 100644 index 0000000000..98ac26a066 --- /dev/null +++ b/challenge-111/abigail/awk/ch-2.gawk @@ -0,0 +1,33 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.gawk < input-file +# + +BEGIN { + IGNORECASE = 1 + longest = "" + # + # Create a pattern /^a*b*...z*$/ + # + pat = "^" + for (i = 97; i <= 122; i ++) { + pat = pat sprintf ("%c*", i) + } + pat = pat "$" +} + +# +# Match words with letters in lexical order; keep longest. +# +match ($0, pat) && length ($0) > length (longest) { + longest = $0 +} + +END { + print longest +} -- cgit From 2d04791820175079daea868135ea04cf7cc5be0c Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 18:33:56 +0200 Subject: C solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/c/ch-2.c | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 challenge-111/abigail/c/ch-2.c diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 53215ce77d..3f7fc99b5f 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -51,6 +51,7 @@ to standard output. In case of ties, we print the first one found. ### Solutions * [GNU AWK](awk/ch-2.gawk) +* [C](c/ch-2.c) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-111/abigail/c/ch-2.c b/challenge-111/abigail/c/ch-2.c new file mode 100644 index 0000000000..a677e4cad6 --- /dev/null +++ b/challenge-111/abigail/c/ch-2.c @@ -0,0 +1,60 @@ +# include +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + char * longest = NULL; + size_t longest_size = 0; + + while (getline (&line, &len, stdin) != -1) { + /* + * First check whether the word has its characters + * in lexical order. + */ + size_t i = 0; + bool in_order = 1; + while (line [i] != '\n') { + if ((i && tolower (line [i]) < tolower (line [i - 1])) || + !isalpha (line [i])) { + in_order = false; + break; + } + i ++; + } + + /* + * If we have a word with its characters in lexical order, and + * it's longer than the longest word found, keep the word. + */ + if (in_order && i > longest_size) { + longest_size = i; + if ((longest = (char *) realloc (longest, (i + 1) * sizeof (char))) + == NULL) { + perror ("Realloc failed"); + exit (1); + } + for (size_t j = 0; j <= i; j ++) { /* Copies the newline as well */ + longest [j] = line [j]; + } + longest [i + 1] = '\0'; + } + } + printf ("%s", longest); + + free (line); + free (longest); + + return (0); +} -- cgit From e2e3fac2bc03b46c8bf1400f842e6b4e4b7b70b1 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 18:49:18 +0200 Subject: Lua solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/lua/ch-2.lua | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 challenge-111/abigail/lua/ch-2.lua diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 3f7fc99b5f..88811306fc 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -52,6 +52,7 @@ to standard output. In case of ties, we print the first one found. ### Solutions * [GNU AWK](awk/ch-2.gawk) * [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-111/abigail/lua/ch-2.lua b/challenge-111/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..6beeb6a979 --- /dev/null +++ b/challenge-111/abigail/lua/ch-2.lua @@ -0,0 +1,25 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local longest = "" + +for line in io . lines () do + -- + -- Find words with their letters in lexical order, and which are + -- longer than the longest found so far. + -- + if line : lower () + : find ("^a*b*c*d*e*f*g*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$") + and line : len () > longest : len () + then longest = line + end +end + +print (longest) -- cgit From fb6114ea6265a0a8242fb07246026fe0023329d6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 19:15:30 +0200 Subject: Node.js solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/node/ch-2.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 challenge-111/abigail/node/ch-2.js diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 88811306fc..87191419b0 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -53,6 +53,7 @@ to standard output. In case of ties, we print the first one found. * [GNU AWK](awk/ch-2.gawk) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-111/abigail/node/ch-2.js b/challenge-111/abigail/node/ch-2.js new file mode 100644 index 0000000000..3d7b301b5d --- /dev/null +++ b/challenge-111/abigail/node/ch-2.js @@ -0,0 +1,23 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +let longest = "" + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => { + if (_ . match (/^a*b*c*d*e*f*g*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$/i) && + _ . length > longest . length) { + longest = _ + } +}) +. on ('close', _ => { + console . log (longest) +}) -- cgit From 01863a959124994447b60f267c0e6c516f3380d1 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 3 May 2021 19:54:29 +0200 Subject: Bash solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/bash/ch-2.sh | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 challenge-111/abigail/bash/ch-2.sh diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 87191419b0..1b8e0cd4dd 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -51,6 +51,7 @@ to standard output. In case of ties, we print the first one found. ### Solutions * [GNU AWK](awk/ch-2.gawk) +* [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) diff --git a/challenge-111/abigail/bash/ch-2.sh b/challenge-111/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..7add8bc05f --- /dev/null +++ b/challenge-111/abigail/bash/ch-2.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + + +set -f +shopt -s extglob + +pat1="*(a)*(b)*(c)*(d)*(e)*(f)*(g)*(h)*(i)*(j)*(k)*(l)*(m)" +pat2="*(n)*(o)*(p)*(q)*(r)*(s)*(t)*(u)*(v)*(w)*(x)*(y)*(z)" + +longest="" + +while read line +do lower=${line,,} # Lower case input + left=${lower/#$pat1$pat2/} # Remove pattern + # Test whether nothing left, + # and string larger longest found + if [ "X$left" == "X" -a ${#line} -gt ${#longest} ] + then longest=$line + fi +done + +echo $longest -- cgit From e674b457aa9a8912365f1ac52a1eaf4e3d386ddc Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 12:04:46 +0200 Subject: Python solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/python/ch-2.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge-111/abigail/python/ch-2.py diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 1b8e0cd4dd..eb38f91e14 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -56,6 +56,7 @@ to standard output. In case of ties, we print the first one found. * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) ### Blog diff --git a/challenge-111/abigail/python/ch-2.py b/challenge-111/abigail/python/ch-2.py new file mode 100644 index 0000000000..ce79d6b393 --- /dev/null +++ b/challenge-111/abigail/python/ch-2.py @@ -0,0 +1,35 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput +import string +import re + + +# +# Create a pattern which matches words with their characters in lexical order. +# +pat = "^" +for x in list (string . ascii_lowercase): + pat = pat + x + "*" +pat += "$" + + +# +# Match strings with their characters in lexical order, and remember +# the longest of them. +# +longest = "" +for line in fileinput . input (): + line = line . strip () + if re . match (pat, line . lower ()) and len (line) > len (longest): + longest = line + +print (longest) -- cgit From 57ec29ba5148345cc41835591e77f567d70aa348 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 12:33:07 +0200 Subject: Ruby solution for week 111, part 2 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/ruby/ch-2.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 challenge-111/abigail/ruby/ch-2.rb diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index eb38f91e14..ac8ba329ad 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -57,6 +57,7 @@ to standard output. In case of ties, we print the first one found. * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) ### Blog diff --git a/challenge-111/abigail/ruby/ch-2.rb b/challenge-111/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..afe0361969 --- /dev/null +++ b/challenge-111/abigail/ruby/ch-2.rb @@ -0,0 +1,34 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +# +# Create a pattern to match words with their characters in +# lexicographical order. +# +pat = "^" +("a" .. "z") . each do + |letter| + pat += letter + "*" +end +pat += "$" + + +# +# Find matching words, and remember the longest. +# +longest = "" +ARGF . each_line do + |line| + if line . downcase =~ /#{pat}/ && line . length > longest . length + longest = line + end +end + +puts (longest) -- cgit From c8bdf59902b53b33005f98d3538fcf57329bf7c8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 13:20:02 +0200 Subject: Perl solution for week 111, part 1 --- challenge-111/abigail/README.md | 21 ++++++++++++++ challenge-111/abigail/perl/ch-1.pl | 50 ++++++++++++++++++++++++++++++++++ challenge-111/abigail/t/ctest.ini | 1 + challenge-111/abigail/t/input-1-1 | 7 +++++ challenge-111/abigail/t/output-1-1.exp | 2 ++ 5 files changed, 81 insertions(+) create mode 100644 challenge-111/abigail/perl/ch-1.pl create mode 100644 challenge-111/abigail/t/input-1-1 create mode 100644 challenge-111/abigail/t/output-1-1.exp diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index ac8ba329ad..c72b062759 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -9,8 +9,29 @@ > efficient search algorithm. ### Notes +This challenge confuses me. We're basically asked to find a number +in a sorted list. Which in languages without hashes one would solve +with binary search (yielding an O (log N) solution), and in languages +with hashes you'd use a hash (yielding an O (1) (expected) time solution). +Sure, the hash takes linear preprocessing time, but since we're asked +to write a script, we're spending linear time reading in the data +anyway. + +Perhaps the intend was a subroutine which takes a matrix and a target +number, but that was not what is being asked. The challenge explicitly +asks for *a script*, which means we have to spend a linear amount of +time reading data anyway. So, that's what you get. + +The only part where we use the fact we are given a matrix is for the +input: the first five lines are assumed to contain the matrix. The +rest of the input is taken as numbers to search for. + +Only for language lacking hashes/maps/dictionaries/tables, we will +make use of the fact the input is sorted. For the majority of +languages, the fact input is sorted does not offer additional benefits. ### Solutions +* [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-111/abigail/perl/ch-1.pl b/challenge-111/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..00f6e2502d --- /dev/null +++ b/challenge-111/abigail/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# This challenge confuses me. We're basically asked to find a number +# in a sorted list. Which in languages without hashes one would solve +# with binary search (yielding an O (log N) solution), and in languages +# with hashes you'd use a hash (yielding an O (1) (expected) time solution). +# Sure, the hash takes linear preprocessing time, but since we're asked +# to write a script, we're spending linear time reading in the data +# anyway. +# +# Perhaps the intend was a subroutine which takes a matrix and a target +# number, but that was not what is being asked. The challenge explicitly +# asks for *a script*, which means we have to spend a linear amount of +# time reading data anyway. So, that's what you get. +# +# The only part where we use the fact we are given a matrix is for the +# input: the first five lines are assumed to contain the matrix. The +# rest of the input is taken as numbers to search for. +# + +my $MATRIX_SIZE = 5; + +# +# Read in a matrix of data +# +my %matrix; +@matrix {<> =~ /-?[0-9]+/g} = () for 1 .. $MATRIX_SIZE; + +# +# Print 0/1 depending on whether the given number is in the matrix or not. +# +chomp, say exists $matrix {$_} || 0 while <> diff --git a/challenge-111/abigail/t/ctest.ini b/challenge-111/abigail/t/ctest.ini index 0c67225c11..593491a556 100644 --- a/challenge-111/abigail/t/ctest.ini +++ b/challenge-111/abigail/t/ctest.ini @@ -4,6 +4,7 @@ # [names] +1-1 = Given example 2-1 = Enable.lst 2-2 = /usr/share/dict/words 2-3 = infochimps.com diff --git a/challenge-111/abigail/t/input-1-1 b/challenge-111/abigail/t/input-1-1 new file mode 100644 index 0000000000..fa2f7e3fa1 --- /dev/null +++ b/challenge-111/abigail/t/input-1-1 @@ -0,0 +1,7 @@ + 1 2 3 5 7 + 9 11 15 19 20 +23 24 25 29 31 +32 33 39 40 42 +45 47 48 49 50 +35 +39 diff --git a/challenge-111/abigail/t/output-1-1.exp b/challenge-111/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..0d66ea1aee --- /dev/null +++ b/challenge-111/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +0 +1 -- cgit From b90ff173699409bb267dca8f122034c8a1572df7 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 13:53:02 +0200 Subject: AWK solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/awk/ch-1.awk | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 challenge-111/abigail/awk/ch-1.awk diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index c72b062759..ce06c3891c 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -31,6 +31,7 @@ make use of the fact the input is sorted. For the majority of languages, the fact input is sorted does not offer additional benefits. ### Solutions +* [AWK](awk/ch-1.awk) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-111/abigail/awk/ch-1.awk b/challenge-111/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..c82c027708 --- /dev/null +++ b/challenge-111/abigail/awk/ch-1.awk @@ -0,0 +1,30 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +BEGIN { + MATRIX_SIZE = 5 +} + +# +# Read in the matrix first +# +NR <= MATRIX_SIZE { + for (i = 1; i <= NF; i ++) { + matrix [$i] = 1 + } +} + +# +# For the rest, print 1/0 depending on whether the input +# is in the matrix or not. +# +NR > MATRIX_SIZE { + print (matrix [$0] ? 1 : 0) +} -- cgit From a6e9ec2dfad251a3012f53fb828064d68948bf49 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 13:53:17 +0200 Subject: More tests for week 111, part 1 --- challenge-111/abigail/t/ctest.ini | 2 ++ challenge-111/abigail/t/input-1-2 | 7 +++++++ challenge-111/abigail/t/input-1-3 | 10 ++++++++++ challenge-111/abigail/t/output-1-2.exp | 2 ++ challenge-111/abigail/t/output-1-3.exp | 5 +++++ 5 files changed, 26 insertions(+) create mode 100644 challenge-111/abigail/t/input-1-2 create mode 100644 challenge-111/abigail/t/input-1-3 create mode 100644 challenge-111/abigail/t/output-1-2.exp create mode 100644 challenge-111/abigail/t/output-1-3.exp diff --git a/challenge-111/abigail/t/ctest.ini b/challenge-111/abigail/t/ctest.ini index 593491a556..0c0dfde484 100644 --- a/challenge-111/abigail/t/ctest.ini +++ b/challenge-111/abigail/t/ctest.ini @@ -5,6 +5,8 @@ [names] 1-1 = Given example +1-2 = Use large numbers +1-3 = Use negative numbers 2-1 = Enable.lst 2-2 = /usr/share/dict/words 2-3 = infochimps.com diff --git a/challenge-111/abigail/t/input-1-2 b/challenge-111/abigail/t/input-1-2 new file mode 100644 index 0000000000..14b659ea7d --- /dev/null +++ b/challenge-111/abigail/t/input-1-2 @@ -0,0 +1,7 @@ + 1000000 2000000 3000000 5000000 7000000 + 9000000 11000000 15000000 19000000 20000000 +23000000 24000000 25000000 29000000 31000000 +32000000 33000000 39000000 40000000 42000000 +45000000 47000000 48000000 49000000 50000000 +35000000 +39000000 diff --git a/challenge-111/abigail/t/input-1-3 b/challenge-111/abigail/t/input-1-3 new file mode 100644 index 0000000000..e5dd243924 --- /dev/null +++ b/challenge-111/abigail/t/input-1-3 @@ -0,0 +1,10 @@ +-9 -7 -5 -3 -2 + 0 11 15 19 20 +23 24 25 29 31 +32 33 39 40 42 +45 47 48 49 50 +-5 +-1 +0 +1 +11 diff --git a/challenge-111/abigail/t/output-1-2.exp b/challenge-111/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..0d66ea1aee --- /dev/null +++ b/challenge-111/abigail/t/output-1-2.exp @@ -0,0 +1,2 @@ +0 +1 diff --git a/challenge-111/abigail/t/output-1-3.exp b/challenge-111/abigail/t/output-1-3.exp new file mode 100644 index 0000000000..33f911e8c0 --- /dev/null +++ b/challenge-111/abigail/t/output-1-3.exp @@ -0,0 +1,5 @@ +1 +0 +1 +0 +1 -- cgit From e9ecb8b56098b11633bed3c39c85574a5f23be5b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 14:26:32 +0200 Subject: Bash solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/bash/ch-1.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-111/abigail/bash/ch-1.sh diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index ce06c3891c..e78fafe291 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -32,6 +32,7 @@ languages, the fact input is sorted does not offer additional benefits. ### Solutions * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-111/abigail/bash/ch-1.sh b/challenge-111/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..0ef20b5732 --- /dev/null +++ b/challenge-111/abigail/bash/ch-1.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +declare -A matrix +MATRIX_SIZE=5 + +# +# Read in the matrix +# +for ((i = 1; i <= $MATRIX_SIZE; i ++)) +do read -a line + for n in ${line[@]} + do matrix[$n]=1 + done +done + +# +# Print 1/0 depending or not whether the number is in the matrix +# +while read number +do echo $((matrix[$number] ? 1 : 0)) +done -- cgit From 163c81d9189cd4ac1ac4a126763b7c889df8a6f7 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 15:03:21 +0200 Subject: C solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/c/ch-1.c | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-111/abigail/c/ch-1.c diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index e78fafe291..92fe052262 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -33,6 +33,7 @@ languages, the fact input is sorted does not offer additional benefits. ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-111/abigail/c/ch-1.c b/challenge-111/abigail/c/ch-1.c new file mode 100644 index 0000000000..a8804c7390 --- /dev/null +++ b/challenge-111/abigail/c/ch-1.c @@ -0,0 +1,49 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +# define MATRIX_SIZE 5 +# define NR_OF_ELEMENTS (MATRIX_SIZE * MATRIX_SIZE) + +static int compare (const void *a, const void *b) { + return * (int *) a - * (int *) b; +} + +int main (void) { + int * matrix; + int target; + + if ((matrix = (int *) malloc (NR_OF_ELEMENTS * sizeof (int))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + + /* + * Read in the matrix + */ + for (int i = 0; i < NR_OF_ELEMENTS; i ++) { + if (scanf ("%d", &matrix [i]) != 1) { + perror ("Scanf failed"); + exit (1); + } + } + + /* + * Read in the search data, and print 1/0 depending on whether + * the read number is in the matrix or not. + */ + while (scanf ("%d", &target) == 1) { + printf ("%d\n", bsearch (&target, matrix, NR_OF_ELEMENTS, + sizeof (int), compare) == NULL ? 0 : 1); + } + + return (0); +} -- cgit From 4ead8fc53f6cd3b29d00a1bc526d1754c03217d4 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 15:19:34 +0200 Subject: Lua solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/lua/ch-1.lua | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge-111/abigail/lua/ch-1.lua diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 92fe052262..bd9ef31d26 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -34,6 +34,7 @@ languages, the fact input is sorted does not offer additional benefits. * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-111/abigail/lua/ch-1.lua b/challenge-111/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..194e55b458 --- /dev/null +++ b/challenge-111/abigail/lua/ch-1.lua @@ -0,0 +1,33 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local MATRIX_SIZE = 5 + +matrix = {} + +-- +-- Read in the matrix +-- +for i = 1, MATRIX_SIZE * MATRIX_SIZE do + matrix [io . read ("*number")] = 1 +end + +-- +-- Read in the rest, printing 1/0 depending on +-- whether the number is present in the matrix or not. +-- +while true do + target = io . read ("*number") + if target == nil then break end + if matrix [target] + then print (1) + else print (0) + end +end -- cgit From 120ddabba9a2e7583c5eeb66d4cc4a463f8cedc2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 17:18:05 +0200 Subject: Node.js solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/node/ch-1.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 challenge-111/abigail/node/ch-1.js diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index bd9ef31d26..a7ac678775 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -35,6 +35,7 @@ languages, the fact input is sorted does not offer additional benefits. * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) * [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-111/abigail/node/ch-1.js b/challenge-111/abigail/node/ch-1.js new file mode 100644 index 0000000000..6e97d03272 --- /dev/null +++ b/challenge-111/abigail/node/ch-1.js @@ -0,0 +1,37 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +let MATRIX_SIZE = 5 + +// +// Read in all the numbers, both the matrix and the target numbers +// +let numbers = + require ("fs") +. readFileSync (0) // Read all. +. toString () // Turn it into a string. +. match (/-?[0-9]+/g) + +// +// Populate the matrix +// +let matrix = {} +for (let i = 0; i < MATRIX_SIZE * MATRIX_SIZE; i ++) { + matrix [numbers [i]] = 1 +} + +// +// Check the rest of the numbers whether they're present +// in the matrix. +// +for (let j = MATRIX_SIZE * MATRIX_SIZE; j < numbers . length; j ++) { + console . log (matrix [numbers [j]] ? 1 : 0) +} + -- cgit From c740d8f54a4355f0d4b58715962541ea9c473597 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 17:49:26 +0200 Subject: Python solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/python/ch-1.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 challenge-111/abigail/python/ch-1.py diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index a7ac678775..45e924351b 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -37,6 +37,7 @@ languages, the fact input is sorted does not offer additional benefits. * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ### Blog diff --git a/challenge-111/abigail/python/ch-1.py b/challenge-111/abigail/python/ch-1.py new file mode 100644 index 0000000000..ba91c8009f --- /dev/null +++ b/challenge-111/abigail/python/ch-1.py @@ -0,0 +1,31 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +MATRIX_SIZE = 5 + +import fileinput +import re + +# +# Read in the matrix +# +matrix = {} +for i in range (MATRIX_SIZE): + for n in re . findall (r'-?[0-9]+', input ()): + matrix [n] = 1 + +# +# For the rest of the input, check whether it's in the matrix +# +for line in fileinput . input (): + if line . strip () in matrix: + print ("1") + else: + print ("0") -- cgit From 74e93b7c85b2797a877de91fe9e6eb3ad59c9650 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 4 May 2021 18:29:25 +0200 Subject: Ruby solution for week 111, part 1 --- challenge-111/abigail/README.md | 1 + challenge-111/abigail/ruby/ch-1.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 challenge-111/abigail/ruby/ch-1.rb diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md index 45e924351b..b9e45903c7 100644 --- a/challenge-111/abigail/README.md +++ b/challenge-111/abigail/README.md @@ -38,6 +38,7 @@ languages, the fact input is sorted does not offer additional benefits. * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ### Blog diff --git a/challenge-111/abigail/ruby/ch-1.rb b/challenge-111/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..ed9d9d9085 --- /dev/null +++ b/challenge-111/abigail/ruby/ch-1.rb @@ -0,0 +1,32 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +matrix_size = 5 + +# +# Read in the matrix +# +matrix = {} +(1 .. matrix_size) . each do + ARGF . readline . scan(/-?[0-9]+/) do + |number| + matrix [number] = 1 + end +end + + +# +# Read in the rest of the numbers, and print 1/0 whether or +# not they're present in the matrix. +# +ARGF . each_line do + |number| + puts (matrix [number . strip] ? 1 : 0) +end -- cgit