diff options
| author | Abigail <abigail@abigail.be> | 2021-10-20 20:58:09 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-10-20 20:58:09 +0200 |
| commit | a69ebee867247a05f53192ea01af5f08f2259b15 (patch) | |
| tree | cb9ce6541a428e105b43bf2ed2a0a6333266638d | |
| parent | 85e6c3b2a56ae3f4e7d1329e3aea42956a53fd10 (diff) | |
| download | perlweeklychallenge-club-a69ebee867247a05f53192ea01af5f08f2259b15.tar.gz perlweeklychallenge-club-a69ebee867247a05f53192ea01af5f08f2259b15.tar.bz2 perlweeklychallenge-club-a69ebee867247a05f53192ea01af5f08f2259b15.zip | |
Go solutions for week 135
| -rw-r--r-- | challenge-135/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-135/abigail/go/ch-1.go | 67 | ||||
| -rw-r--r-- | challenge-135/abigail/go/ch-2.go | 62 |
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index b0164d2fd2..14b229d5a9 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) +* [Go](go/ch-1.go) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) @@ -17,6 +18,7 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) +* [Go](go/ch-2.go) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) 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") + } + } +} |
