aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-03 11:46:28 +0100
committerGitHub <noreply@github.com>2023-10-03 11:46:28 +0100
commit80f7b23d8d1c6849a19f2030277dab5852c2e9bf (patch)
tree9aa9b8c9b8bc129688dd8706cd56e4ec43fbb1bd
parent9a50910d2d97f98fc942e1a5275016a462be8abb (diff)
parent97f2d015bd33e12ea9c82aa5d64e7a3893e9b7ea (diff)
downloadperlweeklychallenge-club-80f7b23d8d1c6849a19f2030277dab5852c2e9bf.tar.gz
perlweeklychallenge-club-80f7b23d8d1c6849a19f2030277dab5852c2e9bf.tar.bz2
perlweeklychallenge-club-80f7b23d8d1c6849a19f2030277dab5852c2e9bf.zip
Merge pull request #8798 from pokgopun/pwc237
pwc237 solution
-rw-r--r--challenge-237/pokgopun/go/ch-1.go74
-rw-r--r--challenge-237/pokgopun/go/ch-2.go103
-rw-r--r--challenge-237/pokgopun/python/ch-1.py55
-rw-r--r--challenge-237/pokgopun/python/ch-2.py62
4 files changed, 294 insertions, 0 deletions
diff --git a/challenge-237/pokgopun/go/ch-1.go b/challenge-237/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..df3cd16249
--- /dev/null
+++ b/challenge-237/pokgopun/go/ch-1.go
@@ -0,0 +1,74 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/
+/*#
+
+Task 1: Seize The Day
+
+Submitted by: [44]Mark Anderson
+ __________________________________________________________________
+
+ Given a year, a month, a weekday of month, and a day of week (1 (Mon)
+ .. 7 (Sun)), print the day.
+
+Example 1
+
+Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2
+Output: 16
+
+The 3rd Tue of Apr 2024 is the 16th
+
+Example 2
+
+Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4
+Output: 9
+
+The 2nd Thu of Oct 2025 is the 9th
+
+Example 3
+
+Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3
+Output: 0
+
+There isn't a 5th Wed in Aug 2026
+
+Task 2: Maximise Greatness
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+ for _, data := range []struct {
+ mDate mDate
+ output int
+ }{
+ {mDate{2024, 4, 3, 2}, 16},
+ {mDate{2025, 10, 2, 4}, 9},
+ {mDate{2026, 8, 5, 3}, 0},
+ } {
+ fmt.Println(data.mDate.day() == data.output)
+ }
+}
+
+type mDate struct {
+ y int
+ m time.Month
+ mw, wd int
+}
+
+func (md mDate) day() int {
+ d := time.Date(md.y, md.m, 1, 0, 0, 0, 0, time.UTC)
+ wdd := (md.wd % 7) - int(d.Weekday())
+ if wdd < 0 {
+ wdd += 7
+ }
+ d = d.AddDate(0, 0, 7*(md.mw-1)+wdd)
+ if d.Month() != md.m {
+ return 0
+ }
+ return d.Day()
+}
diff --git a/challenge-237/pokgopun/go/ch-2.go b/challenge-237/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..fc9c652ddd
--- /dev/null
+++ b/challenge-237/pokgopun/go/ch-2.go
@@ -0,0 +1,103 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/
+/*#
+
+Task 2: Maximise Greatness
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to permute the give array such that you get the maximum
+ possible greatness.
+
+ To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length
+
+Example 1
+
+Input: @nums = (1, 3, 5, 2, 1, 3, 1)
+Output: 4
+
+One possible permutation: (2, 5, 1, 3, 3, 1, 1) which returns 4 greatness as bel
+ow:
+nums[0] < perm[0]
+nums[1] < perm[1]
+nums[3] < perm[3]
+nums[4] < perm[4]
+
+Example 2
+
+Input: @ints = (1, 2, 3, 4)
+Output: 3
+
+One possible permutation: (2, 3, 4, 1) which returns 3 greatness as below:
+nums[0] < perm[0]
+nums[1] < perm[1]
+nums[2] < perm[2]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 8th October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import "fmt"
+
+func main() {
+ for _, data := range []struct {
+ input []int
+ output uint
+ }{
+ {[]int{1, 3, 5, 2, 1, 3, 1}, 4},
+ {[]int{1, 2, 3, 4}, 3},
+ } {
+ fmt.Println(mySlice(data.input).maxGreat() == data.output)
+ }
+}
+
+type mySlice []int
+
+func (ms mySlice) maxGreat() uint {
+ n := uint(len(ms))
+ fact := uint(1)
+ for i := n; i > 1; i-- {
+ fact *= i
+ }
+ var g, mg uint
+ for r := uint(0); r < fact; r++ {
+ g = ms.great(n, r, fact)
+ if mg < g {
+ mg = g
+ }
+ }
+ return mg
+}
+
+func (ms mySlice) great(n, r, fact uint) (g uint) {
+ if r > fact-1 {
+ return 0
+ }
+ fact /= n
+ digits := make([]uint, uint(n))
+ for i := uint(0); i < n; i++ {
+ digits[i] = i
+ }
+ var q uint
+ for i := uint(0); i < n; i++ {
+ q = r / fact
+ r %= fact
+ if ms[digits[q]] > ms[i] {
+ g++
+ }
+ copy(digits[q:], digits[q+1:])
+ if i != n-1 {
+ fact /= n - 1 - i
+ }
+ }
+ return g
+}
diff --git a/challenge-237/pokgopun/python/ch-1.py b/challenge-237/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..e04ed8c727
--- /dev/null
+++ b/challenge-237/pokgopun/python/ch-1.py
@@ -0,0 +1,55 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-237/
+"""
+
+Task 1: Seize The Day
+
+Submitted by: [44]Mark Anderson
+ __________________________________________________________________
+
+ Given a year, a month, a weekday of month, and a day of week (1 (Mon)
+ .. 7 (Sun)), print the day.
+
+Example 1
+
+Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2
+Output: 16
+
+The 3rd Tue of Apr 2024 is the 16th
+
+Example 2
+
+Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4
+Output: 9
+
+The 2nd Thu of Oct 2025 is the 9th
+
+Example 3
+
+Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3
+Output: 0
+
+There isn't a 5th Wed in Aug 2026
+
+Task 2: Maximise Greatness
+"""
+### solution by pokgopun@gmail.com
+
+import datetime
+
+def seizeTheDay(tup):
+ y,m,mw,wd = tup
+ dt = datetime.date(y, m, 1)
+ wdd = wd - dt.isoweekday()
+ if wdd < 0: wdd += 7
+ try:
+ return dt.replace(day = 1 + 7*(mw - 1) + wdd).day
+ except:
+ return 0
+
+for inpt,otpt in {
+ (2024,4,3,2): 16,
+ (2025,10,2,4): 9,
+ (2026,8,5,3): 0,
+ }.items():
+ print(seizeTheDay(inpt)==otpt)
+
diff --git a/challenge-237/pokgopun/python/ch-2.py b/challenge-237/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..7f52133712
--- /dev/null
+++ b/challenge-237/pokgopun/python/ch-2.py
@@ -0,0 +1,62 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-237/
+"""
+
+Task 2: Maximise Greatness
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to permute the give array such that you get the maximum
+ possible greatness.
+
+ To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length
+
+Example 1
+
+Input: @nums = (1, 3, 5, 2, 1, 3, 1)
+Output: 4
+
+One possible permutation: (2, 5, 1, 3, 3, 1, 1) which returns 4 greatness as bel
+ow:
+nums[0] < perm[0]
+nums[1] < perm[1]
+nums[3] < perm[3]
+nums[4] < perm[4]
+
+Example 2
+
+Input: @ints = (1, 2, 3, 4)
+Output: 3
+
+One possible permutation: (2, 3, 4, 1) which returns 3 greatness as below:
+nums[0] < perm[0]
+nums[1] < perm[1]
+nums[2] < perm[2]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 8th October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import permutations
+
+def maxGreat(tup):
+ l, m = len(tup), 0
+ for pm in permutations(tup):
+ g = tuple( pm[i] > tup[i] for i in range(l) ).count(True)
+ if m < g : m = g
+ return m
+
+for inpt,otpt in {
+ (1, 3, 5, 2, 1, 3, 1): 4,
+ (1, 2, 3, 4): 3,
+ }.items():
+ print(maxGreat(inpt)==otpt)
+
+