From d44f394b07c8ad6cc7eb8811aed4a22aa0486fa2 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Thu, 2 Nov 2023 18:01:55 +1100 Subject: pwc241 solution in python --- challenge-241/pokgopun/python/ch-1.py | 55 +++++++++++++++++++++++++++++++++++ challenge-241/pokgopun/python/ch-2.py | 47 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 challenge-241/pokgopun/python/ch-1.py create mode 100644 challenge-241/pokgopun/python/ch-2.py diff --git a/challenge-241/pokgopun/python/ch-1.py b/challenge-241/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..7a50712254 --- /dev/null +++ b/challenge-241/pokgopun/python/ch-1.py @@ -0,0 +1,55 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-241/ +""" + +Task 1: Arithmetic Triplets + +Submitted by: [42]Mohammad S Anwar + __________________________________________________________________ + + You are given an array (3 or more members) of integers in increasing + order and a positive integer. + + Write a script to find out the number of unique Arithmetic Triplets + satisfying the following rules: +a) i < j < k +b) nums[j] - nums[i] == diff +c) nums[k] - nums[j] == diff + +Example 1 + +Input: @nums = (0, 1, 4, 6, 7, 10) + $diff = 3 +Output: 2 + +Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3 +. +Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3 +. + +Example 2 + +Input: @nums = (4, 5, 6, 7, 8, 9) + $diff = 2 +Output: 2 + +(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2. +(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2. + +Task 2: Prime Order +""" +### solution by pokgopun@gmail.com + +from itertools import combinations + +def cntArmtcTrplt(nums: tuple, diff: int): + return len( + tuple( + filter(lambda x: x[1] - x[0] == diff and 2*x[1] == x[0] + x[2], combinations(nums,3)) + ) + ) + +for (num, diff), count in { + ((0, 1, 4, 6, 7, 10), 3): 2, + ((4, 5, 6, 7, 8, 9), 2): 2, + }.items(): + print(cntArmtcTrplt(num,diff)==count) diff --git a/challenge-241/pokgopun/python/ch-2.py b/challenge-241/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..dbaa1480d2 --- /dev/null +++ b/challenge-241/pokgopun/python/ch-2.py @@ -0,0 +1,47 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-241/ +""" + +Task 2: Prime Order + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of unique positive integers greater than 2. + + Write a script to sort them in ascending order of the count of their + prime factors, tie-breaking by ascending value. + +Example 1 + +Input: @int = (11, 8, 27, 4) +Output: (11, 4, 8, 27)) + +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 27 => 3, 3, 3 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 5th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from sympy.ntheory import factorint + +def sortByFactorCount(tup: tuple): + return tuple( + sorted( + sorted(tup), key = lambda x: sum(factorint(x).values()) + ) + ) + +for inpt,otpt in { + (11, 8, 27, 4): (11, 4, 8, 27), + (11, 27, 8, 4): (11, 4, 8, 27), + }.items(): + print(sortByFactorCount(inpt)==otpt) + -- cgit From ce244b1de7a6f16c60f11cfba07b20404bbcd874 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Fri, 3 Nov 2023 02:33:37 +1100 Subject: pwc241 solution in go --- challenge-241/pokgopun/go/ch-1.go | 95 +++++++++++++++++++++++++++++++++++++++ challenge-241/pokgopun/go/ch-2.go | 90 +++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 challenge-241/pokgopun/go/ch-1.go create mode 100644 challenge-241/pokgopun/go/ch-2.go diff --git a/challenge-241/pokgopun/go/ch-1.go b/challenge-241/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..8d71e33aef --- /dev/null +++ b/challenge-241/pokgopun/go/ch-1.go @@ -0,0 +1,95 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-241/ +/*# + +Task 1: Arithmetic Triplets + +Submitted by: [42]Mohammad S Anwar + __________________________________________________________________ + + You are given an array (3 or more members) of integers in increasing + order and a positive integer. + + Write a script to find out the number of unique Arithmetic Triplets + satisfying the following rules: +a) i < j < k +b) nums[j] - nums[i] == diff +c) nums[k] - nums[j] == diff + +Example 1 + +Input: @nums = (0, 1, 4, 6, 7, 10) + $diff = 3 +Output: 2 + +Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3 +. +Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3 +. + +Example 2 + +Input: @nums = (4, 5, 6, 7, 8, 9) + $diff = 2 +Output: 2 + +(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2. +(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2. + +Task 2: Prime Order +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "fmt" + "strings" +) + +func main() { + for _, data := range []struct { + nums []int + diff, count int + }{ + {[]int{0, 1, 4, 6, 7, 10}, 3, 2}, + {[]int{4, 5, 6, 7, 8, 9}, 2, 2}, + } { + fmt.Println(uatCount(data.nums, data.diff) == data.count) + } +} + +func uatCount(s []int, d int) (count int) { + l := len(s) + t := make([]int, 3) + var c string + for _, idxs := range strings.Split(combo(3, string(ibytes(l)), "", &c, byte(l)), string(byte(l))) { + for i, v := range []byte(idxs) { + t[i] = s[int(v)] + } + if t[1]-t[0] == d && 2*t[1] == t[0]+t[2] { + count++ + } + } + return count +} + +func ibytes(n int) []byte { + b := make([]byte, n) + for i := 0; i < n; i++ { + b[i] = byte(i) + } + return b +} + +func combo(r int, e, c string, res *string, sep byte) string { + lc, le := len(c), len(e) + if lc == r || lc+le == r { + *res += string(sep) + (c + e)[:r] + return "" + } else { + for i := 0; i <= lc+le-r; i++ { + combo(r, e[i+1:], c+string(e[i]), res, sep) + } + } + return (*res)[1:] +} diff --git a/challenge-241/pokgopun/go/ch-2.go b/challenge-241/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..70eb50b581 --- /dev/null +++ b/challenge-241/pokgopun/go/ch-2.go @@ -0,0 +1,90 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-241/ +/*# + +Task 2: Prime Order + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of unique positive integers greater than 2. + + Write a script to sort them in ascending order of the count of their + prime factors, tie-breaking by ascending value. + +Example 1 + +Input: @int = (11, 8, 27, 4) +Output: (11, 4, 8, 27)) + +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 27 => 3, 3, 3 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 5th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "cmp" + "fmt" + "math/big" + "slices" + + "github.com/jbarham/primegen" +) + +type processor struct { + pg *primegen.Primegen +} + +// func (p processor) factor() (s []uint64) { +func (p processor) countFactor(n uint64) (count uint64) { + d := p.pg.Next() + for { + if n%d != 0 { + d = p.pg.Next() + } else { + //s = append(s, d) + count++ + n /= d + if n == 1 { + break + } else if big.NewInt(int64(n)).ProbablyPrime(0) { + //s = append(s, n) + count++ + break + } + } + } + p.pg.Reset() + //return s + return count +} + +func (p processor) pfSort(s []uint64) []uint64 { + slices.Sort(s) + slices.SortStableFunc(s, func(a, b uint64) int { + return cmp.Compare(p.countFactor(a), p.countFactor(b)) + }) + return s +} + +func main() { + p := processor{pg: primegen.New()} + for _, data := range []struct { + inpt, otpt []uint64 + }{ + {[]uint64{11, 8, 27, 4}, []uint64{11, 4, 8, 27}}, + {[]uint64{11, 27, 8, 4}, []uint64{11, 4, 8, 27}}, + } { + fmt.Println(slices.Equal(p.pfSort(data.inpt), data.otpt)) + } +} -- cgit