From b6a11101a26005b4366a15e8070a82a0eec2389a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 18:14:08 +0200 Subject: Tests for week 135 --- challenge-135/abigail/t/ctest.ini | 9 +++++++++ challenge-135/abigail/t/input-1-1 | 4 ++++ challenge-135/abigail/t/input-1-2 | 3 +++ challenge-135/abigail/t/input-2-1 | 3 +++ challenge-135/abigail/t/output-1-1.exp | 4 ++++ challenge-135/abigail/t/output-1-2.exp | 3 +++ challenge-135/abigail/t/output-2-1.exp | 3 +++ 7 files changed, 29 insertions(+) create mode 100644 challenge-135/abigail/t/ctest.ini create mode 100644 challenge-135/abigail/t/input-1-1 create mode 100644 challenge-135/abigail/t/input-1-2 create mode 100644 challenge-135/abigail/t/input-2-1 create mode 100644 challenge-135/abigail/t/output-1-1.exp create mode 100644 challenge-135/abigail/t/output-1-2.exp create mode 100644 challenge-135/abigail/t/output-2-1.exp diff --git a/challenge-135/abigail/t/ctest.ini b/challenge-135/abigail/t/ctest.ini new file mode 100644 index 0000000000..e7927c1522 --- /dev/null +++ b/challenge-135/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +1-2 = More tests +2-1 = Given Examples diff --git a/challenge-135/abigail/t/input-1-1 b/challenge-135/abigail/t/input-1-1 new file mode 100644 index 0000000000..51edc43438 --- /dev/null +++ b/challenge-135/abigail/t/input-1-1 @@ -0,0 +1,4 @@ +1234567 +-123 +1 +10 diff --git a/challenge-135/abigail/t/input-1-2 b/challenge-135/abigail/t/input-1-2 new file mode 100644 index 0000000000..f10923ad73 --- /dev/null +++ b/challenge-135/abigail/t/input-1-2 @@ -0,0 +1,3 @@ +12121212 +1212121 +1,000,000 diff --git a/challenge-135/abigail/t/input-2-1 b/challenge-135/abigail/t/input-2-1 new file mode 100644 index 0000000000..fbb9fd8bda --- /dev/null +++ b/challenge-135/abigail/t/input-2-1 @@ -0,0 +1,3 @@ +2936921 +1234567 +B0YBKL9 diff --git a/challenge-135/abigail/t/output-1-1.exp b/challenge-135/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..99150bddeb --- /dev/null +++ b/challenge-135/abigail/t/output-1-1.exp @@ -0,0 +1,4 @@ +345 +123 +too short +even number of digits diff --git a/challenge-135/abigail/t/output-1-2.exp b/challenge-135/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..f9502fcb06 --- /dev/null +++ b/challenge-135/abigail/t/output-1-2.exp @@ -0,0 +1,3 @@ +even number of digits +121 +not an integer diff --git a/challenge-135/abigail/t/output-2-1.exp b/challenge-135/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..16db301bb5 --- /dev/null +++ b/challenge-135/abigail/t/output-2-1.exp @@ -0,0 +1,3 @@ +1 +0 +1 -- cgit From 9ff133bea3a7f0c19cb1e9b0171533d4cb3b7633 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 19:04:45 +0200 Subject: Perl solutions for week 135 --- challenge-135/abigail/README.md | 34 ------------------------------ challenge-135/abigail/perl/ch-1.pl | 28 +++++++++++++++++++++++++ challenge-135/abigail/perl/ch-2.pl | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 challenge-135/abigail/perl/ch-1.pl create mode 100644 challenge-135/abigail/perl/ch-2.pl diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 701b285f17..aa835b7f1e 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -2,42 +2,8 @@ ## Part 1 -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [Basic](basic/ch-1.bas) -* [bc](bc/ch-1.bc) -* [Befunge-93](befunge-93/ch-1.bf93) -* [C](c/ch-1.c) -* [csh](csh/ch-1.csh) -* [Erlang](erlang/ch-1.erl) -* [Go](go/ch-1.go) -* [Java](java/ch-1.java) -* [Lua](lua/ch-1.lua) -* [m4](m4/ch-1.m4) -* [Node.js](node/ch-1.js) -* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) -* [PHP](php/ch-1.php) -* [Python](python/ch-1.py) -* [R](r/ch-1.r) -* [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) -* [Tcl](tcl/ch-1.tcl) ## Part 2 -* [AWK](awk/ch-2.awk) -* [Bash](bash/ch-2.sh) -* [bc](bc/ch-2.bc) -* [C](c/ch-2.c) -* [Go](go/ch-2.go) -* [Java](java/ch-2.java) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) -* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) -* [R](r/ch-2.r) -* [Ruby](ruby/ch-2.rb) -* [Scheme](scheme/ch-2.scm) -* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-135/abigail/perl/ch-1.pl b/challenge-135/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..a74d8655a5 --- /dev/null +++ b/challenge-135/abigail/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/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 +# + +while (<>) { + s/^[-+]\s*//g; # We don't care about signs. + say /^([0-9]*)([0-9]{3})([0-9]*)$ + (??{length ($1) == length ($3) ? "" : "(*FAIL)"})/x + ? $2 + : length () % 2 ? "even number of digits" + : length () < 4 ? "too short" + : "not an integer"; +} diff --git a/challenge-135/abigail/perl/ch-2.pl b/challenge-135/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..8fda269604 --- /dev/null +++ b/challenge-135/abigail/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; +use experimental 'regex_sets'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +# +# Map characters to their value. +# +my %c = do {my $c = 0; map {$_ => $c ++} 0 .. 9, 'A' .. 'Z'}; + +# +# Perform the following checks: +# - All characters are either digits, or capital letters, excluding vowels +# + ASCII digits and letters +# + Y is not a vowel +# - We have exactly 7 of them. +# - We have the correct check digit +# +# ((?[[0-9A-Z]-[AEIOU]])) matches any digit or upper case ASCII character, +# except vowels; this is capture group 1. +# ((?1)): use the same subpattern as capture group 1, +# and capture it. +# @{["((?1))" x 6]}: repeat the above 6 times. +# +say /^((?[[0-9A-Z]-[AEIOU]]))@{["((?1))" x 6]}/x && +($c {$1} + 3 * $c {$2} + $c {$3} + 7 * $c {$4} + 3 * $c {$5} + 9 * $c {$6} + + $c {$7}) % 10 == 0 || 0 while <>; -- cgit From 69f1d3147c97538c8b7e385e3973b408fcf352f3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 19:45:00 +0200 Subject: C solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/c/ch-1.c | 72 +++++++++++++++++++++++++++++++++++++++++ challenge-135/abigail/c/ch-2.c | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 challenge-135/abigail/c/ch-1.c create mode 100644 challenge-135/abigail/c/ch-2.c diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index aa835b7f1e..6c58ec0530 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -2,8 +2,10 @@ ## Part 1 +* [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ## Part 2 +* [C](c/ch-2.c) * [Perl](perl/ch-2.pl) diff --git a/challenge-135/abigail/c/ch-1.c b/challenge-135/abigail/c/ch-1.c new file mode 100644 index 0000000000..13776746e5 --- /dev/null +++ b/challenge-135/abigail/c/ch-1.c @@ -0,0 +1,72 @@ +# include +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + bool done = false; + if (* line_ptr == '-' || * line_ptr == '+') { + str_len --; + line_ptr ++; + while (isspace (* line_ptr)) { + line_ptr ++; + str_len --; + } + } + /* + * Is it an integer? + */ + char * line_ptr2 = line_ptr; + while (* line_ptr2 && * line_ptr2 != '\n') { + if (!isdigit (* line_ptr2)) { + printf ("not an integer\n"); + done = true; + break; + } + line_ptr2 ++; + } + if (done) { + continue; + } + + /* + * Is the length odd? (Note the string itself still contains a newline) + */ + if (str_len % 2 == 1) { + printf ("even number of digits\n"); + continue; + } + + /* + * Long enough? + */ + if (str_len < 4) { + printf ("too short\n"); + continue; + } + + size_t mid = (str_len - 3) / 2; + for (size_t i = 0; i < 3; i ++) { + printf ("%c", line_ptr [mid + i]); + } + printf ("\n"); + } + free (line); + + return (0); +} diff --git a/challenge-135/abigail/c/ch-2.c b/challenge-135/abigail/c/ch-2.c new file mode 100644 index 0000000000..ba49db177e --- /dev/null +++ b/challenge-135/abigail/c/ch-2.c @@ -0,0 +1,59 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +short w [] = {1, 3, 1, 7, 3, 9, 1}; + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + short valid = 1; + if (str_len == 8) { + int check = 0; + for (size_t i = 0; i < 7 && valid; i ++) { + char first; + int base; + if ('0' <= line_ptr [i] && line_ptr [i] <= '9') { + first = '0'; + base = 0; + } + else { + if ('B' <= line_ptr [i] && line_ptr [i] <= 'Z' && + line_ptr [i] != 'E' && + line_ptr [i] != 'I' && + line_ptr [i] != 'O' && + line_ptr [i] != 'U') { + first = 'A'; + base = 10; + } + else { + valid = 0; + } + } + check += (line_ptr [i] - first + base) * w [i]; + } + if (check % 10 != 0) { + valid = 0; + } + } + else { + valid = 0; + } + printf ("%d\n", valid); + } + free (line); + + return (0); +} -- cgit From afb90dd6756afcd43a86249358fe3a41e3cf1950 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 20:20:18 +0200 Subject: AWK solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/awk/ch-1.awk | 28 ++++++++++++++++++++++++++++ challenge-135/abigail/awk/ch-2.awk | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 challenge-135/abigail/awk/ch-1.awk create mode 100644 challenge-135/abigail/awk/ch-2.awk diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 6c58ec0530..e0e26b70dd 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -2,10 +2,12 @@ ## Part 1 +* [AWK](awk/ch-1.awk) * [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ## Part 2 +* [AWK](awk/ch-2.awk) * [C](c/ch-2.c) * [Perl](perl/ch-2.pl) diff --git a/challenge-135/abigail/awk/ch-1.awk b/challenge-135/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..210b27b9b0 --- /dev/null +++ b/challenge-135/abigail/awk/ch-1.awk @@ -0,0 +1,28 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + + +# +# Get rid of leading sign +# +/^[-+]/ {$0 = substr ($0, 2)} + +# +# Validation +# +/[^0-9]/ {print "not an integer"; next} +/^(..)*$/ {print "even number of digits"; next} +length ($0) < 3 {print "too short"; next} + +# +# Print the middle three digits +# + + {print substr ($0, 1 + int ((length ($0) - 3) / 2), 3)} diff --git a/challenge-135/abigail/awk/ch-2.awk b/challenge-135/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..021224dfe0 --- /dev/null +++ b/challenge-135/abigail/awk/ch-2.awk @@ -0,0 +1,38 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +BEGIN { + split ("1 3 1 7 3 9 1", w, " ") + for (i = 48; i <= 57; i ++) { + c [sprintf ("%c", i)] = i - 48 + } + for (i = 65; i <= 90; i ++) { + c [sprintf ("%c", i)] = i - 65 + } +} + + +# +# Check for the right characters, and the correct length. +# +/[^0-9BCDFGHJKLMNPQRSTVWXYZ]/ {print 0; next} +length ($0) != 7 {print 0; next} + +# +# Do the check sum +# +{ + check = 0 + for (i = 1; i <= 7; i ++) { + check += w [i] * c [substr ($0, i, 1)] + } + print (check % 10 == 0 ? 1 : 0) +} + -- cgit From b29cb298a8d08a9d47d37b3d1ee7a62cc31a43d3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 21:38:38 +0200 Subject: Bash solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/bash/ch-1.sh | 27 +++++++++++++++++++++++++++ challenge-135/abigail/bash/ch-2.sh | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 challenge-135/abigail/bash/ch-1.sh create mode 100644 challenge-135/abigail/bash/ch-2.sh diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index e0e26b70dd..5d1820d670 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -3,11 +3,13 @@ ## Part 1 * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ## Part 2 * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-1.sh) * [C](c/ch-2.c) * [Perl](perl/ch-2.pl) diff --git a/challenge-135/abigail/bash/ch-1.sh b/challenge-135/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..d85a744a0c --- /dev/null +++ b/challenge-135/abigail/bash/ch-1.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read line +do line=${line/#[-+]/} # Get rid of sign + if [[ $line =~ [^0-9] ]] + then + echo "not an integer" + elif ((${#line} % 2 == 0)) + then + echo "even number of digits" + elif ((${#line} < 3)) + then + echo "too short" + else + echo ${line:$(((${#line} - 3) / 2)):3} + fi +done diff --git a/challenge-135/abigail/bash/ch-2.sh b/challenge-135/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..0d4c43e705 --- /dev/null +++ b/challenge-135/abigail/bash/ch-2.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +w=(1 3 1 7 3 9 1) + +while read line +do if ((${#line} != 7)) + then echo 0 + elif [[ $line =~ [^0-9BCDFGHJKLMNPQRSTVWXYZ] ]] + then echo 0 + else + ((check = 0)) + for ((i = 0; i < 7; i ++)) + do char=${line:$i:1} + printf -v ord %d "'$char" + if [[ $char =~ [0-9] ]] + then ((value = ord - 48)) + else ((value = ord - 65 + 10)) + fi + ((check += ${w[i]} * value)) + done + if ((check % 10 == 0)) + then echo 1 + else echo 0 + fi + fi +done -- cgit From e65a48092eba86eeb10c54a4637d0c5ee2b375da Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 22:00:56 +0200 Subject: Node.js solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/node/ch-1.js | 32 ++++++++++++++++++++++++++++++++ challenge-135/abigail/node/ch-2.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 challenge-135/abigail/node/ch-1.js create mode 100644 challenge-135/abigail/node/ch-2.js diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 5d1820d670..9affa5c624 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -5,6 +5,7 @@ * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) ## Part 2 @@ -12,4 +13,5 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-2.c) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-2.pl) diff --git a/challenge-135/abigail/node/ch-1.js b/challenge-135/abigail/node/ch-1.js new file mode 100644 index 0000000000..a16d936f9d --- /dev/null +++ b/challenge-135/abigail/node/ch-1.js @@ -0,0 +1,32 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + line = line . replace (/^[-+]/, '') . trim () + if (line . match (/[^0-9]/)) { + console . log ("not an integer") + } + else { + if (line . length % 2 == 0) { + console . log ("even number of digits") + } + else { + if (line . length < 3) { + console . log ("too short") + } + else { + console . log (line . substr ((line . length - 3) / 2, 3)) + } + } + } +}) + diff --git a/challenge-135/abigail/node/ch-2.js b/challenge-135/abigail/node/ch-2.js new file mode 100644 index 0000000000..d1ef83492a --- /dev/null +++ b/challenge-135/abigail/node/ch-2.js @@ -0,0 +1,29 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +let w = [1, 3, 1, 7, 3, 9, 1] + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + line = line . trim () + if (line . length != 7 || line . match (/[^0-9BCDFGHJKLMNPQRSTVWXYZ]/)) { + console . log (0) + } + else { + let check = 0 + for (let i = 0; i < 7; i ++) { + let value = line . charCodeAt (i) + value -= value <= 57 ? 48 : 55 + check += w [i] * value + } + console . log (check % 10 == 0 ? 1 : 0) + } +}) -- cgit From e87959f9cfc997d0b9c5e8c17293797aaaba8915 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 22:03:16 +0200 Subject: README fixes --- challenge-135/abigail/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 9affa5c624..1e8d468c68 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -11,7 +11,7 @@ ## Part 2 * [AWK](awk/ch-2.awk) -* [Bash](bash/ch-1.sh) +* [Bash](bash/ch-2.sh) * [C](c/ch-2.c) -* [Node.js](node/ch-1.js) +* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) -- cgit From 6fbe2f4e31051b2ecc881d9f476c61fe08b5b43b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 18 Oct 2021 23:44:35 +0200 Subject: Lua solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/lua/ch-1.lua | 27 +++++++++++++++++++++++++++ challenge-135/abigail/lua/ch-2.lua | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 challenge-135/abigail/lua/ch-1.lua create mode 100644 challenge-135/abigail/lua/ch-2.lua diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 1e8d468c68..9f6cb7b1ff 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -5,6 +5,7 @@ * [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) @@ -13,5 +14,6 @@ * [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) diff --git a/challenge-135/abigail/lua/ch-1.lua b/challenge-135/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..b5db984457 --- /dev/null +++ b/challenge-135/abigail/lua/ch-1.lua @@ -0,0 +1,27 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + line = line : gsub ("^[-+]%s*", "") + if line : find ("%D") then + print ("not an integer") + else + if line : len () % 2 == 0 then + print ("even number of digits") + else + if line : len () < 3 then + print ("too short") + else + local start = 1 + (line : len () - 3) / 2 + print (line : sub (start , start + 2)) + end + end + end +end diff --git a/challenge-135/abigail/lua/ch-2.lua b/challenge-135/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..e6b70b2830 --- /dev/null +++ b/challenge-135/abigail/lua/ch-2.lua @@ -0,0 +1,31 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local w = {1, 3, 1, 7, 3, 9, 1} + +for line in io . lines () do + if line : find ("^[0-9BCDFGHJKLMNPQRSTVWXYZ]*$") then + local check = 0 + for i = 1, 7 do + local byte = string . byte (line : sub (i, i)) + if byte <= string . byte ("9") + then byte = byte - string . byte ("0") + else byte = byte - string . byte ("A") + 10 + end + check = check + w [i] * byte + end + if check % 10 == 0 + then print (1) + else print (0) + end + else + print (0) + end +end -- cgit From 71d3279fed60471b894f85006f0573eba5e0e66f Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 16:40:57 +0200 Subject: Python solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/python/ch-1.py | 24 ++++++++++++++++++++++++ challenge-135/abigail/python/ch-2.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 challenge-135/abigail/python/ch-1.py create mode 100644 challenge-135/abigail/python/ch-2.py diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 9f6cb7b1ff..a69904f5c4 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -8,6 +8,7 @@ * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ## Part 2 @@ -17,3 +18,4 @@ * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) diff --git a/challenge-135/abigail/python/ch-1.py b/challenge-135/abigail/python/ch-1.py new file mode 100644 index 0000000000..451fa94c9f --- /dev/null +++ b/challenge-135/abigail/python/ch-1.py @@ -0,0 +1,24 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput +import re + +for line in fileinput . input (): + line = re . sub (r'^[-+]', '', line . strip ()) + ll = len (line) + if re . search (r'[^0-9]', line): + print ("not an integer") + elif ll % 2 == 0: + print ("even number of digits") + elif ll < 3: + print ("too short") + else: + print (line [(ll - 3) // 2 : (ll + 3) // 2]) diff --git a/challenge-135/abigail/python/ch-2.py b/challenge-135/abigail/python/ch-2.py new file mode 100644 index 0000000000..93ba2921e2 --- /dev/null +++ b/challenge-135/abigail/python/ch-2.py @@ -0,0 +1,32 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput +import re + +w = [1, 3, 1, 7, 3, 9, 1] + +for line in fileinput . input (): + line = line . strip () + if re . search (r'^[0-9BCDFGHJKLMNPQRSTVWXYZ]{7}$', line): + check = 0 + for i in range (len (line)): + val = ord (line [i : i + 1]) + if val <= ord ("9"): + val = val - ord ("0") + else: + val = val - ord ("A") + 10 + check = check + w [i] * val + if check % 10 == 0: + print (1) + else: + print (0) + else: + print (0) -- cgit From 5f51c70957a90c3ea098217e195c8f84dd87d389 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 17:33:42 +0200 Subject: Ruby solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/ruby/ch-1.rb | 24 ++++++++++++++++++++++++ challenge-135/abigail/ruby/ch-2.rb | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 challenge-135/abigail/ruby/ch-1.rb create mode 100644 challenge-135/abigail/ruby/ch-2.rb diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index a69904f5c4..ef6c3b18c8 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -9,6 +9,7 @@ * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ## Part 2 @@ -19,3 +20,4 @@ * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) diff --git a/challenge-135/abigail/ruby/ch-1.rb b/challenge-135/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..508907eb97 --- /dev/null +++ b/challenge-135/abigail/ruby/ch-1.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + | line | + line = line . strip() . gsub(/^[-+]/, "") + ll = line . length + if line . match (/[^0-9]/) then + puts ("not an integer") + elsif ll % 2 == 0 then + puts ("even number of digits") + elsif ll < 3 then + puts ("too short") + else + puts (line [((ll - 3) / 2) . to_i .. ((ll + 2) / 2) . to_i]) + end +end diff --git a/challenge-135/abigail/ruby/ch-2.rb b/challenge-135/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..e77981d64a --- /dev/null +++ b/challenge-135/abigail/ruby/ch-2.rb @@ -0,0 +1,37 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +w = [1, 3, 1, 7, 3, 9, 1] + +ARGF . each_line do + | line | + line . strip! + if line . match (/^[0-9BCDFGHJKLMNPQRSTVWXYZ]/) then + check = 0 + line . split('') . each_with_index do + | char, i | + val = char . ord + if val <= "9" . ord then + val -= "0" . ord + else + val -= "A" . ord + val += 10 + end + check += w [i] * val + end + if check % 10 == 0 then + puts (1) + else + puts (0) + end + else + puts (0) + end +end -- cgit From ec0548d9d1f8f277a0cd78d5a626c4efdd42f3ef Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 20:02:55 +0200 Subject: Tcl solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/tcl/ch-1.tcl | 23 +++++++++++++++++++++++ challenge-135/abigail/tcl/ch-2.tcl | 31 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 challenge-135/abigail/tcl/ch-1.tcl create mode 100644 challenge-135/abigail/tcl/ch-2.tcl diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index ef6c3b18c8..b0164d2fd2 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -10,6 +10,7 @@ * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [Ruby](ruby/ch-1.rb) +* [Tcl](tcl/ch-1.tcl) ## Part 2 @@ -21,3 +22,4 @@ * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) * [Ruby](ruby/ch-2.rb) +* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-135/abigail/tcl/ch-1.tcl b/challenge-135/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..b4024a86a5 --- /dev/null +++ b/challenge-135/abigail/tcl/ch-1.tcl @@ -0,0 +1,23 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin line] >= 0} { + if {[regexp {^[-+]?([0-9]+)$} $line -> digits]} { + set ll [string length $digits] + if {[expr $ll % 2] == 0} { + puts "even number of digits" + } elseif {[expr $ll < 3]} { + puts "too short" + } else { + puts [string range $digits [expr ($ll - 3) / 2]\ + [expr ($ll + 2) / 2]] + } + } else { + puts "not an integer" + } +} diff --git a/challenge-135/abigail/tcl/ch-2.tcl b/challenge-135/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..9df349e33b --- /dev/null +++ b/challenge-135/abigail/tcl/ch-2.tcl @@ -0,0 +1,31 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-2.tcl < input-file +# + +set w {1 3 1 7 3 9 1} + +while {[gets stdin line] >= 0} { + if {[regexp {^[0-9BCDFGHJKLMNPQRSTVWXYZ]{7}$} $line]} { + set check 0 + for {set i 0} {$i < [string length $line]} {incr i} { + set val [scan [string index $line $i] %c] + if {[expr $val <= [scan "9" %c]]} { + set val [expr $val - [scan "0" %c]] + } else { + set val [expr $val - [scan "A" %c] + 10] + } + set check [expr $check + [lindex $w $i] * $val] + } + if {[expr $check % 10] == 0} { + puts 1 + } else { + puts 0 + } + } else { + puts 0 + } +} -- cgit From 210e271ec3014cf6f08e9ce5d3e2ffbd92a61619 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 20:13:33 +0200 Subject: Last character of a SEDOL must be a number. Without this check, it could validate the SEDOL which uses a letter as a check digit. For instance, if the check digit is a '1', using 'B' would validate as well. --- challenge-135/abigail/awk/ch-2.awk | 1 + challenge-135/abigail/bash/ch-2.sh | 2 ++ challenge-135/abigail/c/ch-2.c | 3 ++- challenge-135/abigail/lua/ch-2.lua | 6 +++++- challenge-135/abigail/node/ch-2.js | 4 +++- challenge-135/abigail/perl/ch-2.pl | 5 +++-- challenge-135/abigail/python/ch-2.py | 2 +- challenge-135/abigail/ruby/ch-2.rb | 2 +- challenge-135/abigail/tcl/ch-2.tcl | 2 +- 9 files changed, 19 insertions(+), 8 deletions(-) diff --git a/challenge-135/abigail/awk/ch-2.awk b/challenge-135/abigail/awk/ch-2.awk index 021224dfe0..8da795ccf7 100644 --- a/challenge-135/abigail/awk/ch-2.awk +++ b/challenge-135/abigail/awk/ch-2.awk @@ -24,6 +24,7 @@ BEGIN { # /[^0-9BCDFGHJKLMNPQRSTVWXYZ]/ {print 0; next} length ($0) != 7 {print 0; next} +/[^0-9]$/ {print 0; next} # # Do the check sum diff --git a/challenge-135/abigail/bash/ch-2.sh b/challenge-135/abigail/bash/ch-2.sh index 0d4c43e705..d79226883a 100644 --- a/challenge-135/abigail/bash/ch-2.sh +++ b/challenge-135/abigail/bash/ch-2.sh @@ -17,6 +17,8 @@ do if ((${#line} != 7)) then echo 0 elif [[ $line =~ [^0-9BCDFGHJKLMNPQRSTVWXYZ] ]] then echo 0 + elif [[ $line =~ [^0-9]$ ]] + then echo 0 else ((check = 0)) for ((i = 0; i < 7; i ++)) diff --git a/challenge-135/abigail/c/ch-2.c b/challenge-135/abigail/c/ch-2.c index ba49db177e..27295d6bc9 100644 --- a/challenge-135/abigail/c/ch-2.c +++ b/challenge-135/abigail/c/ch-2.c @@ -34,7 +34,8 @@ int main (void) { line_ptr [i] != 'E' && line_ptr [i] != 'I' && line_ptr [i] != 'O' && - line_ptr [i] != 'U') { + line_ptr [i] != 'U' && + i < 6) { first = 'A'; base = 10; } diff --git a/challenge-135/abigail/lua/ch-2.lua b/challenge-135/abigail/lua/ch-2.lua index e6b70b2830..d979fbe2a0 100644 --- a/challenge-135/abigail/lua/ch-2.lua +++ b/challenge-135/abigail/lua/ch-2.lua @@ -10,8 +10,12 @@ local w = {1, 3, 1, 7, 3, 9, 1} +local valid = "[0-9BCDFGHJKLMNPQRSTVWXYZ]" +local pat = "^" .. valid .. valid .. valid .. + valid .. valid .. valid .. "[0-9]$" + for line in io . lines () do - if line : find ("^[0-9BCDFGHJKLMNPQRSTVWXYZ]*$") then + if line : find (pat) then local check = 0 for i = 1, 7 do local byte = string . byte (line : sub (i, i)) diff --git a/challenge-135/abigail/node/ch-2.js b/challenge-135/abigail/node/ch-2.js index d1ef83492a..8085ca0f6b 100644 --- a/challenge-135/abigail/node/ch-2.js +++ b/challenge-135/abigail/node/ch-2.js @@ -14,7 +14,9 @@ let w = [1, 3, 1, 7, 3, 9, 1] . createInterface ({input: process . stdin}) . on ('line', line => { line = line . trim () - if (line . length != 7 || line . match (/[^0-9BCDFGHJKLMNPQRSTVWXYZ]/)) { + if (line . length != 7 || + line . match (/[^0-9BCDFGHJKLMNPQRSTVWXYZ]/) || + line . match (/[^0-9]$/)) { console . log (0) } else { diff --git a/challenge-135/abigail/perl/ch-2.pl b/challenge-135/abigail/perl/ch-2.pl index 8fda269604..df6a3e18f6 100644 --- a/challenge-135/abigail/perl/ch-2.pl +++ b/challenge-135/abigail/perl/ch-2.pl @@ -35,8 +35,9 @@ my %c = do {my $c = 0; map {$_ => $c ++} 0 .. 9, 'A' .. 'Z'}; # except vowels; this is capture group 1. # ((?1)): use the same subpattern as capture group 1, # and capture it. -# @{["((?1))" x 6]}: repeat the above 6 times. +# @{["((?1))" x 5]}: repeat the above 5 times. +# [0-9]: last character must be digit # -say /^((?[[0-9A-Z]-[AEIOU]]))@{["((?1))" x 6]}/x && +say /^((?[[0-9A-Z]-[AEIOU]]))@{["((?1))" x 5]}([0-9])$/x && ($c {$1} + 3 * $c {$2} + $c {$3} + 7 * $c {$4} + 3 * $c {$5} + 9 * $c {$6} + $c {$7}) % 10 == 0 || 0 while <>; diff --git a/challenge-135/abigail/python/ch-2.py b/challenge-135/abigail/python/ch-2.py index 93ba2921e2..13ab812ae0 100644 --- a/challenge-135/abigail/python/ch-2.py +++ b/challenge-135/abigail/python/ch-2.py @@ -15,7 +15,7 @@ w = [1, 3, 1, 7, 3, 9, 1] for line in fileinput . input (): line = line . strip () - if re . search (r'^[0-9BCDFGHJKLMNPQRSTVWXYZ]{7}$', line): + if re . search (r'^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$', line): check = 0 for i in range (len (line)): val = ord (line [i : i + 1]) diff --git a/challenge-135/abigail/ruby/ch-2.rb b/challenge-135/abigail/ruby/ch-2.rb index e77981d64a..da2f131c28 100644 --- a/challenge-135/abigail/ruby/ch-2.rb +++ b/challenge-135/abigail/ruby/ch-2.rb @@ -13,7 +13,7 @@ w = [1, 3, 1, 7, 3, 9, 1] ARGF . each_line do | line | line . strip! - if line . match (/^[0-9BCDFGHJKLMNPQRSTVWXYZ]/) then + if line . match (/^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$/) then check = 0 line . split('') . each_with_index do | char, i | diff --git a/challenge-135/abigail/tcl/ch-2.tcl b/challenge-135/abigail/tcl/ch-2.tcl index 9df349e33b..045e1f2d07 100644 --- a/challenge-135/abigail/tcl/ch-2.tcl +++ b/challenge-135/abigail/tcl/ch-2.tcl @@ -9,7 +9,7 @@ set w {1 3 1 7 3 9 1} while {[gets stdin line] >= 0} { - if {[regexp {^[0-9BCDFGHJKLMNPQRSTVWXYZ]{7}$} $line]} { + if {[regexp {^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$} $line]} { set check 0 for {set i 0} {$i < [string length $line]} {incr i} { set val [scan [string index $line $i] %c] -- cgit From 32ffa2beeb07181afb6af80ebd749bb8629bf636 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 20:26:23 +0200 Subject: More tests for week 135/part 2 --- challenge-135/abigail/t/ctest.ini | 1 + challenge-135/abigail/t/input-2-2 | 4 ++++ challenge-135/abigail/t/output-2-2.exp | 4 ++++ 3 files changed, 9 insertions(+) create mode 100644 challenge-135/abigail/t/input-2-2 create mode 100644 challenge-135/abigail/t/output-2-2.exp diff --git a/challenge-135/abigail/t/ctest.ini b/challenge-135/abigail/t/ctest.ini index e7927c1522..e3c9965d46 100644 --- a/challenge-135/abigail/t/ctest.ini +++ b/challenge-135/abigail/t/ctest.ini @@ -7,3 +7,4 @@ 1-1 = Given Examples 1-2 = More tests 2-1 = Given Examples +2-2 = Additional tests diff --git a/challenge-135/abigail/t/input-2-2 b/challenge-135/abigail/t/input-2-2 new file mode 100644 index 0000000000..733544de82 --- /dev/null +++ b/challenge-135/abigail/t/input-2-2 @@ -0,0 +1,4 @@ +000000 +0000000 +00000000 +293692B diff --git a/challenge-135/abigail/t/output-2-2.exp b/challenge-135/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..5a4d7b6ab7 --- /dev/null +++ b/challenge-135/abigail/t/output-2-2.exp @@ -0,0 +1,4 @@ +0 +1 +0 +0 -- cgit From 3f9069255c9ab3f2a600ce465fa08e2cb2edc57d Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 22:02:01 +0200 Subject: Might as well map 'A' to 0. The SEDOL specification says 'A' maps to 10, 'B' to 11, etc. However, to perform a checksum, all we care about is module 10 arithmetic. So, whether 'A' maps to 10, or to 0, doesn't matter. --- challenge-135/abigail/bash/ch-2.sh | 2 +- challenge-135/abigail/c/ch-2.c | 5 +---- challenge-135/abigail/lua/ch-2.lua | 2 +- challenge-135/abigail/node/ch-2.js | 2 +- challenge-135/abigail/python/ch-2.py | 2 +- challenge-135/abigail/ruby/ch-2.rb | 1 - challenge-135/abigail/tcl/ch-2.tcl | 2 +- 7 files changed, 6 insertions(+), 10 deletions(-) diff --git a/challenge-135/abigail/bash/ch-2.sh b/challenge-135/abigail/bash/ch-2.sh index d79226883a..0cb9fca74f 100644 --- a/challenge-135/abigail/bash/ch-2.sh +++ b/challenge-135/abigail/bash/ch-2.sh @@ -26,7 +26,7 @@ do if ((${#line} != 7)) printf -v ord %d "'$char" if [[ $char =~ [0-9] ]] then ((value = ord - 48)) - else ((value = ord - 65 + 10)) + else ((value = ord - 65)) fi ((check += ${w[i]} * value)) done diff --git a/challenge-135/abigail/c/ch-2.c b/challenge-135/abigail/c/ch-2.c index 27295d6bc9..09b0b9fde9 100644 --- a/challenge-135/abigail/c/ch-2.c +++ b/challenge-135/abigail/c/ch-2.c @@ -24,10 +24,8 @@ int main (void) { int check = 0; for (size_t i = 0; i < 7 && valid; i ++) { char first; - int base; if ('0' <= line_ptr [i] && line_ptr [i] <= '9') { first = '0'; - base = 0; } else { if ('B' <= line_ptr [i] && line_ptr [i] <= 'Z' && @@ -37,13 +35,12 @@ int main (void) { line_ptr [i] != 'U' && i < 6) { first = 'A'; - base = 10; } else { valid = 0; } } - check += (line_ptr [i] - first + base) * w [i]; + check += (line_ptr [i] - first) * w [i]; } if (check % 10 != 0) { valid = 0; diff --git a/challenge-135/abigail/lua/ch-2.lua b/challenge-135/abigail/lua/ch-2.lua index d979fbe2a0..a8c89f57dc 100644 --- a/challenge-135/abigail/lua/ch-2.lua +++ b/challenge-135/abigail/lua/ch-2.lua @@ -21,7 +21,7 @@ for line in io . lines () do local byte = string . byte (line : sub (i, i)) if byte <= string . byte ("9") then byte = byte - string . byte ("0") - else byte = byte - string . byte ("A") + 10 + else byte = byte - string . byte ("A") end check = check + w [i] * byte end diff --git a/challenge-135/abigail/node/ch-2.js b/challenge-135/abigail/node/ch-2.js index 8085ca0f6b..f91f2d9420 100644 --- a/challenge-135/abigail/node/ch-2.js +++ b/challenge-135/abigail/node/ch-2.js @@ -23,7 +23,7 @@ let w = [1, 3, 1, 7, 3, 9, 1] let check = 0 for (let i = 0; i < 7; i ++) { let value = line . charCodeAt (i) - value -= value <= 57 ? 48 : 55 + value -= value <= 57 ? 48 : 65 check += w [i] * value } console . log (check % 10 == 0 ? 1 : 0) diff --git a/challenge-135/abigail/python/ch-2.py b/challenge-135/abigail/python/ch-2.py index 13ab812ae0..13e8e484dd 100644 --- a/challenge-135/abigail/python/ch-2.py +++ b/challenge-135/abigail/python/ch-2.py @@ -22,7 +22,7 @@ for line in fileinput . input (): if val <= ord ("9"): val = val - ord ("0") else: - val = val - ord ("A") + 10 + val = val - ord ("A") check = check + w [i] * val if check % 10 == 0: print (1) diff --git a/challenge-135/abigail/ruby/ch-2.rb b/challenge-135/abigail/ruby/ch-2.rb index da2f131c28..920bbe1379 100644 --- a/challenge-135/abigail/ruby/ch-2.rb +++ b/challenge-135/abigail/ruby/ch-2.rb @@ -22,7 +22,6 @@ ARGF . each_line do val -= "0" . ord else val -= "A" . ord - val += 10 end check += w [i] * val end diff --git a/challenge-135/abigail/tcl/ch-2.tcl b/challenge-135/abigail/tcl/ch-2.tcl index 045e1f2d07..dfc9abfa74 100644 --- a/challenge-135/abigail/tcl/ch-2.tcl +++ b/challenge-135/abigail/tcl/ch-2.tcl @@ -16,7 +16,7 @@ while {[gets stdin line] >= 0} { if {[expr $val <= [scan "9" %c]]} { set val [expr $val - [scan "0" %c]] } else { - set val [expr $val - [scan "A" %c] + 10] + set val [expr $val - [scan "A" %c]] } set check [expr $check + [lindex $w $i] * $val] } -- cgit From 85e6c3b2a56ae3f4e7d1329e3aea42956a53fd10 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 22:07:44 +0200 Subject: Remove hardcoded values --- challenge-135/abigail/awk/ch-2.awk | 12 ++++++++---- challenge-135/abigail/bash/ch-2.sh | 7 +++++-- challenge-135/abigail/node/ch-2.js | 6 +++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/challenge-135/abigail/awk/ch-2.awk b/challenge-135/abigail/awk/ch-2.awk index 8da795ccf7..f135044e49 100644 --- a/challenge-135/abigail/awk/ch-2.awk +++ b/challenge-135/abigail/awk/ch-2.awk @@ -9,12 +9,16 @@ # BEGIN { + ord_0 = 48 + ord_9 = 57 + ord_A = 65 + ord_Z = 90 split ("1 3 1 7 3 9 1", w, " ") - for (i = 48; i <= 57; i ++) { - c [sprintf ("%c", i)] = i - 48 + for (i = ord_0; i <= ord_9; i ++) { + c [sprintf ("%c", i)] = i - ord_0 } - for (i = 65; i <= 90; i ++) { - c [sprintf ("%c", i)] = i - 65 + for (i = ord_A; i <= ord_Z; i ++) { + c [sprintf ("%c", i)] = i - ord_A } } diff --git a/challenge-135/abigail/bash/ch-2.sh b/challenge-135/abigail/bash/ch-2.sh index 0cb9fca74f..71e4c172b8 100644 --- a/challenge-135/abigail/bash/ch-2.sh +++ b/challenge-135/abigail/bash/ch-2.sh @@ -12,6 +12,9 @@ set -f w=(1 3 1 7 3 9 1) +printf -v ord_0 %d "'0" +printf -v ord_A %d "'A" + while read line do if ((${#line} != 7)) then echo 0 @@ -25,8 +28,8 @@ do if ((${#line} != 7)) do char=${line:$i:1} printf -v ord %d "'$char" if [[ $char =~ [0-9] ]] - then ((value = ord - 48)) - else ((value = ord - 65)) + then ((value = ord - ord_0)) + else ((value = ord - ord_A)) fi ((check += ${w[i]} * value)) done diff --git a/challenge-135/abigail/node/ch-2.js b/challenge-135/abigail/node/ch-2.js index f91f2d9420..2bdbe79c28 100644 --- a/challenge-135/abigail/node/ch-2.js +++ b/challenge-135/abigail/node/ch-2.js @@ -22,9 +22,9 @@ let w = [1, 3, 1, 7, 3, 9, 1] else { let check = 0 for (let i = 0; i < 7; i ++) { - let value = line . charCodeAt (i) - value -= value <= 57 ? 48 : 65 - check += w [i] * value + let val = line . charCodeAt (i) + val -= (val <= "9" . charCodeAt (0) ? "0" : "A") . charCodeAt (0) + check += w [i] * val } console . log (check % 10 == 0 ? 1 : 0) } -- cgit