diff options
| author | Michael Manring <michael@manring> | 2024-09-02 14:53:44 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-09-02 14:53:44 +1000 |
| commit | 6c3141c0a985d70f931356c3cdd6bd2c2e72a54d (patch) | |
| tree | 1ecb9a16050a94a868bfc2a2c95e30ba83172784 | |
| parent | f0d97c322ad8201eaf3c5a95e2fb74eba1ceaa82 (diff) | |
| download | perlweeklychallenge-club-6c3141c0a985d70f931356c3cdd6bd2c2e72a54d.tar.gz perlweeklychallenge-club-6c3141c0a985d70f931356c3cdd6bd2c2e72a54d.tar.bz2 perlweeklychallenge-club-6c3141c0a985d70f931356c3cdd6bd2c2e72a54d.zip | |
pwc285 solution in go
| -rw-r--r-- | challenge-285/pokgopun/go/ch-1.go | 75 | ||||
| -rw-r--r-- | challenge-285/pokgopun/go/ch-2.go | 90 |
2 files changed, 165 insertions, 0 deletions
diff --git a/challenge-285/pokgopun/go/ch-1.go b/challenge-285/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..25e61b4565 --- /dev/null +++ b/challenge-285/pokgopun/go/ch-1.go @@ -0,0 +1,75 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-285/ +/*# + +Task 1: No Connection + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of routes, @routes. + + Write a script to find the destination with no further outgoing + connection. + +Example 1 + +Input: @routes = (["B","C"], ["D","B"], ["C","A"]) +Output: "A" + +"D" -> "B" -> "C" -> "A". +"B" -> "C" -> "A". +"C" -> "A". +"A". + +Example 2 + +Input: @routes = (["A","Z"]) +Output: "Z" + +Task 2: Making Change +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type route struct { + src, dst string +} + +type routes []route + +func (rs routes) nc() string { + m := make(map[string]bool) + for _, v := range rs { + m[v.src] = false + _, ok := m[v.dst] + if !ok { + m[v.dst] = true + } + } + for k, v := range m { + if v { + return k + } + } + return "" +} + +func main() { + for _, data := range []struct { + input routes + output string + }{ + {routes{route{"B", "C"}, route{"D", "B"}, route{"C", "A"}}, "A"}, + {routes{route{"A", "Z"}}, "Z"}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.nc(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-285/pokgopun/go/ch-2.go b/challenge-285/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..58a63ee266 --- /dev/null +++ b/challenge-285/pokgopun/go/ch-2.go @@ -0,0 +1,90 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-285/ +/*# + +Task 2: Making Change + +Submitted by: [50]David Ferrone + __________________________________________________________________ + + Compute the number of ways to make change for given amount in cents. By + using the coins e.g. Penny, Nickel, Dime, Quarter and Half-dollar, in + how many distinct ways can the total value equal to the given amount? + Order of coin selection does not matter. +A penny (P) is equal to 1 cent. +A nickel (N) is equal to 5 cents. +A dime (D) is equal to 10 cents. +A quarter (Q) is equal to 25 cents. +A half-dollar (HD) is equal to 50 cents. + +Example 1 + +Input: $amount = 9 +Ouput: 2 + +1: 9P +2: N + 4P + +Example 2 + +Input: $amount = 15 +Ouput: 6 + +1: D + 5P +2: D + N +3: 3N +4: 2N + 5P +5: N + 10P +6: 15P + +Example 3 + +Input: $amount = 100 +Ouput: 292 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 8th September + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func mc(a int) int { + c := 0 + for c50 := range a/50 + 1 { + for c25 := range (a-50*c50)/25 + 1 { + for c10 := range (a-50*c50-25*c25)/10 + 1 { + for c5 := range (a-50*c50-25*c25-10*c10)/5 + 1 { + for c1 := range (a - 50*c50 - 25*c25 - 10*c10 - 5*c5) + 1 { + if 50*c50+25*c25+10*c10+5*c5+c1 == a { + c++ + } + } + } + } + } + } + return c +} + +func main() { + for _, data := range []struct { + input, output int + }{ + {9, 2}, + {15, 6}, + {100, 292}, + } { + io.WriteString(os.Stdout, cmp.Diff(mc(data.input), data.output)) // blank if ok, otherwise show the difference + } +} |
