diff options
| author | Pok <pok@goyangi> | 2025-06-16 18:25:28 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-06-16 18:25:28 +1000 |
| commit | 4e9d659a989936ba745eea63d1169734a7721436 (patch) | |
| tree | d73cf8c3df5f31a82c7ee7e8875c1cceb6079c59 | |
| parent | e20fda73f366076e5adebd3469ed9b5b82db9bc0 (diff) | |
| download | perlweeklychallenge-club-4e9d659a989936ba745eea63d1169734a7721436.tar.gz perlweeklychallenge-club-4e9d659a989936ba745eea63d1169734a7721436.tar.bz2 perlweeklychallenge-club-4e9d659a989936ba745eea63d1169734a7721436.zip | |
pwc326 solution in go
| -rw-r--r-- | challenge-326/pokgopun/go/ch-1.go | 63 | ||||
| -rw-r--r-- | challenge-326/pokgopun/go/ch-2.go | 79 |
2 files changed, 142 insertions, 0 deletions
diff --git a/challenge-326/pokgopun/go/ch-1.go b/challenge-326/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..4dc41b705a --- /dev/null +++ b/challenge-326/pokgopun/go/ch-1.go @@ -0,0 +1,63 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-326/ +/*# + +Task 1: Day of the Year + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a date in the format YYYY-MM-DD. + + Write a script to find day number of the year that the given date + represent. + +Example 1 + +Input: $date = '2025-02-02' +Output: 33 + +The 2nd Feb, 2025 is the 33rd day of the year. + +Example 2 + +Input: $date = '2025-04-10' +Output: 100 + +Example 3 + +Input: $date = '2025-09-07' +Output: 250 + +Task 2: Decompressed List +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "time" + + "github.com/google/go-cmp/cmp" +) + +const shortForm = "2006-01-02" + +func doy(str string) int { + t, _ := time.Parse(shortForm, str) + return t.YearDay() +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"2025-02-02", 33}, + {"2025-04-10", 100}, + {"2025-09-07", 250}, + } { + io.WriteString(os.Stdout, cmp.Diff(doy(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-326/pokgopun/go/ch-2.go b/challenge-326/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a2c2d22806 --- /dev/null +++ b/challenge-326/pokgopun/go/ch-2.go @@ -0,0 +1,79 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-326/ +/*# + +Task 2: Decompressed List + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of positive integers having even elements. + + Write a script to to return the decompress list. To decompress, pick + adjacent pair (i, j) and replace it with j, i times. + +Example 1 + +Input: @ints = (1, 3, 2, 4) +Output: (3, 4, 4) + +Pair 1: (1, 3) => 3 one time => (3) +Pair 2: (2, 4) => 4 two times => (4, 4) + +Example 2 + +Input: @ints = (1, 1, 2, 2) +Output: (1, 2, 2) + +Pair 1: (1, 1) => 1 one time => (1) +Pair 2: (2, 2) => 2 two times => (2, 2) + +Example 3 + +Input: @ints = (3, 1, 3, 2) +Output: (1, 1, 1, 2, 2, 2) + +Pair 1: (3, 1) => 1 three times => (1, 1, 1) +Pair 2: (3, 2) => 2 three times => (2, 2, 2) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 22nd June 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) process() ints { + var out ints + for i := range len(in) / 2 { + j := in[2*i] + for j > 0 { + j-- + out = append(out, in[2*i+1]) + } + } + return out +} + +func main() { + for _, data := range []struct { + input, output ints + }{ + {ints{1, 3, 2, 4}, ints{3, 4, 4}}, + {ints{1, 1, 2, 2}, ints{1, 2, 2}}, + {ints{3, 1, 3, 2}, ints{1, 1, 1, 2, 2, 2}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} |
