diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-24 22:56:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-24 22:56:17 +0100 |
| commit | a27c2066959b2fb17000ce91b31c05cebe2fb133 (patch) | |
| tree | 66e162ed4b794d597d239dcbfcf3a22a42bc5c38 | |
| parent | d4e3cf0651279c07251f2a6840a435d6f08e938f (diff) | |
| parent | 9d0c7a33b83dd4d9120862fc5932c02525d3f2c5 (diff) | |
| download | perlweeklychallenge-club-a27c2066959b2fb17000ce91b31c05cebe2fb133.tar.gz perlweeklychallenge-club-a27c2066959b2fb17000ce91b31c05cebe2fb133.tar.bz2 perlweeklychallenge-club-a27c2066959b2fb17000ce91b31c05cebe2fb133.zip | |
Merge pull request #5089 from Abigail/abigail/week-135
Abigail/week 135
| -rw-r--r-- | challenge-135/abigail/README.md | 6 | ||||
| -rw-r--r-- | challenge-135/abigail/awk/ch-1.awk | 2 | ||||
| -rw-r--r-- | challenge-135/abigail/bash/ch-1.sh | 12 | ||||
| -rw-r--r-- | challenge-135/abigail/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-135/abigail/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-135/abigail/c/ch-2.c | 2 | ||||
| -rw-r--r-- | challenge-135/abigail/go/ch-1.go | 67 | ||||
| -rw-r--r-- | challenge-135/abigail/go/ch-2.go | 62 | ||||
| -rw-r--r-- | challenge-135/abigail/java/ch-1.java | 44 | ||||
| -rw-r--r-- | challenge-135/abigail/java/ch-2.java | 45 | ||||
| -rw-r--r-- | challenge-135/abigail/lua/ch-1.lua | 2 | ||||
| -rw-r--r-- | challenge-135/abigail/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-135/abigail/scheme/ch-1.scm | 41 | ||||
| -rw-r--r-- | challenge-135/abigail/scheme/ch-2.scm | 67 |
14 files changed, 348 insertions, 17 deletions
diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index b0164d2fd2..6178e463fe 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -5,11 +5,14 @@ * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Go](go/ch-1.go) +* [Java](java/ch-1.java) * [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) +* [Scheme](scheme/ch-1.scm) * [Tcl](tcl/ch-1.tcl) ## Part 2 @@ -17,9 +20,12 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) * [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) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) * [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 index 210b27b9b0..e446ce9669 100644 --- a/challenge-135/abigail/awk/ch-1.awk +++ b/challenge-135/abigail/awk/ch-1.awk @@ -25,4 +25,4 @@ length ($0) < 3 {print "too short"; next} # Print the middle three digits # - {print substr ($0, 1 + int ((length ($0) - 3) / 2), 3)} + {print substr ($0, 1 + (length ($0) - 3) / 2, 3)} diff --git a/challenge-135/abigail/bash/ch-1.sh b/challenge-135/abigail/bash/ch-1.sh index d85a744a0c..31d8586a14 100644 --- a/challenge-135/abigail/bash/ch-1.sh +++ b/challenge-135/abigail/bash/ch-1.sh @@ -13,15 +13,11 @@ set -f while read line do line=${line/#[-+]/} # Get rid of sign if [[ $line =~ [^0-9] ]] - then - echo "not an integer" + then echo "not an integer" elif ((${#line} % 2 == 0)) - then - echo "even number of digits" + then echo "even number of digits" elif ((${#line} < 3)) - then - echo "too short" - else - echo ${line:$(((${#line} - 3) / 2)):3} + then echo "too short" + else echo ${line:$(((${#line} - 3) / 2)):3} fi done diff --git a/challenge-135/abigail/blog.txt b/challenge-135/abigail/blog.txt new file mode 100644 index 0000000000..1e0b3ac6ed --- /dev/null +++ b/challenge-135/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-135-1.html diff --git a/challenge-135/abigail/blog1.txt b/challenge-135/abigail/blog1.txt new file mode 100644 index 0000000000..e2e0291ee3 --- /dev/null +++ b/challenge-135/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-135-2.html diff --git a/challenge-135/abigail/c/ch-2.c b/challenge-135/abigail/c/ch-2.c index 09b0b9fde9..ed33f0283f 100644 --- a/challenge-135/abigail/c/ch-2.c +++ b/challenge-135/abigail/c/ch-2.c @@ -23,7 +23,7 @@ int main (void) { if (str_len == 8) { int check = 0; for (size_t i = 0; i < 7 && valid; i ++) { - char first; + char first = 0; if ('0' <= line_ptr [i] && line_ptr [i] <= '9') { first = '0'; } diff --git a/challenge-135/abigail/go/ch-1.go b/challenge-135/abigail/go/ch-1.go new file mode 100644 index 0000000000..7a8c7350b5 --- /dev/null +++ b/challenge-135/abigail/go/ch-1.go @@ -0,0 +1,67 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "bufio" + "os" + "strings" +) + +func main () { + var reader = bufio . NewReader (os. Stdin) + main_loop: + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + // + // Get rid of newline + // + text = strings . TrimRight (text, "\n") + + // + // Remove leading sign, if any + // + if (text [0:1] == "-" || text [0:1] == "+") { + text = text [1:] + } + + // + // Check if each character is a number + // + for _, rune := range text { + if rune < '0' || rune > '9' { + fmt . Print ("not an integer\n") + continue main_loop + } + } + + // + // Check for even length + // + if len (text) % 2 == 0 { + fmt . Print ("even number of digits\n") + continue main_loop + } + + // + // Long enough? + // + if len (text) < 3 { + fmt . Print ("too short\n") + continue main_loop + } + + fmt . Printf ("%s\n", text [(len (text) - 3) / 2 : + (len (text) + 3) / 2]) + } +} diff --git a/challenge-135/abigail/go/ch-2.go b/challenge-135/abigail/go/ch-2.go new file mode 100644 index 0000000000..bb8e636c00 --- /dev/null +++ b/challenge-135/abigail/go/ch-2.go @@ -0,0 +1,62 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import ( + "fmt" + "bufio" + "os" + "strings" + "regexp" +) + +func main () { + var reader = bufio . NewReader (os. Stdin) + var w = [] rune {1, 3, 1, 7, 3, 9, 1} + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + + // + // Remove trailing newline + // + text = strings . TrimRight (text, "\n") + + // + // Syntax check + // + match, _ := regexp . MatchString ( + "^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$", text) + + if !match { + fmt . Print ("0\n") + continue + } + + // + // Check sum + // + var check rune = 0 + for i, rune := range text { + if rune <= '9' { + rune -= '0' + } else { + rune -= 'A' + } + check += w [i] * rune + } + if check % 10 == 0 { + fmt . Print ("1\n") + } else { + fmt . Print ("0\n") + } + } +} diff --git a/challenge-135/abigail/java/ch-1.java b/challenge-135/abigail/java/ch-1.java new file mode 100644 index 0000000000..c22cbf3132 --- /dev/null +++ b/challenge-135/abigail/java/ch-1.java @@ -0,0 +1,44 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; +import java.util.regex.Pattern; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + try { + while (true) { + String line = scanner . nextLine (); + if (!Pattern . matches ("^[-+]?[0-9]+$", line)) { + System . out . println ("not an integer"); + continue; + } + if (Pattern . matches ("^[-+].*", line)) { + line = line . substring (1); + } + int ll = line . length (); + if (ll % 2 == 0) { + System . out . println ("even number of digits"); + continue; + } + if (ll < 3) { + System . out . println ("too short"); + continue; + } + System . out . println (line . substring ((ll - 3) / 2, + (ll + 3) / 2)); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-135/abigail/java/ch-2.java b/challenge-135/abigail/java/ch-2.java new file mode 100644 index 0000000000..477e59f8d3 --- /dev/null +++ b/challenge-135/abigail/java/ch-2.java @@ -0,0 +1,45 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; +import java.util.regex.Pattern; + +public class ch2 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + String valid = "[0-9BCDFGHJKLMNPQRSTVWXYZ]"; + String pattern = "^" + valid + valid + valid + + valid + valid + valid + "[0-9]" + "$"; + int [] w = {1, 3, 1, 7, 3, 9, 1}; + try { + while (true) { + String sedol = scanner . nextLine (); + // + // Check for the correct syntax + // + if (!Pattern . matches (pattern, sedol)) { + System . out . println (0); + continue; + } + char [] chars = sedol . toCharArray (); + int check = 0; + for (int i = 0; i < 7; i ++) { + int val = (int) chars [i]; + val -= val <= (int) '9' ? (int) '0' : (int) 'A'; + check += w [i] * val; + } + System . out . println (check % 10 == 0 ? 1 : 0); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-135/abigail/lua/ch-1.lua b/challenge-135/abigail/lua/ch-1.lua index b5db984457..d986622640 100644 --- a/challenge-135/abigail/lua/ch-1.lua +++ b/challenge-135/abigail/lua/ch-1.lua @@ -20,7 +20,7 @@ for line in io . lines () do print ("too short") else local start = 1 + (line : len () - 3) / 2 - print (line : sub (start , start + 2)) + print (line : sub (start, start + 2)) end end end diff --git a/challenge-135/abigail/perl/ch-1.pl b/challenge-135/abigail/perl/ch-1.pl index a74d8655a5..878d686567 100644 --- a/challenge-135/abigail/perl/ch-1.pl +++ b/challenge-135/abigail/perl/ch-1.pl @@ -18,11 +18,12 @@ use experimental 'lexical_subs'; # while (<>) { - s/^[-+]\s*//g; # We don't care about signs. - say /^([0-9]*)([0-9]{3})([0-9]*)$ + 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"; + ? $2 + : /^[-+]?[0-9]*[^0-9].*\n/ ? "not an integer" + : /^[-+]?(?:[0-9][0-9])*\n/ ? "even number of digits" + : "too short" } + +__END__ diff --git a/challenge-135/abigail/scheme/ch-1.scm b/challenge-135/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..1b391cd159 --- /dev/null +++ b/challenge-135/abigail/scheme/ch-1.scm @@ -0,0 +1,41 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm < input-file +;;; + +(use-modules (ice-9 regex)) +(use-modules (ice-9 rdelim)) + +(define (main) + (define line (read-line)) + (define is-number) + (define number) + (define ll) + (if (not (eof-object? line)) + (begin + (set! is-number (string-match "^[-+]?([0-9]+)$" line)) + (if (not is-number) + (display "not an integer") + (begin + (set! number (match:substring is-number 1)) + (set! ll (string-length number)) + (if (= (modulo ll 2) 0) + (display "even number of digits") + (if (< ll 3) + (display "too short") + (display (substring number (/ (- ll 3) 2) + (/ (+ ll 3) 2))) + ) + ) + ) + ) + (newline) + (main) + ) + ) +) + +(main) diff --git a/challenge-135/abigail/scheme/ch-2.scm b/challenge-135/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..f5c29b5dac --- /dev/null +++ b/challenge-135/abigail/scheme/ch-2.scm @@ -0,0 +1,67 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm < input-file +;;; + +(use-modules (ice-9 regex)) +(use-modules (ice-9 rdelim)) +(use-modules (ice-9 iconv)) +(use-modules (rnrs bytevectors)) +(use-modules (srfi srfi-1)) + +(define pat "^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$") +(define w (list 1 3 1 7 3 9 1)) + + +;; +;; Create a procedure 'byte->val'. It takes the ASCII value of a +;; character, and returns the corresponding number for the SEDOL +;; checksum (-10 for "A" .. "Z"). +;; +(define ords (bytevector->u8-list (string->bytevector "09A" "UTF-8"))) +(define ord-0 (list-ref ords 0)) +(define ord-9 (list-ref ords 1)) +(define ord-A (list-ref ords 2)) +(define (byte->val b) + (if (<= b ord-9) + (- b ord-0) + (- b ord-A))) + + +(define (main) + (define sedol (read-line)) + (define is-sedol) + (define valid 0) + (define check 0) + (define values) + (if (not (eof-object? sedol)) + (begin + (set! valid 0) + (set! check 0) + (set! is-sedol (string-match pat sedol)) + (if (regexp-match? is-sedol) + (begin + (set! values + (map-in-order byte->val + (bytevector->u8-list + (string->bytevector + (match:substring is-sedol 0) "UTF-8")))) + (set! check + (fold (lambda (weight val sum) (+ sum (* weight val))) + 0 w values)) + ;; + ;; A SEDOL is valid iff the checksum is a multiple of 10 + ;; + (if (= 0 (modulo check 10)) (set! valid 1)) + ) + ) + (display valid)(newline) + (main) + ) + ) +) + +(main) |
