diff options
| -rw-r--r-- | challenge-266/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-266/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-266/jeanluc2020/perl/ch-1.pl | 67 | ||||
| -rwxr-xr-x | challenge-266/jeanluc2020/perl/ch-2.pl | 77 |
4 files changed, 146 insertions, 0 deletions
diff --git a/challenge-266/jeanluc2020/blog-1.txt b/challenge-266/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..46d66fb379 --- /dev/null +++ b/challenge-266/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-266-1.html diff --git a/challenge-266/jeanluc2020/blog-2.txt b/challenge-266/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..e7143c744b --- /dev/null +++ b/challenge-266/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-266-2.html diff --git a/challenge-266/jeanluc2020/perl/ch-1.pl b/challenge-266/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..d678fc333d --- /dev/null +++ b/challenge-266/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/#TASK1 +# +# Task 1: Uncommon Words +# ====================== +# +# 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: ('') +# +############################################################ +## +## discussion +## +############################################################ +# +# The task is to find word that only appear once across two sentence. +# So we split the sentences into the single words and count each one. +# We can avoid creating two loops by concatenating the two sentences +# first. + +use strict; +use warnings; + +uncommon_words('Mango is sweet', 'Mango is sour'); +uncommon_words('Mango Mango', 'Orange'); +uncommon_words('Mango is Mango', 'Orange is Orange'); + +sub uncommon_words { + my ($line1, $line2) = @_; + print "Input: '$line1', '$line2'\n"; + my $words = {}; + my @output = (); + foreach my $word (split /\s+/, "$line1 $line2") { + $words->{$word}++ + } + foreach my $word (keys %$words) { + push @output, $word if $words->{$word} == 1; + } + if(@output) { + print "Output: ('", join("', '", @output), "')\n"; + } else { + print "Output: ('')\n"; + } +} diff --git a/challenge-266/jeanluc2020/perl/ch-2.pl b/challenge-266/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..90ea753b6a --- /dev/null +++ b/challenge-266/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/#TASK2 +# +# Task 2: X Matrix +# ================ +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# So we just walk the matrix using an index $i for the rows and another +# index $j for the columns. If we're on the diagonals, we check whether +# the value is != 0, otherwise we check whether it is == 0. + +use strict; +use warnings; + +x_matrix( [ [1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1] ] ); +x_matrix( [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] ); +x_matrix( [ [1, 0, 2], [0, 3, 0], [4, 0, 5] ] ); + +sub x_matrix { + my $matrix = shift; + my $rows = scalar(@$matrix); + my $columns = scalar(@{$matrix->[0]}); + print "Input: [\n"; + foreach my $row (@$matrix) { + print " [ ", join(", ", @$row), "],\n"; + } + print "]\n"; + die "Not a square matrix" unless $rows == $columns; + my $is_x_matrix = 1; + foreach my $i (0..$rows-1) { + foreach my $j (0..$columns-1) { + if($i==$j or $i == ($columns - 1 - $j)) { + $is_x_matrix = 0 unless $matrix->[$i]->[$j] != 0; + } else { + $is_x_matrix = 0 unless $matrix->[$i]->[$j] == 0; + } + } + } + print "Output: ", $is_x_matrix == 0 ? "false" : "true", "\n"; +} |
