From 87dfd7b9a9b717fa6847ee0e0710e2798ea76ce6 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 12 Mar 2024 11:22:11 +1100 Subject: pwc260 solution in python --- challenge-260/pokgopun/python/ch-1.py | 56 +++++++++++++++++++++++++++++ challenge-260/pokgopun/python/ch-2.py | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 challenge-260/pokgopun/python/ch-1.py create mode 100644 challenge-260/pokgopun/python/ch-2.py diff --git a/challenge-260/pokgopun/python/ch-1.py b/challenge-260/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..734382ae7c --- /dev/null +++ b/challenge-260/pokgopun/python/ch-1.py @@ -0,0 +1,56 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +""" + +Task 1: Unique Occurrences + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return 1 if the number of occurrences of each value + in the given array is unique or 0 otherwise. + +Example 1 + +Input: @ints = (1,2,2,1,1,3) +Output: 1 + +The number 1 occurred 3 times. +The number 2 occurred 2 times. +The number 3 occurred 1 time. + +All occurrences are unique, therefore the output is 1. + +Example 2 + +Input: @ints = (1,2,3) +Output: 0 + +Example 3 + +Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +Output: 1 + +Task 2: Dictionary Rank +""" +### solution by pokgopun@gmail.com + +def UO(tup: tuple): + s = set(tup) + return len(s)==len(set(tup.count(e) for e in s)) + +import unittest + +class TestUO(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1,2,2,1,1,3): 1, + (1,2,3): 0, + (-2,0,1,-2,1,1,0,1,-2,9): 1, + }.items(): + self.assertEqual(UO(inpt),otpt) + +unittest.main() + + diff --git a/challenge-260/pokgopun/python/ch-2.py b/challenge-260/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..6925831197 --- /dev/null +++ b/challenge-260/pokgopun/python/ch-2.py @@ -0,0 +1,68 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +""" + +Task 2: Dictionary Rank + +Submitted by: [43]Mark Anderson + __________________________________________________________________ + + You are given a word, $word. + + Write a script to compute the dictionary rank of the given word. + +Example 1 + +Input: $word = 'CAT' +Output: 3 + +All possible combinations of the letters: +CAT, CTA, ATC, TCA, ACT, TAC + +Arrange them in alphabetical order: +ACT, ATC, CAT, CTA, TAC, TCA + +CAT is the 3rd in the list. +Therefore the dictionary rank of CAT is 3. + +Example 2 + +Input: $word = 'GOOGLE' +Output: 88 + +Example 3 + +Input: $word = 'SECRET' +Output: 255 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 17th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import permutations + +def DC(word: str): + return tuple( + sorted( + set( + permutations(word,len(word)) + ) + ) + ).index(tuple(word)) + 1 + +import unittest + +class TestDC(unittest.TestCase): + def test(self): + for inpt, otpt in { + 'CAT': 3, + 'GOOGLE': 88, + 'SECRET': 255, + }.items(): + self.assertEqual(DC(inpt),otpt) + +unittest.main() -- cgit From 867127a74fe3c558a20cba4b1cd609f8e987e4ba Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 12 Mar 2024 13:50:16 +1100 Subject: pwc260 solution in go --- challenge-260/pokgopun/go/ch-1.go | 104 ++++++++++++++++++++++++++++++++++++++ challenge-260/pokgopun/go/ch-2.go | 98 +++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 challenge-260/pokgopun/go/ch-1.go create mode 100644 challenge-260/pokgopun/go/ch-2.go diff --git a/challenge-260/pokgopun/go/ch-1.go b/challenge-260/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..a401e1588a --- /dev/null +++ b/challenge-260/pokgopun/go/ch-1.go @@ -0,0 +1,104 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +/*# + +Task 1: Unique Occurrences + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return 1 if the number of occurrences of each value + in the given array is unique or 0 otherwise. + +Example 1 + +Input: @ints = (1,2,2,1,1,3) +Output: 1 + +The number 1 occurred 3 times. +The number 2 occurred 2 times. +The number 3 occurred 1 time. + +All occurrences are unique, therefore the output is 1. + +Example 2 + +Input: @ints = (1,2,3) +Output: 0 + +Example 3 + +Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +Output: 1 + +Task 2: Dictionary Rank +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +type myMap map[int]int + +func newMyMap() myMap { + return make(myMap) +} + +func (mm myMap) proc(is ints) { + clear(mm) + for _, v := range is { + mm[v]++ + } +} + +func (mm myMap) keys() ints { + var s ints + for k := range mm { + s = append(s, k) + } + return s +} + +func (mm myMap) values() ints { + var s ints + for _, v := range mm { + s = append(s, v) + } + return s +} + +func (mm myMap) uo(is ints) bool { + mm.proc(is) + //fmt.Println("mm = ", mm) + count_uniq_elem := len(mm.keys()) + //fmt.Println("count_uniq_elem =", count_uniq_elem) + mm.proc(mm.values()) + //fmt.Println("mm = ", mm) + count_uniq_occur := len(mm.keys()) + //fmt.Println("count_uniq_occur =", count_uniq_occur) + return count_uniq_elem == count_uniq_occur +} + +func main() { + mm := newMyMap() + for _, data := range []struct { + input ints + output bool + }{ + {ints{1, 2, 2, 1, 1, 3}, true}, + {ints{1, 2, 3}, false}, + {ints{-2, 0, 1, -2, 1, 1, 0, 1, -2, 9}, true}, + } { + //fmt.Println(data.input) + io.WriteString(os.Stdout, cmp.Diff(mm.uo(data.input), data.output)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-260/pokgopun/go/ch-2.go b/challenge-260/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..aba232c3e9 --- /dev/null +++ b/challenge-260/pokgopun/go/ch-2.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +/*# + +Task 2: Dictionary Rank + +Submitted by: [43]Mark Anderson + __________________________________________________________________ + + You are given a word, $word. + + Write a script to compute the dictionary rank of the given word. + +Example 1 + +Input: $word = 'CAT' +Output: 3 + +All possible combinations of the letters: +CAT, CTA, ATC, TCA, ACT, TAC + +Arrange them in alphabetical order: +ACT, ATC, CAT, CTA, TAC, TCA + +CAT is the 3rd in the list. +Therefore the dictionary rank of CAT is 3. + +Example 2 + +Input: $word = 'GOOGLE' +Output: 88 + +Example 3 + +Input: $word = 'SECRET' +Output: 255 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 17th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func dc(word string) int { + var res string + s := strings.Split(permute(word, "", &res), " ") + slices.Sort(s) + l := len(s) + i, j := 0, l + for j > 1 { + if s[i] == s[i+1] { + copy(s[i:], s[i+1:]) + l-- + } else { + i++ + } + j-- + } + return slices.Index(s, word) + 1 +} + +func permute(s, t string, res *string) string { + if len(s) > 0 { + for i, v := range []byte(s) { + permute(s[:i]+s[i+1:], t+string(v), res) + } + } else { + *res += " " + t + return "" + } + return (*res)[1:] +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"CAT", 3}, + {"GOOGLE", 88}, + {"SECRET", 255}, + } { + io.WriteString(os.Stdout, cmp.Diff(dc(data.input), data.output)) // blank if ok, otherwise show the differences + } +} -- cgit