aboutsummaryrefslogtreecommitdiff
path: root/challenge-234
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-17 22:37:44 +0100
committerGitHub <noreply@github.com>2023-09-17 22:37:44 +0100
commitf3fad89060c7190fe884661cdf76ab2c6d94baff (patch)
treedde7aac0e6204d584420462b8fc0c25bcd55bdcd /challenge-234
parent948cac4542eb01458759719597b425b823c51e85 (diff)
parent092cf4319860e9faaaccd6a80d19977296270af7 (diff)
downloadperlweeklychallenge-club-f3fad89060c7190fe884661cdf76ab2c6d94baff.tar.gz
perlweeklychallenge-club-f3fad89060c7190fe884661cdf76ab2c6d94baff.tar.bz2
perlweeklychallenge-club-f3fad89060c7190fe884661cdf76ab2c6d94baff.zip
Merge pull request #8691 from pokgopun/pwc234
pwc234 solution
Diffstat (limited to 'challenge-234')
-rw-r--r--challenge-234/pokgopun/go/ch-1.go87
-rw-r--r--challenge-234/pokgopun/go/ch-2.go98
-rw-r--r--challenge-234/pokgopun/python/ch-1.py67
-rw-r--r--challenge-234/pokgopun/python/ch-2.py71
4 files changed, 323 insertions, 0 deletions
diff --git a/challenge-234/pokgopun/go/ch-1.go b/challenge-234/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..ed7c9e0ee5
--- /dev/null
+++ b/challenge-234/pokgopun/go/ch-1.go
@@ -0,0 +1,87 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-234/
+/*#
+
+Task 1: Common Characters
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of words made up of alphabetic characters only.
+
+ Write a script to return all alphabetic characters that show up in all
+ words including duplicates.
+
+Example 1
+
+Input: @words = ("java", "javascript", "julia")
+Output: ("j", "a")
+
+Example 2
+
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+
+Example 3
+
+Input: @words = ("cool", "lock", "cook")
+Output: ("c", "o")
+
+Task 2: Unequal Triplets
+#*/
+
+package main
+
+import (
+ "fmt"
+ "slices"
+ "strings"
+)
+
+func main() {
+ var mw mwords
+ for _, data := range []struct {
+ input []string
+ output []rune
+ }{
+ {[]string{"java", "javascript", "julia"}, []rune{'j', 'a'}},
+ {[]string{"bella", "label", "roller"}, []rune{'e', 'l', 'l'}},
+ {[]string{"cool", "lock", "cook"}, []rune{'c', 'o'}},
+ } {
+ mw.words = data.input
+ fmt.Println(slices.Equal(mw.commonChars(), data.output))
+ }
+}
+
+type mchar struct {
+ chr rune
+ cnt int
+}
+
+type mwords struct {
+ words []string
+ word0 []mchar
+}
+
+func (mw mwords) commonChars() (r []rune) {
+ for _, c := range mw.words[0] {
+ mw.word0 = append(mw.word0, mchar{c, 1})
+ }
+ //fmt.Println(mw.word0)
+ for _, w := range mw.words[1:] {
+ for i := range mw.word0 {
+ ri := strings.IndexRune(w, mw.word0[i].chr)
+ if ri != -1 {
+ mw.word0[i].cnt++
+ w = string(slices.Delete([]rune(w), ri, ri+1))
+ }
+ }
+ }
+ //fmt.Println(mc)
+ l := len(mw.words)
+ for _, v := range mw.word0 {
+ if v.cnt == l {
+ r = append(r, v.chr)
+ }
+ }
+ return r
+}
diff --git a/challenge-234/pokgopun/go/ch-2.go b/challenge-234/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..6ce69c3cf3
--- /dev/null
+++ b/challenge-234/pokgopun/go/ch-2.go
@@ -0,0 +1,98 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-234/
+/*#
+
+Task 2: Unequal Triplets
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers.
+
+ Write a script to find the number of triplets (i, j, k) that satisfies
+ num[i] != num[j], num[j] != num[k] and num[k] != num[i].
+
+Example 1
+
+Input: @ints = (4, 4, 2, 4, 3)
+Ouput: 3
+
+(0, 2, 4) because 4 != 2 != 3
+(1, 2, 4) because 4 != 2 != 3
+(2, 3, 4) because 2 != 4 != 3
+
+Example 2
+
+Input: @ints = (1, 1, 1, 1, 1)
+Ouput: 0
+
+Example 3
+
+Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1)
+Output: 28
+
+triplets of 1, 4, 7 = 3x2×2 = 12 combinations
+triplets of 1, 4, 10 = 3×2×1 = 6 combinations
+triplets of 4, 7, 10 = 2×2×1 = 4 combinations
+triplets of 1, 7, 10 = 3x2x1 = 6 combinations
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 17th September
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+*/
+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+func main() {
+ for _, data := range []struct {
+ inpt []int
+ otpt int
+ }{
+ {[]int{4, 4, 2, 4, 3}, 3},
+ {[]int{1, 1, 1, 1, 1}, 0},
+ {[]int{4, 7, 1, 10, 7, 4, 1, 1}, 28},
+ } {
+ fmt.Println(utCount(data.inpt) == data.otpt)
+ }
+}
+
+func utCount(s []int) (count int) {
+ l := len(s)
+ m := make(map[int]int)
+ var c string
+ for _, idxs := range strings.Split(combo(3, string(ibytes(l)), "", &c, byte(l)), string(byte(l))) {
+ for _, v := range []byte(idxs) {
+ m[s[int(v)]]++
+ }
+ if len(m) == 3 {
+ count++
+ }
+ clear(m)
+ }
+ 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-234/pokgopun/python/ch-1.py b/challenge-234/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..8095c0df1b
--- /dev/null
+++ b/challenge-234/pokgopun/python/ch-1.py
@@ -0,0 +1,67 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-234/
+"""
+
+Task 1: Common Characters
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of words made up of alphabetic characters only.
+
+ Write a script to return all alphabetic characters that show up in all
+ words including duplicates.
+
+Example 1
+
+Input: @words = ("java", "javascript", "julia")
+Output: ("j", "a")
+
+Example 2
+
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+
+Example 3
+
+Input: @words = ("cool", "lock", "cook")
+Output: ("c", "o")
+
+Task 2: Unequal Triplets
+"""
+
+def commonChars(words):
+ ### store 1st word in a list of tuples to compare with the rest
+ ### we cannot simply use map here because we need to accout for individual duplicated char, see Example 2
+ ### each tuple contains a char and initial count 1
+
+ word0Chars = [ (words[0][x],1) for x in range(len(words[0])) ]
+
+ for word in words[1:]: ### the remaining words to check against word0Chars
+
+ for i in range(len(word0Chars)):
+
+ c = word0Chars[i][0] ### a char of word0 to check against word
+ j = word.find(c) ### find the char in word
+
+ if j != -1: ### if the char is found in word, increase its count in word0Chars, and then remove it from word
+
+ word0Chars[i] = (c, word0Chars[i][1]+1)
+ word = word[:j] + word[j+1:]
+
+ count = len(words) ### count of common chars, this will use to filter word0Chars and map it create common chars
+ return tuple(
+ map(
+ lambda x: x[0],
+ filter(
+ lambda x: x[1]==count,
+ word0Chars
+ )
+ )
+ )
+
+for inpt,otpt in {
+ ("java", "javascript", "julia"): ("j", "a"),
+ ("bella", "label", "roller"): ("e", "l", "l"),
+ ("cool", "lock", "cook"): ("c", "o"),
+ }.items():
+ print(commonChars(inpt)==otpt)
diff --git a/challenge-234/pokgopun/python/ch-2.py b/challenge-234/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..d7a2b1d06a
--- /dev/null
+++ b/challenge-234/pokgopun/python/ch-2.py
@@ -0,0 +1,71 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-234/
+"""
+
+Task 2: Unequal Triplets
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers.
+
+ Write a script to find the number of triplets (i, j, k) that satisfies
+ num[i] != num[j], num[j] != num[k] and num[k] != num[i].
+
+Example 1
+
+Input: @ints = (4, 4, 2, 4, 3)
+Ouput: 3
+
+(0, 2, 4) because 4 != 2 != 3
+(1, 2, 4) because 4 != 2 != 3
+(2, 3, 4) because 2 != 4 != 3
+
+Example 2
+
+Input: @ints = (1, 1, 1, 1, 1)
+Ouput: 0
+
+Example 3
+
+Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1)
+Output: 28
+
+triplets of 1, 4, 7 = 3x2×2 = 12 combinations
+triplets of 1, 4, 10 = 3×2×1 = 6 combinations
+triplets of 4, 7, 10 = 2×2×1 = 4 combinations
+triplets of 1, 7, 10 = 3x2x1 = 6 combinations
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 17th September
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+from itertools import combinations
+
+def utCount(tup):
+ return len(
+ tuple(
+ filter( ### set of unequal triplets will have 3 members
+ lambda x: x==3,
+ map( ### then count set's member
+ lambda x: len(x),
+ map( ### convert list of values to set to remove duplicated values
+ lambda x: set(x),
+ map( ### map combo of index (i,j,k) to list of correspoing values
+ lambda x: ( tup[i] for i in x ),
+ combinations(range(len(tup)),3)
+ )
+ )
+ )
+ )
+ )
+ )
+
+for inpt,otpt in {
+ (4, 4, 2, 4, 3): 3,
+ (1, 1, 1, 1, 1): 0,
+ (4, 7, 1, 10, 7, 4, 1, 1): 28,
+ }.items():
+ print(utCount(inpt)==otpt)