diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-24 13:03:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-24 13:03:35 +0100 |
| commit | 7d977904d2fd35d6bd17868a725e9eb312e92e04 (patch) | |
| tree | bd8116471cfe4ff34dcf2d7c16a0b9e146db0b32 | |
| parent | b66d98af00713b84c1917037c67c623ca8207476 (diff) | |
| parent | a91aab38d898f0eb964c8e1c8e1d47ba9ca8b354 (diff) | |
| download | perlweeklychallenge-club-7d977904d2fd35d6bd17868a725e9eb312e92e04.tar.gz perlweeklychallenge-club-7d977904d2fd35d6bd17868a725e9eb312e92e04.tar.bz2 perlweeklychallenge-club-7d977904d2fd35d6bd17868a725e9eb312e92e04.zip | |
Merge pull request #9984 from pokgopun/pwc266
pwc266 solution
| -rw-r--r-- | challenge-266/pokgopun/go/ch-1.go | 88 | ||||
| -rw-r--r-- | challenge-266/pokgopun/go/ch-2.go | 143 | ||||
| -rw-r--r-- | challenge-266/pokgopun/python/ch-1.py | 57 | ||||
| -rw-r--r-- | challenge-266/pokgopun/python/ch-2.py | 103 |
4 files changed, 391 insertions, 0 deletions
diff --git a/challenge-266/pokgopun/go/ch-1.go b/challenge-266/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..b5d697fe59 --- /dev/null +++ b/challenge-266/pokgopun/go/ch-1.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +/*# + +Task 1: Uncommon Words + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two sentences, $line1 and $line2. + + Write a script to find all uncommmon words in any order in the given + two sentences. Return ('') if none found. + + A word is uncommon if it appears exactly once in one of the + sentences and doesn’t appear in other sentence. + +Example 1 + +Input: $line1 = 'Mango is sweet' + $line2 = 'Mango is sour' +Output: ('sweet', 'sour') + +Example 2 + +Input: $line1 = 'Mango Mango' + $line2 = 'Orange' +Output: ('Orange') + +Example 3 + +Input: $line1 = 'Mango is Mango' + $line2 = 'Orange is Orange' +Output: ('') + +Task 2: X Matrix +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func uncommonWords(line1, line2 string) []string { + wc := make(map[string]int) + var words []string + for _, v := range strings.Split(line1+" "+line2, " ") { + wc[v]++ + if wc[v] == 1 { + words = append(words, v) + } + } + //fmt.Println("words =>", words, "wc =>", wc) + l := len(words) + i := l + for i > 0 { + if wc[words[i-1]] > 1 { + copy(words[i-1:], words[i:]) + l-- + } + i-- + } + if l == 0 { + return []string{""} + } + return words[:l] +} + +func main() { + for _, data := range []struct { + line1, line2 string + output []string + }{ + {"Mango is sweet", "Mango is sour", []string{"sweet", "sour"}}, + {"Mango Mango", "Orange", []string{"Orange"}}, + {"Mango is Mango", "Orange is Orange", []string{""}}, + {"Mango", "Mango", []string{""}}, + {"Mango", "Orange Orange", []string{"Mango"}}, + } { + //fmt.Println("=>", data, uncommonWords(data.line1, data.line2)) + io.WriteString(os.Stdout, cmp.Diff(uncommonWords(data.line1, data.line2), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-266/pokgopun/go/ch-2.go b/challenge-266/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..cee52da9ef --- /dev/null +++ b/challenge-266/pokgopun/go/ch-2.go @@ -0,0 +1,143 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +/*# + +Task 2: X Matrix + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a square matrix, $matrix. + + Write a script to find if the given matrix is X Matrix. + + A square matrix is an X Matrix if all the elements on the main + diagonal and antidiagonal are non-zero and everything else are zero. + +Example 1 + +Input: $matrix = [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], + ] +Output: true + +Example 2 + +Input: $matrix = [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ] +Output: false + +Example 3 + +Input: $matrix = [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], + ] +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 28th April + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "errors" + "io" + "log" + "os" + + "github.com/google/go-cmp/cmp" +) + +type row []int + +type matrix []row + +func (mtx matrix) isSquare() bool { + l := len(mtx) + i := l + for i > 0 { + i-- + if len(mtx[i]) != l { + return false + } + } + return true +} + +type squareMatrix struct { + l int + m matrix +} + +func newSquareMatrix(mtx matrix) (squareMatrix, error) { + if mtx.isSquare() { + return squareMatrix{len(mtx), mtx}, nil + } + return squareMatrix{}, errors.New("not square") +} + +func (sm squareMatrix) onX(r, c int) bool { + if r == c || r == sm.l-c-1 || c == sm.l-r-1 { + return true + } + return false +} + +func (sm squareMatrix) isX() bool { + var v int + for r := 0; r < sm.l; r++ { + for c := 0; c < sm.l; c++ { + v = sm.m[r][c] + if sm.onX(r, c) { + if v == 0 { + return false + } + } else { + if v != 0 { + return false + } + } + } + } + return true +} + +func main() { + for _, data := range []struct { + input matrix + output bool + }{ + {matrix{ + row{1, 0, 0, 2}, + row{0, 3, 4, 0}, + row{0, 5, 6, 0}, + row{7, 0, 0, 1}, + }, true}, + {matrix{ + row{1, 2, 3}, + row{4, 5, 6}, + row{7, 8, 9}, + }, false}, + {matrix{ + row{1, 0, 2}, + row{0, 3, 0}, + row{4, 0, 5}, + }, true}, + } { + sm, err := newSquareMatrix(data.input) + if err != nil { + log.Fatal(err) + } + io.WriteString(os.Stdout, cmp.Diff(sm.isX(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-266/pokgopun/python/ch-1.py b/challenge-266/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..96addf3d91 --- /dev/null +++ b/challenge-266/pokgopun/python/ch-1.py @@ -0,0 +1,57 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +""" + +Task 1: Uncommon Words + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two sentences, $line1 and $line2. + + Write a script to find all uncommmon words in any order in the given + two sentences. Return ('') if none found. + + A word is uncommon if it appears exactly once in one of the + sentences and doesn’t appear in other sentence. + +Example 1 + +Input: $line1 = 'Mango is sweet' + $line2 = 'Mango is sour' +Output: ('sweet', 'sour') + +Example 2 + +Input: $line1 = 'Mango Mango' + $line2 = 'Orange' +Output: ('Orange') + +Example 3 + +Input: $line1 = 'Mango is Mango' + $line2 = 'Orange is Orange' +Output: ('') + +Task 2: X Matrix +""" +### solution by pokgopun@gmail.com + +def uncommonWords(line1, line2): + words = f'{line1} {line2}'.split() + return tuple( + w for w in words if words.count(w)==1 + ) or ('',) + + +import unittest + +class TestUncommonWords(unittest.TestCase): + def test(self): + for (line1,line2), ans in { + ('Mango is sweet','Mango is sour'): ('sweet', 'sour'), + ('Mango Mango','Orange'): ('Orange',), + ('Mango is Mango','Orange is Orange'): ('',), + }.items(): + self.assertEqual(uncommonWords(line1,line2),ans) + +unittest.main() diff --git a/challenge-266/pokgopun/python/ch-2.py b/challenge-266/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..f12584ae55 --- /dev/null +++ b/challenge-266/pokgopun/python/ch-2.py @@ -0,0 +1,103 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +""" + +Task 2: X Matrix + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a square matrix, $matrix. + + Write a script to find if the given matrix is X Matrix. + + A square matrix is an X Matrix if all the elements on the main + diagonal and antidiagonal are non-zero and everything else are zero. + +Example 1 + +Input: $matrix = [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], + ] +Output: true + +Example 2 + +Input: $matrix = [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ] +Output: false + +Example 3 + +Input: $matrix = [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], + ] +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 28th April + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def hasAllZero(arr): + return + +def mtxIsValid(mtx: list): + l = len(mtx) + if l < 3: + return None + h = l // 2 + col = 0 + lst = list() + for row in range(h): + for r in (row,-row-1): + lst = [e for e in mtx[r]] + if len(lst) != l: + return None + for c in (col,-col-1): + if lst[c] == 0: + return False + lst[c] = 0 + for e in lst: + if e != 0: + return False + col += 1 + if h % 2: + lst = [e for e in mtx[h]] + if len(lst) != l: + return None + if mtx[h][h] == 0: + return False + lst[h] = 0 + for e in lst: + if e != 0: + return False + return True + +import unittest + +class TestMtxIsValid(unittest.TestCase): + def test(self): + for ans, mtx in { + True: [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1],], + False: [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9],], + True: [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5],], + }.items(): + self.assertEqual(ans,mtxIsValid(mtx)) + +unittest.main() |
