diff options
| author | Michael Firkins <michael@firkins> | 2023-01-30 00:52:54 +0700 |
|---|---|---|
| committer | Michael Firkins <michael@firkins> | 2023-01-30 00:52:54 +0700 |
| commit | 94ce4e6196804beb215f951f6acb9dfdb2cdb24f (patch) | |
| tree | 9617d842ff8a6e486513f847863c9660f4735dd2 /challenge-201 | |
| parent | b0a72f0e06997be68a7cd9413885e99c21c4ab8e (diff) | |
| download | perlweeklychallenge-club-94ce4e6196804beb215f951f6acb9dfdb2cdb24f.tar.gz perlweeklychallenge-club-94ce4e6196804beb215f951f6acb9dfdb2cdb24f.tar.bz2 perlweeklychallenge-club-94ce4e6196804beb215f951f6acb9dfdb2cdb24f.zip | |
pwc201/go
Diffstat (limited to 'challenge-201')
| -rw-r--r-- | challenge-201/pokgopun/go/ch-1.go | 69 | ||||
| -rw-r--r-- | challenge-201/pokgopun/go/ch-2.go | 74 |
2 files changed, 143 insertions, 0 deletions
diff --git a/challenge-201/pokgopun/go/ch-1.go b/challenge-201/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..2ceb351c86 --- /dev/null +++ b/challenge-201/pokgopun/go/ch-1.go @@ -0,0 +1,69 @@ +/* + https://theweeklychallenge.org/blog/perl-weekly-challenge-201/ + +Task 1: Missing Numbers +Submitted by: Mohammad S Anwar +You are given an array of unique numbers. + +Write a script to find out all missing numbers in the range 0..$n where $n is the array size. + +Example 1 +Input: @array = (0,1,3) +Output: 2 + +The array size i.e. total element count is 3, so the range is 0..3. +The missing number is 2 in the given array. + +Example 2 +Input: @array = (0,1) +Output: 2 + +The array size is 2, therefore the range is 0..2. +The missing number is 2. +*/ +package main + +import ( + "fmt" + "log" + "os" + "sort" + "strconv" + "strings" +) + +func main() { + a := []uint64{} + var ( + i uint64 + err error + ) + for _, v := range os.Args[1:] { + i, err = strconv.ParseUint(v, 10, 64) + if err != nil { + log.Fatal(err) + } + a = append(a, i) + } + if len(a) == 0 { + a = []uint64{0, 1, 3} + } + alen := len(a) + sort.SliceStable(a, func(i, j int) bool { + return a[i] < a[j] + }) + s := make([]bool, alen+1) + for _, v := range a { + if int(v) > alen { + break + } + s[v] = true + } + var b strings.Builder + for i := range s { + if s[i] == false { + b.WriteString(", " + strconv.Itoa(i)) + } + } + fmt.Printf("Input: array = %v\nOutput: %v\n", a, b.String()[2:]) +} diff --git a/challenge-201/pokgopun/go/ch-2.go b/challenge-201/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..91b872ee9a --- /dev/null +++ b/challenge-201/pokgopun/go/ch-2.go @@ -0,0 +1,74 @@ +/* + https://theweeklychallenge.org/blog/perl-weekly-challenge-201/ + +Task 2: Penny Piles +Submitted by: Robbie Hatley +You are given an integer, $n > 0. + +Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right. + +Example +Input: $n = 5 +Output: 7 + +Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: + + 1 1 1 1 1 + 1 1 1 2 + 1 2 2 + 1 1 3 + 2 3 + 1 4 + 5 +*/ +package main + +import ( + "fmt" + "log" + "os" + "sort" + "strconv" + "strings" +) + +func main() { + var ( + n uint64 = 5 + err error + ) + if len(os.Args) > 1 { + n, err = strconv.ParseUint(os.Args[1], 10, 64) + if err != nil { + 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)) + for k := range m { + fmt.Println(k) + } +} + +func penny(m map[string]struct{}, 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 { + s := make([]uint64, len(n)+1) + copy(s, n) + for i := uint64(1); i <= last; i++ { + s[len(s)-2] = i + s[len(s)-1] = last - i + penny(m, s...) + } + } + return len(m) +} |
