aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-23 16:52:16 +0100
committerGitHub <noreply@github.com>2024-04-23 16:52:16 +0100
commitf6dae56cf2a65c12250b1d24698f5ba90ebc3e1d (patch)
tree018ea3f5d2e6bbc0755a4a76f6dffb685c8a1073
parent55c3cb816ae597d31d71eef42cf3451778b48439 (diff)
parent2f581890a1b604cc0cfb421176a1209379277bc3 (diff)
downloadperlweeklychallenge-club-f6dae56cf2a65c12250b1d24698f5ba90ebc3e1d.tar.gz
perlweeklychallenge-club-f6dae56cf2a65c12250b1d24698f5ba90ebc3e1d.tar.bz2
perlweeklychallenge-club-f6dae56cf2a65c12250b1d24698f5ba90ebc3e1d.zip
Merge pull request #9982 from LubosKolouch/master
Challenge 266 LK Perl Python
-rw-r--r--challenge-266/lubos-kolouch/perl/ch-1.pl28
-rw-r--r--challenge-266/lubos-kolouch/perl/ch-2.pl47
-rw-r--r--challenge-266/lubos-kolouch/python/ch-1.py38
-rw-r--r--challenge-266/lubos-kolouch/python/ch-2.py20
4 files changed, 133 insertions, 0 deletions
diff --git a/challenge-266/lubos-kolouch/perl/ch-1.pl b/challenge-266/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..7120850eb5
--- /dev/null
+++ b/challenge-266/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+
+sub uncommon_words {
+ my ($line1, $line2) = @_;
+
+ my %count1 = map { $_ => scalar(() = $line1 =~ /\b$_\b/g) } split ' ', $line1;
+ my %count2 = map { $_ => scalar(() = $line2 =~ /\b$_\b/g) } split ' ', $line2;
+
+ my @result;
+ foreach my $word (keys %count1) {
+ push @result, $word if $count1{$word} == 1 and not exists $count2{$word};
+ }
+ foreach my $word (keys %count2) {
+ push @result, $word if $count2{$word} == 1 and not exists $count1{$word};
+ }
+
+ return @result ? @result : ('');
+}
+
+# Test cases
+use Test::More tests => 3;
+is_deeply([uncommon_words('Mango is sweet', 'Mango is sour')], ['sweet', 'sour'], 'Example 1');
+is_deeply([uncommon_words('Mango Mango', 'Orange')], ['Orange'], 'Example 2');
+is_deeply([uncommon_words('Mango is Mango', 'Orange is Orange')], [''], 'Example 3');
+
+1;
diff --git a/challenge-266/lubos-kolouch/perl/ch-2.pl b/challenge-266/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..2222b70596
--- /dev/null
+++ b/challenge-266/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub is_x_matrix {
+ my $matrix = shift;
+ my $size = @$matrix;
+
+ for (my $i = 0; $i < $size; $i++) {
+ for (my $j = 0; $j < $size; $j++) {
+ if ($i == $j || $i + $j == $size - 1) {
+ return 0 unless $matrix->[$i]->[$j];
+ } else {
+ return 0 if $matrix->[$i]->[$j];
+ }
+ }
+ }
+ return 1;
+}
+
+# Test 1
+my $matrix1 = [
+ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+];
+
+print is_x_matrix($matrix1) ? "true\n" : "false\n"; # Output: true
+
+# Test 2
+my $matrix2 = [
+ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+];
+
+print is_x_matrix($matrix2) ? "true\n" : "false\n"; # Output: false
+
+# Test 3
+my $matrix3 = [
+ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+];
+
+print is_x_matrix($matrix3) ? "true\n" : "false\n"; # Output: true \ No newline at end of file
diff --git a/challenge-266/lubos-kolouch/python/ch-1.py b/challenge-266/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..e000c30a89
--- /dev/null
+++ b/challenge-266/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,38 @@
+from collections import Counter
+from typing import Tuple
+
+
+def uncommon_words(line1: str, line2: str) -> Tuple[str, ...]:
+ """
+ Find all uncommon words in any order in the given two sentences.
+
+ Args:
+ line1 (str): The first sentence.
+ line2 (str): The second sentence.
+
+ Returns:
+ Tuple[str, ...]: A tuple of uncommon words. If none found, returns an empty tuple.
+ """
+ words1 = line1.split()
+ words2 = line2.split()
+
+ counter1 = Counter(words1)
+ counter2 = Counter(words2)
+
+ uncommon = [
+ word for word in counter1 if counter1[word] == 1 and word not in counter2
+ ]
+ for word in counter2:
+ if counter2[word] == 1 and word not in counter1:
+ uncommon.append(word)
+
+ return tuple(uncommon)
+
+
+# Test cases
+print(uncommon_words("Mango is sweet", "Mango is sour"))
+# Output: ('sweet', 'sour')
+print(uncommon_words("Mango Mango", "Orange"))
+# Output: ('Orange')
+print(uncommon_words("Mango is Mango", "Orange is Orange"))
+# Output: ()
diff --git a/challenge-266/lubos-kolouch/python/ch-2.py b/challenge-266/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..b26020f837
--- /dev/null
+++ b/challenge-266/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,20 @@
+from typing import List
+
+
+def is_x_matrix(matrix: List[List[int]]) -> bool:
+ n = len(matrix)
+ return all(
+ (
+ (matrix[i][i] != 0 and matrix[i][n - 1 - i] != 0)
+ if i == j or j == n - 1 - i
+ else matrix[i][j] == 0
+ )
+ for i in range(n)
+ for j in range(n)
+ )
+
+
+# Test cases
+assert is_x_matrix([[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]]) is True
+assert is_x_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) is False
+assert is_x_matrix([[1, 0, 2], [0, 3, 0], [4, 0, 5]]) is True