aboutsummaryrefslogtreecommitdiff
path: root/challenge-201
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-30 12:00:28 +0000
committerGitHub <noreply@github.com>2023-01-30 12:00:28 +0000
commit81e796219fe7ddaf5f50e72a0702297a2dee6db7 (patch)
treea4d8affd395287e26f3453acce676b5d64371da5 /challenge-201
parent5c5d6a7b81e31436de9a54f68281ae8b1b3b8c5c (diff)
parentba9d81a08e32764433aeac56eca15a42bf01296b (diff)
downloadperlweeklychallenge-club-81e796219fe7ddaf5f50e72a0702297a2dee6db7.tar.gz
perlweeklychallenge-club-81e796219fe7ddaf5f50e72a0702297a2dee6db7.tar.bz2
perlweeklychallenge-club-81e796219fe7ddaf5f50e72a0702297a2dee6db7.zip
Merge pull request #7498 from pokgopun/pwc201
Pwc201
Diffstat (limited to 'challenge-201')
-rw-r--r--challenge-201/pokgopun/go/ch-2.go36
1 files changed, 15 insertions, 21 deletions
diff --git a/challenge-201/pokgopun/go/ch-2.go b/challenge-201/pokgopun/go/ch-2.go
index 4c7b355988..b112a34289 100644
--- a/challenge-201/pokgopun/go/ch-2.go
+++ b/challenge-201/pokgopun/go/ch-2.go
@@ -28,7 +28,6 @@ import (
"io"
"log"
"os"
- "sort"
"strconv"
"strings"
)
@@ -44,35 +43,30 @@ func main() {
log.Fatal(err)
}
}
- m := make(map[string]struct{})
-
- fmt.Printf("Input: n = %d\nOutput: %d\n\nThere are %[2]d ways of stacking %[1]d pennies in ascending piles\n\n", n, penny(m, n))
var b strings.Builder
- for k := range m {
- b.WriteString("\t" + k + "\n")
- }
+ fmt.Printf("Input: n = %d\nOutput: %d\n\nThere are %[2]d ways of stacking %[1]d pennies in ascending piles\n\n", n, penny(&b, n))
io.WriteString(os.Stdout, b.String())
}
-func penny(m map[string]struct{}, n ...uint64) int {
+func penny(b *strings.Builder, n ...uint64) int {
last := n[len(n)-1]
- if last == 0 && len(n) > 1 {
- sort.SliceStable(n, func(i, j int) bool {
- return n[i] < n[j]
- })
- var b strings.Builder
- for _, v := range n[1:] {
- b.WriteString(strconv.FormatUint(v, 10) + " ")
- }
- m[b.String()] = struct{}{}
- } else {
+ if last > 0 {
s := make([]uint64, len(n)+1)
copy(s, n)
- for i := uint64(1); i <= last; i++ {
+ var first uint64 = 1
+ if len(n) > 1 {
+ first = n[len(n)-2]
+ }
+ for i := first; i <= last; i++ {
s[len(s)-2] = i
s[len(s)-1] = last - i
- penny(m, s...)
+ penny(b, s...)
+ }
+ } else if len(n) > 1 {
+ for _, v := range n[:len(n)-1] {
+ b.WriteString(strconv.FormatUint(v, 10) + " ")
}
+ b.WriteRune('\n')
}
- return len(m)
+ return strings.Count(b.String(), "\n")
}