aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-28 10:54:04 +0100
committerGitHub <noreply@github.com>2024-04-28 10:54:04 +0100
commit1bbbb55bfe0394ac353add94a10044721b60543e (patch)
treeac71667477f72d29c680d1393ed984566d91aa21
parent7d79ac0092214977f3d3d17b065411c2f5fa08ea (diff)
parent7e744e23e4a78fa99a371eb01363b265371d0ef2 (diff)
downloadperlweeklychallenge-club-1bbbb55bfe0394ac353add94a10044721b60543e.tar.gz
perlweeklychallenge-club-1bbbb55bfe0394ac353add94a10044721b60543e.tar.bz2
perlweeklychallenge-club-1bbbb55bfe0394ac353add94a10044721b60543e.zip
Merge pull request #9998 from simongreen-net/master
sgreen solutions to challenge 266
-rw-r--r--challenge-266/sgreen/README.md4
-rw-r--r--challenge-266/sgreen/blog.txt1
-rwxr-xr-xchallenge-266/sgreen/perl/ch-1.pl29
-rwxr-xr-xchallenge-266/sgreen/perl/ch-2.pl42
-rwxr-xr-xchallenge-266/sgreen/python/ch-1.py38
-rwxr-xr-xchallenge-266/sgreen/python/ch-2.py45
-rwxr-xr-xchallenge-266/sgreen/python/test.py43
7 files changed, 200 insertions, 2 deletions
diff --git a/challenge-266/sgreen/README.md b/challenge-266/sgreen/README.md
index 04194d6d36..0a7e07635b 100644
--- a/challenge-266/sgreen/README.md
+++ b/challenge-266/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 265
+# The Weekly Challenge 266
-Blog: [Completing Appearance](https://dev.to/simongreennet/completing-appearance-51p0)
+Blog: [Uncommon matrix](https://dev.to/simongreennet/uncommon-matrix-4mfh)
diff --git a/challenge-266/sgreen/blog.txt b/challenge-266/sgreen/blog.txt
new file mode 100644
index 0000000000..d4e0adb56a
--- /dev/null
+++ b/challenge-266/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/uncommon-matrix-4mfh \ No newline at end of file
diff --git a/challenge-266/sgreen/perl/ch-1.pl b/challenge-266/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..b4a97be57c
--- /dev/null
+++ b/challenge-266/sgreen/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@strings) {
+ my %freq = ();
+
+ # Compute the frequency of each word
+ foreach my $string (@strings) {
+ foreach my $word ( split / /, $string ) {
+ ++$freq{$word};
+ }
+ }
+
+ # Return the word(s) that appear only once.
+ my @match = grep { $freq{$_} == 1 } keys %freq;
+ if ( scalar(@match) == 0 ) {
+ # If there is no result, show an empty string
+ say "('')";
+ }
+ else {
+ say "('", join( "', '", @match ), "')";
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-266/sgreen/perl/ch-2.pl b/challenge-266/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..9049eac39c
--- /dev/null
+++ b/challenge-266/sgreen/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use JSON 'decode_json';
+
+sub main ($matrix) {
+ my $rows = scalar(@$matrix);
+
+ # Check we have a square
+ foreach my $row ( 0 .. $#$matrix ) {
+ if ( scalar( @{ $matrix->[$row] } ) != $rows ) {
+ die "Please specify a square matrix\n";
+ }
+ }
+
+ # Check that all the values are correct
+ my $is_x_matrix = 1;
+ MATRIX: foreach my $row ( 0 .. $#$matrix ) {
+ foreach my $col ( 0 .. $#$matrix ) {
+ if ( $col == $row or $col == $rows - 1 - $row ) {
+ # We are expecting a non-zero value
+ if ( $matrix->[$row][$col] == 0 ) {
+ $is_x_matrix = 0;
+ last MATRIX;
+ }
+ }
+ # We are expecting a zero value
+ elsif ( $matrix->[$row][$col] != 0 ) {
+ $is_x_matrix = 0;
+ last MATRIX;
+ }
+ }
+ }
+
+ say $is_x_matrix ? 'true' : 'false';
+}
+
+main( decode_json( $ARGV[0] ) ); \ No newline at end of file
diff --git a/challenge-266/sgreen/python/ch-1.py b/challenge-266/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..bc0372dc4c
--- /dev/null
+++ b/challenge-266/sgreen/python/ch-1.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+from collections import defaultdict
+import sys
+
+
+def uncommon_words(*strings) -> list:
+ """Find words that only occur once in one string, and don't appear in others
+
+ Params:
+ strings: One or more strings
+
+ Returns:
+ list: Words that meat the criteria
+ """
+ freq = defaultdict(int)
+
+ # Compute the frequency of each word
+ for string in strings:
+ for word in string.split(' '):
+ freq[word] += 1
+
+ # Return the word(s) that appear only once
+ return [w for w in freq if freq[w] == 1]
+
+
+def main():
+ result = uncommon_words(*sys.argv[1:])
+
+ if len(result) == 0:
+ # If there is no result, show an empty string
+ print("('')")
+ else:
+ print("('" + "', '".join(result) + "')")
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-266/sgreen/python/ch-2.py b/challenge-266/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d0df7257ee
--- /dev/null
+++ b/challenge-266/sgreen/python/ch-2.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def x_matrix(matrix: list) -> bool:
+ """Determine if the matrix is a X Matrix
+
+ Args:
+ matrix (list): The supplied matrix
+
+ Returns:
+ bool: Wether the matrix as a X Matrix or not
+ """
+ rows = len(matrix)
+
+ # Check we have a square
+ for row in range(rows):
+ if len(matrix[row]) != rows:
+ raise ValueError("Please specify a square matrix")
+
+ # Check that all the values are correct
+ for row in range(rows):
+ for col in range(rows):
+ if col == row or col == rows - 1 - row:
+ # We are expecting a non-zero value
+ if matrix[row][col] == 0:
+ return False
+ elif matrix[row][col] != 0:
+ # We are expecting a zero value
+ return False
+
+ return True
+
+
+def main():
+ # Parse the matrix from the input
+ matrix = json.loads(sys.argv[1])
+ result = x_matrix(matrix)
+ print('true' if result else 'false')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-266/sgreen/python/test.py b/challenge-266/sgreen/python/test.py
new file mode 100755
index 0000000000..22886bc8d7
--- /dev/null
+++ b/challenge-266/sgreen/python/test.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(
+ ch_1.uncommon_words('Mango is sweet', 'Mango is sour'),
+ ['sweet', 'sour']
+ )
+ self.assertEqual(
+ ch_1.uncommon_words('Mango Mango', 'Orange'),
+ ['Orange']
+ )
+ self.assertEqual(
+ ch_1.uncommon_words('Mango is Mango', 'Orange is Orange'),
+ []
+ )
+
+ def test_ch_2(self):
+ self.assertTrue(ch_2.x_matrix([
+ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ]))
+ self.assertFalse(ch_2.x_matrix([
+ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]))
+ self.assertTrue(ch_2.x_matrix([
+ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]))
+
+
+if __name__ == '__main__':
+ unittest.main()