aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-18 20:03:42 +0100
committerGitHub <noreply@github.com>2024-06-18 20:03:42 +0100
commitc2c9d9de7b7dd618a5805db87e6e47427062fe07 (patch)
tree87e0e06d25b41b3944afbf2737717f31667b222e
parente6d972342bc4131550f955ef5238b785bfa64205 (diff)
parenta4783013a466a8448139068b537d72a9e8a5f86f (diff)
downloadperlweeklychallenge-club-c2c9d9de7b7dd618a5805db87e6e47427062fe07.tar.gz
perlweeklychallenge-club-c2c9d9de7b7dd618a5805db87e6e47427062fe07.tar.bz2
perlweeklychallenge-club-c2c9d9de7b7dd618a5805db87e6e47427062fe07.zip
Merge pull request #10280 from pokgopun/pwc274
pwc274 solution in python
-rw-r--r--challenge-274/pokgopun/go/ch-1.go88
-rw-r--r--challenge-274/pokgopun/go/ch-2.go133
-rw-r--r--challenge-274/pokgopun/python/ch-1.py58
-rw-r--r--challenge-274/pokgopun/python/ch-2.py77
4 files changed, 356 insertions, 0 deletions
diff --git a/challenge-274/pokgopun/go/ch-1.go b/challenge-274/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..58e0323d58
--- /dev/null
+++ b/challenge-274/pokgopun/go/ch-1.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-274/
+/*#
+
+Task 1: Goat Latin
+
+Submitted by: [57]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a sentence, $sentance.
+
+ Write a script to convert the given sentence to Goat Latin, a made up
+ language similar to Pig Latin.
+
+ Rules for Goat Latin:
+1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append
+ "ma" to the end of the word.
+2) If a word begins with consonant i.e. not a vowel, remove first
+ letter and append it to the end then add "ma".
+3) Add letter "a" to the end of first word in the sentence, "aa" to
+ the second word, etc etc.
+
+Example 1
+
+Input: $sentence = "I love Perl"
+Output: "Imaa ovelmaaa erlPmaaaa"
+
+Example 2
+
+Input: $sentence = "Perl and Raku are friends"
+Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa"
+
+Example 3
+
+Input: $sentence = "The Weekly Challenge"
+Output: "heTmaa eeklyWmaaa hallengeCmaaaa"
+
+Task 2: Bus Route
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func isVowel(b byte) bool {
+ for _, v := range []byte("aeiouAEIOU") {
+ if b == v {
+ return true
+ }
+ }
+ return false
+}
+
+func goatLatin(sntnce string) string {
+ var b strings.Builder
+ for i, wrd := range strings.Split(sntnce, " ") {
+ b.WriteRune(' ')
+ if !isVowel(wrd[0]) {
+ b.WriteString(wrd[1:])
+ b.WriteByte(wrd[0])
+ } else {
+ b.WriteString(wrd)
+ }
+ b.WriteRune('m')
+ for j := 0; j < i+2; j++ {
+ b.WriteRune('a')
+ }
+ }
+ return b.String()[1:]
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output string
+ }{
+ {"I love Perl", "Imaa ovelmaaa erlPmaaaa"},
+ {"Perl and Raku are friends", "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa"},
+ {"The Weekly Challenge", "heTmaa eeklyWmaaa hallengeCmaaaa"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(goatLatin(data.input), data.output)) // blank if ok, otherwise show the differences
+ }
+}
diff --git a/challenge-274/pokgopun/go/ch-2.go b/challenge-274/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..223543c096
--- /dev/null
+++ b/challenge-274/pokgopun/go/ch-2.go
@@ -0,0 +1,133 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-274/
+/*#
+
+Task 2: Bus Route
+
+Submitted by: [58]Peter Campbell Smith
+ __________________________________________________________________
+
+ Several bus routes start from a bus stop near my home, and go to the
+ same stop in town. They each run to a set timetable, but they take
+ different times to get into town.
+
+ Write a script to find the times - if any - I should let one bus leave
+ and catch a strictly later one in order to get into town strictly
+ sooner.
+
+ An input timetable consists of the service interval, the offset within
+ the hour, and the duration of the trip.
+
+Example 1
+
+Input: [ [12, 11, 41], [15, 5, 35] ]
+Output: [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]
+
+Route 1 leaves every 12 minutes, starting at 11 minutes past the hour (so 11, 23
+, ...) and takes 41 minutes. Route 2 leaves every 15 minutes, starting at 5 minu
+tes past (5, 20, ...) and takes 35 minutes.
+
+At 45 minutes past the hour I could take the route 1 bus at 47 past the hour, ar
+riving at 28 minutes past the following hour, but if I wait for the route 2 bus
+at 50 past I will get to town sooner, at 25 minutes past the next hour.
+
+Example 2
+
+Input: [ [[12, 3, 41], [15, 9, 35], [30, 5, 25] ]
+Output: [ 0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
+, 55, 56, 57, 58, 59 ]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 23rd June 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type busInfo struct {
+ freq, start, dur int
+}
+
+func busRoute(bis ...busInfo) []int {
+ t2d := make(map[int]int)
+ var (
+ dur, tm int
+ ok bool
+ bi busInfo
+ )
+ for _, bi = range bis {
+ tm = bi.start
+ for tm < 60 {
+ dur, ok = t2d[tm]
+ if !ok || bi.dur < dur {
+ t2d[tm] = bi.dur
+ }
+ tm += bi.freq
+ }
+ }
+ l := len(t2d)
+ bs := make([]string, l)
+ var i, j, k, v int
+ for k, v = range t2d {
+ bs[i] = string([]byte{byte(k), byte(v)})
+ i++
+ }
+ slices.Sort(bs)
+ var (
+ br []int
+ d, g [2]byte
+ t [3]byte
+ gap byte
+ )
+ for i = 0; i < l; i++ {
+ for j = 0; j < 3; j++ {
+ t[j] = bs[(i+j)%l][0] + 60*byte((i+j)/l)
+ if j > 0 {
+ g[j-1] = (t[j] - t[j-1]) % 60
+ d[j-1] = bs[(i+j)%l][1]
+ }
+ }
+ if g[1]+d[1] < d[0] {
+ for gap = range g[0] {
+ br = append(br, int((t[0]+gap+1)%60))
+ }
+ }
+ }
+ slices.Sort(br)
+ return br
+}
+
+func main() {
+ for _, data := range []struct {
+ input []busInfo
+ output []int
+ }{
+ {
+ []busInfo{
+ busInfo{12, 11, 41},
+ busInfo{15, 5, 35},
+ },
+ []int{36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47},
+ },
+ {
+ []busInfo{
+ busInfo{12, 3, 41},
+ busInfo{15, 9, 35},
+ busInfo{30, 5, 25},
+ },
+ []int{0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 55, 56, 57, 58, 59},
+ },
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(busRoute(data.input...), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-274/pokgopun/python/ch-1.py b/challenge-274/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..83016c0f0b
--- /dev/null
+++ b/challenge-274/pokgopun/python/ch-1.py
@@ -0,0 +1,58 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-274/
+"""
+
+Task 1: Goat Latin
+
+Submitted by: [57]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a sentence, $sentance.
+
+ Write a script to convert the given sentence to Goat Latin, a made up
+ language similar to Pig Latin.
+
+ Rules for Goat Latin:
+1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append
+ "ma" to the end of the word.
+2) If a word begins with consonant i.e. not a vowel, remove first
+ letter and append it to the end then add "ma".
+3) Add letter "a" to the end of first word in the sentence, "aa" to
+ the second word, etc etc.
+
+Example 1
+
+Input: $sentence = "I love Perl"
+Output: "Imaa ovelmaaa erlPmaaaa"
+
+Example 2
+
+Input: $sentence = "Perl and Raku are friends"
+Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa"
+
+Example 3
+
+Input: $sentence = "The Weekly Challenge"
+Output: "heTmaa eeklyWmaaa hallengeCmaaaa"
+
+Task 2: Bus Route
+"""
+### solution by pokgopun@gmail.com
+
+def goatLatin(sentence: str):
+ ws = [ e + "m" if e[0] in "aeiouAEIOU" else e[1:] + e[0] + "m" for e in sentence.split()]
+ return " ".join(
+ ws[i]+"a"*(i+2) for i in range(len(ws))
+ )
+
+import unittest
+
+class TestGoatLatin(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ "I love Perl": "Imaa ovelmaaa erlPmaaaa",
+ "Perl and Raku are friends": "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa",
+ "The Weekly Challenge": "heTmaa eeklyWmaaa hallengeCmaaaa",
+ }.items():
+ self.assertEqual(goatLatin(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-274/pokgopun/python/ch-2.py b/challenge-274/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..314d959187
--- /dev/null
+++ b/challenge-274/pokgopun/python/ch-2.py
@@ -0,0 +1,77 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-274/
+"""
+
+Task 2: Bus Route
+
+Submitted by: [58]Peter Campbell Smith
+ __________________________________________________________________
+
+ Several bus routes start from a bus stop near my home, and go to the
+ same stop in town. They each run to a set timetable, but they take
+ different times to get into town.
+
+ Write a script to find the times - if any - I should let one bus leave
+ and catch a strictly later one in order to get into town strictly
+ sooner.
+
+ An input timetable consists of the service interval, the offset within
+ the hour, and the duration of the trip.
+
+Example 1
+
+Input: [ [12, 11, 41], [15, 5, 35] ]
+Output: [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]
+
+Route 1 leaves every 12 minutes, starting at 11 minutes past the hour (so 11, 23
+, ...) and takes 41 minutes. Route 2 leaves every 15 minutes, starting at 5 minu
+tes past (5, 20, ...) and takes 35 minutes.
+
+At 45 minutes past the hour I could take the route 1 bus at 47 past the hour, ar
+riving at 28 minutes past the following hour, but if I wait for the route 2 bus
+at 50 past I will get to town sooner, at 25 minutes past the next hour.
+
+Example 2
+
+Input: [ [[12, 3, 41], [15, 9, 35], [30, 5, 25] ]
+Output: [ 0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
+, 55, 56, 57, 58, 59 ]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 23rd June 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def busRoute(sch):
+ t2d = dict()
+ for offst, strt, drtn in sch:
+ for t in range(strt, 60, offst):
+ if t2d.get(t) == None or drtn < t2d[t]:
+ t2d[t] = drtn
+ tt = list()
+ t2d = sorted(t2d.items())
+ for i in range(len(t2d)):
+ t0, t1, t2 = t2d[i-2][0], t2d[i-1][0], t2d[i][0]
+ gap0 = (t1 - t0) % 60
+ gap = (t2 - t1) % 60
+ dur1 = t2d[i-1][1]
+ dur2 = t2d[i][1]
+ if gap + dur2 < dur1:
+ for g in range(gap0):
+ tt.append((t0 + g + 1) % 60)
+ tt.sort()
+ return tt
+
+import unittest
+
+class TestBusRoute(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ ((12,11,41),(15,5,35)): [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
+ ((12, 3, 41), (15, 9, 35), (30, 5, 25)): [ 0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 , 55, 56, 57, 58, 59 ],
+ }.items():
+ self.assertEqual(busRoute(inpt),otpt)
+
+unittest.main()