diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-20 07:39:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-20 07:39:35 +0100 |
| commit | 28359c08bda75e6152a1a4e3126e09655fa1711c (patch) | |
| tree | e6dfe1d827bfd23178fc0f2453a724a64fa1dd0b /challenge-135 | |
| parent | ebd2f6d52b36f18a7fb85f9da5e38180835f6367 (diff) | |
| parent | 85e6c3b2a56ae3f4e7d1329e3aea42956a53fd10 (diff) | |
| download | perlweeklychallenge-club-28359c08bda75e6152a1a4e3126e09655fa1711c.tar.gz perlweeklychallenge-club-28359c08bda75e6152a1a4e3126e09655fa1711c.tar.bz2 perlweeklychallenge-club-28359c08bda75e6152a1a4e3126e09655fa1711c.zip | |
Merge pull request #5060 from Abigail/abigail/week-135
Abigail/week 135
Diffstat (limited to 'challenge-135')
28 files changed, 672 insertions, 18 deletions
diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 701b285f17..b0164d2fd2 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -4,40 +4,22 @@ * [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/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..f135044e49 --- /dev/null +++ b/challenge-135/abigail/awk/ch-2.awk @@ -0,0 +1,43 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +BEGIN { + ord_0 = 48 + ord_9 = 57 + ord_A = 65 + ord_Z = 90 + split ("1 3 1 7 3 9 1", w, " ") + for (i = ord_0; i <= ord_9; i ++) { + c [sprintf ("%c", i)] = i - ord_0 + } + for (i = ord_A; i <= ord_Z; i ++) { + c [sprintf ("%c", i)] = i - ord_A + } +} + + +# +# Check for the right characters, and the correct length. +# +/[^0-9BCDFGHJKLMNPQRSTVWXYZ]/ {print 0; next} +length ($0) != 7 {print 0; next} +/[^0-9]$/ {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) +} + 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..71e4c172b8 --- /dev/null +++ b/challenge-135/abigail/bash/ch-2.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +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 + elif [[ $line =~ [^0-9BCDFGHJKLMNPQRSTVWXYZ] ]] + then echo 0 + elif [[ $line =~ [^0-9]$ ]] + 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 - ord_0)) + else ((value = ord - ord_A)) + fi + ((check += ${w[i]} * value)) + done + if ((check % 10 == 0)) + then echo 1 + else echo 0 + fi + fi +done 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 <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <ctype.h> +# include <stdbool.h> + +/* + * 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..09b0b9fde9 --- /dev/null +++ b/challenge-135/abigail/c/ch-2.c @@ -0,0 +1,57 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * 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; + if ('0' <= line_ptr [i] && line_ptr [i] <= '9') { + first = '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' && + i < 6) { + first = 'A'; + } + else { + valid = 0; + } + } + check += (line_ptr [i] - first) * w [i]; + } + if (check % 10 != 0) { + valid = 0; + } + } + else { + valid = 0; + } + printf ("%d\n", valid); + } + free (line); + + return (0); +} 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..a8c89f57dc --- /dev/null +++ b/challenge-135/abigail/lua/ch-2.lua @@ -0,0 +1,35 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +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 (pat) 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") + end + check = check + w [i] * byte + end + if check % 10 == 0 + then print (1) + else print (0) + end + else + print (0) + end +end 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..2bdbe79c28 --- /dev/null +++ b/challenge-135/abigail/node/ch-2.js @@ -0,0 +1,31 @@ +#!/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]/) || + line . match (/[^0-9]$/)) { + console . log (0) + } + else { + let check = 0 + for (let i = 0; i < 7; i ++) { + 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) + } +}) 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..df6a3e18f6 --- /dev/null +++ b/challenge-135/abigail/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/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 5]}: repeat the above 5 times. +# [0-9]: last character must be digit +# +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-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..13e8e484dd --- /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]{6}[0-9]$', 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") + check = check + w [i] * val + if check % 10 == 0: + print (1) + else: + print (0) + else: + print (0) 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..920bbe1379 --- /dev/null +++ b/challenge-135/abigail/ruby/ch-2.rb @@ -0,0 +1,36 @@ +#!/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]{6}[0-9]$/) 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 + end + check += w [i] * val + end + if check % 10 == 0 then + puts (1) + else + puts (0) + end + else + puts (0) + end +end diff --git a/challenge-135/abigail/t/ctest.ini b/challenge-135/abigail/t/ctest.ini new file mode 100644 index 0000000000..e3c9965d46 --- /dev/null +++ b/challenge-135/abigail/t/ctest.ini @@ -0,0 +1,10 @@ +#
+# 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
+2-2 = Additional tests
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/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-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 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 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..dfc9abfa74 --- /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]{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] + if {[expr $val <= [scan "9" %c]]} { + set val [expr $val - [scan "0" %c]] + } else { + set val [expr $val - [scan "A" %c]] + } + set check [expr $check + [lindex $w $i] * $val] + } + if {[expr $check % 10] == 0} { + puts 1 + } else { + puts 0 + } + } else { + puts 0 + } +} |
