diff options
| author | Michael Manring <michael@manring> | 2023-12-05 21:54:27 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2023-12-05 21:54:27 +1100 |
| commit | 44e554f1c1efe469e79fd6045e45b605d4449a8f (patch) | |
| tree | abb890db42335d7241ccb39c1f6ded5d4917f792 | |
| parent | b41848354dc1a09ae68e01e91b551a36d13f8bda (diff) | |
| download | perlweeklychallenge-club-44e554f1c1efe469e79fd6045e45b605d4449a8f.tar.gz perlweeklychallenge-club-44e554f1c1efe469e79fd6045e45b605d4449a8f.tar.bz2 perlweeklychallenge-club-44e554f1c1efe469e79fd6045e45b605d4449a8f.zip | |
pwc246 solution in go
| -rw-r--r-- | challenge-246/pokgopun/go/ch-1.go | 103 | ||||
| -rw-r--r-- | challenge-246/pokgopun/go/ch-2.go | 123 |
2 files changed, 226 insertions, 0 deletions
diff --git a/challenge-246/pokgopun/go/ch-1.go b/challenge-246/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..919d0fb31b --- /dev/null +++ b/challenge-246/pokgopun/go/ch-1.go @@ -0,0 +1,103 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-246/ +/*# + +Task 1: 6 out of 49 + +Submitted by: [46]Andreas Voegele + __________________________________________________________________ + + 6 out of 49 is a German lottery. + + Write a script that outputs six unique random integers from the range 1 + to 49. + +Output + +3 +10 +11 +22 +38 +49 + +Task 2: Linear Recurrence of Second Order +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "fmt" + "math/rand" + "time" +) + +type gmLot struct { + rg *rand.Rand + val [6]uint8 +} + +func newGmLot() gmLot { + gl := gmLot{rg: rand.New(rand.NewSource(time.Now().UnixNano()))} + gl.update() + return gl +} + +func (gl gmLot) String() string { + return fmt.Sprint(gl.val) +} + +func (gl *gmLot) update() { + i := 0 + for _, v := range gl.rg.Perm(50)[:7] { + if v != 0 { + gl.val[i] = uint8(v) + i++ + } + if i == 6 { + break + } + } +} + +func (gl gmLot) isInRange() bool { + for _, v := range gl.val { + if v < 1 || v > 49 { + return false + } + } + return true +} + +func (gl gmLot) isUnique() bool { + m := make(map[uint8]bool) + for _, v := range gl.val { + if m[v] { + return false + } + m[v] = true + } + return true +} + +func (gl *gmLot) hasNoDupAfterN(n uint) bool { + m := make(map[[6]uint8]bool) + for i := uint(0); i < n; i++ { + if m[gl.val] { + //fmt.Println(m) + return false + } + m[gl.val] = true + //if i < n/2 { + gl.update() + //fmt.Println(gl) + //} + } + //fmt.Println(m) + return true +} + +func main() { + gl := newGmLot() + fmt.Println(gl.isInRange(), gl.isUnique(), gl.hasNoDupAfterN(100), gl) +} diff --git a/challenge-246/pokgopun/go/ch-2.go b/challenge-246/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..28a2f986fa --- /dev/null +++ b/challenge-246/pokgopun/go/ch-2.go @@ -0,0 +1,123 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-246/ +/*# + +Task 2: Linear Recurrence of Second Order + +Submitted by: [47]Jorg Sommrey + __________________________________________________________________ + + You are given an array @a of five integers. + + Write a script to decide whether the given integers form a linear + recurrence of second order with integer factors. + + A linear recurrence of second order has the form +a[n] = p * a[n-2] + q * a[n-1] with n > 1 + +where p and q must be integers. + +Example 1 + +Input: @a = (1, 1, 2, 3, 5) +Output: true + +@a is the initial part of the Fibonacci sequence a[n] = a[n-2] + a[n-1] +with a[0] = 1 and a[1] = 1. + +Example 2 + +Input: @a = (4, 2, 4, 5, 7) +Output: false + +a[1] and a[2] are even. Any linear combination of two even numbers with integer +factors is even, too. +Because a[3] is odd, the given numbers cannot form a linear recurrence of second + order with integer factors. + +Example 3 + +Input: @a = (4, 1, 2, -3, 8) +Output: true + +a[n] = a[n-2] - 2 * a[n-1] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 10th December + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import "fmt" + +type equation struct { + a, b, c int // a*p + b*q = c +} + +type answer struct { + isValid bool // true if both p and q are ints + p, q int // Only correct if isValid==true +} + +func (eqt equation) solve(aeqt equation) answer { + // q = dvd / dvs + dvd, dvs := aeqt.a*eqt.c-eqt.a*aeqt.c, aeqt.a*eqt.b-aeqt.b*eqt.a + if dvd%dvs != 0 { + return answer{false, 0, 0} + } + q := dvd / dvs + //fmt.Println(eqt, aeqt, dvd, dvs) + // p = dvd / dvs + dvd, dvs = eqt.c-eqt.b*q, eqt.a + if dvd%dvs != 0 { + return answer{false, 0, 0} + } + p := dvd / dvs + return answer{true, p, q} +} + +type equations []equation + +func newEquations(ints int5) equations { + eqts := make(equations, 3) + for i := 0; i < 3; i++ { + eqts[i] = equation{ints[i], ints[i+1], ints[i+2]} + } + return eqts +} + +func (eqts equations) isValid() bool { + anss := make(map[answer]struct{}) + for i := 0; i < 2; i++ { + ans := eqts[i].solve(eqts[i+1]) + //fmt.Println(ans) + if ans.isValid == false { + return false + } + anss[ans] = struct{}{} + if len(anss) > 1 { + return false + } + } + return true +} + +type int5 [5]int + +func main() { + for _, data := range []struct { + input int5 + output bool + }{ + {int5{1, 1, 2, 3, 5}, true}, + {int5{4, 2, 4, 5, 7}, false}, + {int5{4, 1, 2, -3, 8}, true}, + } { + fmt.Println(newEquations(data.input).isValid() == data.output) + + } +} |
