aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--challenge-266/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-266/peter-campbell-smith/perl/ch-1.pl30
-rwxr-xr-xchallenge-266/peter-campbell-smith/perl/ch-2.pl67
7 files changed, 231 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
diff --git a/challenge-266/peter-campbell-smith/blog.txt b/challenge-266/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..fde98aee8c
--- /dev/null
+++ b/challenge-266/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/pwc/challenge/266
diff --git a/challenge-266/peter-campbell-smith/perl/ch-1.pl b/challenge-266/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..8a971df75d
--- /dev/null
+++ b/challenge-266/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-04-22
+use utf8; # Week 266 - task 1 - Uncommon words
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+uncommon_words('Mango is sweet', 'Mango is sour');
+uncommon_words('Roses are red', 'Violets are blue');
+uncommon_words('Red red roses are sweet', 'Roses are sweet');
+uncommon_words('London is the capital of the UK', 'The capital of the UK is London');
+uncommon_words('Alpha Bravo Charlie Delta Echo', 'Foxtrot Golf Hotel India Juliett');
+
+sub uncommon_words {
+
+ my (@words, %counts, $uncommon);
+
+ # concatenate the lines, lower-cased, and count word frequency
+ @words = split(/ +/, lc(qq[$_[0] $_[1]]));
+ $counts{$_} ++ for @words;
+
+ # find the words where frequency is 1
+ $uncommon .= $counts{$_} == 1 ? qq['$_', ] : '' for sort keys %counts;
+
+ # show the answer
+ say qq[\nInput: \$line1 = '$_[0]'\n \$line2 = '$_[1]'];
+ say qq[Output: (] . ($uncommon ? substr($uncommon, 0, -2) : q['']) . ')';
+}
diff --git a/challenge-266/peter-campbell-smith/perl/ch-2.pl b/challenge-266/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..c3dda93812
--- /dev/null
+++ b/challenge-266/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-04-22
+use utf8; # Week 266 - task 2 - X matrix
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+x_matrix([ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1] ]);
+
+x_matrix([ [0, 2, 0],
+ [4, 0, 6],
+ [0, 8, 0] ]);
+
+x_matrix([ [1, 0, 0, 0, 1],
+ [0, 2, 0, 2, 0],
+ [0, 0, 3, 0, 0],
+ [0, 4, 0, 4, 0],
+ [5, 0, 0, 0, 0] ]);
+
+x_matrix([ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5] ]);
+
+sub x_matrix {
+
+ my ($matrix, $dimension, $x, $y);
+
+ # initialise
+ $matrix = shift;
+ $dimension = scalar @$matrix - 1;
+ print_matrix(qq[Input: ], $matrix);
+
+ # loop over matrix elements
+ for $x (0 .. $dimension) {
+ for $y (0 .. $dimension) {
+
+ # conditions that must be met
+ next if ($x == $y or $x == $dimension - $y) ?
+ $matrix->[$x]->[$y] != 0 : $matrix->[$x]->[$y] == 0;
+
+ # ... but they are not
+ say qq[Output: false (∵ matrix[$x, $y] == $matrix->[$x]->[$y])];
+ return;
+ }
+ }
+
+ # all values are good
+ say qq[Output: true];
+}
+
+sub print_matrix {
+
+ my ($legend, $matrix, $j);
+
+ # format rows of matrix
+ ($legend, $matrix) = @_;
+ say '';
+ for $j (0 .. @$matrix - 1) {
+ say qq{$legend [} . join(', ', @{$matrix->[$j]}) . qq(]);
+ $legend = ' ' x length($legend);
+ }
+}