aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreax <eax@alvar3z.com>2021-10-24 21:31:37 -0400
committereax <eax@alvar3z.com>2021-10-24 21:31:37 -0400
commit9274f44434ba10615b534189970bfcc96a5ed037 (patch)
treef0c174f84c4f6fe6950d5358d16f0c6057e4a174
parentee2924132a7b0a54a6792bf050b8667b6c8852cf (diff)
downloadperlweeklychallenge-club-9274f44434ba10615b534189970bfcc96a5ed037.tar.gz
perlweeklychallenge-club-9274f44434ba10615b534189970bfcc96a5ed037.tar.bz2
perlweeklychallenge-club-9274f44434ba10615b534189970bfcc96a5ed037.zip
ealvar3z solution for week 135
-rw-r--r--challenge-135/ealvar3z/go/ch-1.go69
-rw-r--r--challenge-135/ealvar3z/go/ch-2.go67
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-135/ealvar3z/go/ch-1.go b/challenge-135/ealvar3z/go/ch-1.go
new file mode 100644
index 0000000000..696c122d24
--- /dev/null
+++ b/challenge-135/ealvar3z/go/ch-1.go
@@ -0,0 +1,69 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "strings"
+)
+
+// TASK #1: Middle 3-digits
+// You are given an integer.
+// Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible
+// error.
+
+// Example 1
+// Input: $n = 1234567
+// Output: 345
+
+// Example 2
+// Input: $n = -123
+// Output: 123
+
+// Example 3
+// Input: $n = 1
+// Output: too short
+
+// Example 4
+// Input: $n = 10
+// Output: even number of digits
+
+func main() {
+ r := bufio.NewReader(os.Stdin)
+
+ // labeled loop for easier debugging
+repeat:
+ for {
+ txt, err := r.ReadString('\n')
+ if err != nil {
+ break
+ }
+ // strip the newline
+ txt = strings.TrimRight(txt, "\n")
+
+ // remove the leading sign, if there are any
+ if txt[0:1] == "-" || txt[0:1] == "+" {
+ txt = txt[1:]
+ }
+
+ // check if char is a num
+ for _, c := range txt {
+ if c < '0' || c > '9' {
+ fmt.Println("not an integer")
+ continue repeat
+ }
+ }
+ // is it an even num?
+ if len(txt)%2 == 0 {
+ fmt.Println("even number of digits")
+ continue repeat
+ }
+ // how long is this things?
+ if len(txt) < 3 {
+ fmt.Println("too short")
+ continue repeat
+ }
+ // snatch the 3 middle ones
+ fmt.Printf("%+v\n", txt[(len(txt)-3)/2:(len(txt)+3)/2])
+ }
+}
diff --git a/challenge-135/ealvar3z/go/ch-2.go b/challenge-135/ealvar3z/go/ch-2.go
new file mode 100644
index 0000000000..261847beac
--- /dev/null
+++ b/challenge-135/ealvar3z/go/ch-2.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "regexp"
+ "strings"
+)
+
+// You are given 7-characters alphanumeric SEDOL.
+// Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL otherwise 0.
+// For more information about SEDOL, please checkout the wikipedia page.
+
+// Example 1
+// Input: $SEDOL = '2936921'
+// Output: 1
+
+// Example 2
+// Input: $SEDOL = '1234567'
+// Output: 0
+
+// Example 3
+// Input: $SEDOL = 'B0YBKL9'
+// Output: 1
+
+func main() {
+ r := bufio.NewReader(os.Stdin)
+ // set the weights per the wikipedia page
+ w := []rune{1, 3, 1, 7, 3, 9, 1}
+
+ for {
+ txt, err := r.ReadString('\n')
+ if err != nil {
+ break
+ }
+ // strip new lines
+ txt = strings.TrimRight(txt, "\n")
+
+ // check for syntax
+ m, _ := regexp.MatchString("^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$", txt)
+
+ if !m {
+ fmt.Println("Invalid SEDOL Number")
+ continue
+ }
+ // check the sum
+ var check rune = 0
+ for i, rune := range txt {
+ if rune <= '9' {
+ // too much magic in that example
+ // i'll simply substract from the
+ // upper limit of the rune
+ rune -= '0'
+ } else {
+ rune -= 'A'
+ }
+ // bounce it against the weights
+ check += w[i] * rune
+ }
+ if check%10 == 0 {
+ fmt.Println("1")
+ } else {
+ fmt.Println("0")
+ }
+ }
+}