diff options
| author | Pok <pok@goyangi> | 2025-11-17 18:02:50 +0700 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-11-17 18:02:50 +0700 |
| commit | e80a2ae531aa79507271b69576177ff61ef6c0df (patch) | |
| tree | c2412a66ddaf3819c821229ae5459a89966fbb0e | |
| parent | 054cfeed028723380f6879241103477f3b9e4d28 (diff) | |
| download | perlweeklychallenge-club-e80a2ae531aa79507271b69576177ff61ef6c0df.tar.gz perlweeklychallenge-club-e80a2ae531aa79507271b69576177ff61ef6c0df.tar.bz2 perlweeklychallenge-club-e80a2ae531aa79507271b69576177ff61ef6c0df.zip | |
pwc348 solution in go
| -rw-r--r-- | challenge-348/pokgopun/go/ch-1.go | 98 | ||||
| -rw-r--r-- | challenge-348/pokgopun/go/ch-2.go | 109 |
2 files changed, 207 insertions, 0 deletions
diff --git a/challenge-348/pokgopun/go/ch-1.go b/challenge-348/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ff985e941a --- /dev/null +++ b/challenge-348/pokgopun/go/ch-1.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +/*# + +Task 1: String Alike + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string of even length. + + Write a script to find if the given string split into two halves of + equal lengths and they both have same number of vowels. + +Example 1 + +Input: $str = "textbook" +Output: false + +1st half: "text" (1 vowel) +2nd half: "book" (2 vowels) + +Example 2 + +Input: $str = "book" +Output: true + +1st half: "bo" (1 vowel) +2nd half: "ok" (1 vowel) + +Example 3 + +Input: $str = "AbCdEfGh" +Output: true + +1st half: "AbCd" (1 vowel) +2nd half: "EfGh" (1 vowel) + +Example 4 + +Input: $str = "rhythmmyth" +Output: false + +1st half: "rhyth" (0 vowel) +2nd half: "mmyth" (0 vowel) + +Example 5 + +Input: $str = "UmpireeAudio" +Output: false + +1st half: "Umpire" (3 vowels) +2nd half: "eAudio" (5 vowels) + +Task 2: Covert Time +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func sa(str string) bool { + h := len(str) / 2 + var cnt [2]int + d := 'a' - 'A' + for i, rs := range []string{str[:h], str[h:]} { + for _, r := range rs { + if r < 'a' { + r += d + } + switch r { + case 'a', 'e', 'i', 'o', 'u': + cnt[i]++ + } + } + } + return cnt[0] > 0 && cnt[0] == cnt[1] +} + +func main() { + for _, data := range []struct { + input string + output bool + }{ + {"textbook", false}, + {"book", true}, + {"AbCdEfGh", true}, + {"rhythmmyth", false}, + {"UmpireeAudio", false}, + } { + io.WriteString(os.Stdout, cmp.Diff(sa(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-348/pokgopun/go/ch-2.go b/challenge-348/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..6c7808a6d1 --- /dev/null +++ b/challenge-348/pokgopun/go/ch-2.go @@ -0,0 +1,109 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +/*# + +Task 2: Covert Time + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $source and $target, containing time in + 24-hour time form. + + Write a script to convert the source into target by performing one of + the following operations: +1. Add 1 minute +2. Add 5 minutes +3. Add 15 minutes +4. Add 60 minutes + + Find the total operations needed to get to the target. + +Example 1 + +Input: $source = "02:30" + $target = "02:45" +Output: 1 + +Just one operation i.e. "Add 15 minutes". + +Example 2 + +Input: $source = "11:55" + $target = "12:15" +Output: 2 + +Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". + +Example 3 + +Input: $source = "09:00" + $target = "13:00" +Output: 4 + +Four operations of "Add 60 minutes". + +Example 4 + +Input: $source = "23:45" + $target = "00:30" +Output: 3 + +Three operations of "Add 15 minutes". + +Example 5 + +Input: $source = "14:20" + $target = "15:25" +Output: 2 + +Two operations, one "Add 60 minutes" and one "Add 5 minutes" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 23rd November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "time" + + "github.com/google/go-cmp/cmp" +) + +func ct(source, target string) int { + tmpl := "15:04" + t1, _ := time.Parse(tmpl, source) + t2, _ := time.Parse(tmpl, target) + if t2.Before(t1) { + t2 = t2.AddDate(0, 0, 1) + } + m := int(t2.Sub(t1).Minutes()) + c := 0 + for _, n := range []int{60, 15, 5} { + c += m / n + m = m % n + } + return c + m +} + +func main() { + for _, data := range []struct { + source, target string + output int + }{ + {"02:30", "02:45", 1}, + {"11:55", "12:15", 2}, + {"09:00", "13:00", 4}, + {"23:45", "00:30", 3}, + {"14:20", "15:25", 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(ct(data.source, data.target), data.output)) // blank if ok, otherwise show the difference + } +} |
