aboutsummaryrefslogtreecommitdiff
path: root/challenge-111
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-09 19:21:30 +0100
committerGitHub <noreply@github.com>2021-05-09 19:21:30 +0100
commit327a8134c4fb5cebbc3f6eb45dc2a140a4136848 (patch)
treead611858785e299d81b594c03422c24e5545c202 /challenge-111
parent35d7f03c3edda628f573fd60e0d62e5c8eddba55 (diff)
parent615a4698dc8356fc36eff71746564ee5da8cb6ec (diff)
downloadperlweeklychallenge-club-327a8134c4fb5cebbc3f6eb45dc2a140a4136848.tar.gz
perlweeklychallenge-club-327a8134c4fb5cebbc3f6eb45dc2a140a4136848.tar.bz2
perlweeklychallenge-club-327a8134c4fb5cebbc3f6eb45dc2a140a4136848.zip
Merge pull request #4035 from adamcrussell/challenge-111
Challenge 111
Diffstat (limited to 'challenge-111')
-rw-r--r--challenge-111/adam-russell/blog.txt1
-rw-r--r--challenge-111/adam-russell/perl/ch-1.pl65
-rw-r--r--challenge-111/adam-russell/perl/ch-2.pl29
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-111/adam-russell/blog.txt b/challenge-111/adam-russell/blog.txt
new file mode 100644
index 0000000000..356d4e2844
--- /dev/null
+++ b/challenge-111/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/05/09
diff --git a/challenge-111/adam-russell/perl/ch-1.pl b/challenge-111/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..4b2153fb72
--- /dev/null
+++ b/challenge-111/adam-russell/perl/ch-1.pl
@@ -0,0 +1,65 @@
+use strict;
+use warnings;
+##
+# You are given 5x5 matrix filled with integers such that each row
+# is sorted from left to right and the first integer of each row is
+# greater than the last integer of the previous row.
+# Write a script to find a given integer in the matrix using an efficient
+# search algorithm.
+##
+use boolean;
+use constant MATRIX_SIZE => 5;
+
+sub matrix_search{
+ my($matrix, $search) = @_;
+ unless(@{$matrix} == 1){
+ my $half = int(@{$matrix} / 2);
+ if($matrix->[$half]->[0] > $search){
+ my @matrix_reduced = @{$matrix}[0 .. $half - 1];
+ matrix_search(\@matrix_reduced, $search);
+ }
+ elsif($matrix->[$half]->[0] < $search){
+ my @matrix_reduced = @{$matrix}[$half .. @{$matrix} - 1];
+ matrix_search(\@matrix_reduced, $search);
+ }
+ elsif($matrix->[$half]->[0] == $search){
+ return true;
+ }
+ }
+ else{
+ return row_search($matrix->[0], $search);
+ }
+}
+
+sub row_search{
+ my ($row, $search) = @_;
+ unless(@{$row} == 1){
+ my $half = int(@{$row} / 2);
+ if($row->[$half] > $search){
+ my @row_reduced = @{$row}[0 .. $half - 1];
+ row_search(\@row_reduced, $search);
+ }
+ elsif($row->[$half] < $search){
+ my @row_reduced = @{$row}[$half .. @{$row} - 1];
+ row_search(\@row_reduced, $search);
+ }
+ elsif($row->[$half] == $search){
+ return true;
+ }
+ }
+ else{
+ return false;
+ }
+}
+
+MAIN:{
+ my $N = [[ 1, 2, 3, 5, 7 ],
+ [ 9, 11, 15, 19, 20 ],
+ [ 23, 24, 25, 29, 31 ],
+ [ 32, 33, 39, 40, 42 ],
+ [ 45, 47, 48, 49, 50 ]];
+ my $search = 35;
+ print matrix_search($N, $search) . "\n";
+ $search = 39;
+ print matrix_search($N, $search) . "\n";
+}
diff --git a/challenge-111/adam-russell/perl/ch-2.pl b/challenge-111/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..7bfa36ba65
--- /dev/null
+++ b/challenge-111/adam-russell/perl/ch-2.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+##
+# Write a script to find the longest English words that don't change
+# when their letters are sorted.
+##
+sub max_sorted{
+ my($words) = @_;
+ my $max = -1;
+ my @length_words;
+ for my $word (@{$words}){
+ my $sorted_word = join("", sort { $a cmp $b } split(//, $word));
+ if($word eq $sorted_word && length($word) >= $max){
+ $length_words[length($word)] = [] if(!$length_words[length($word)]);
+ push @{$length_words[length($word)]}, $word;
+ $max = length($word);
+ }
+ }
+ return $length_words[$max];
+}
+
+MAIN:{
+ my @words;
+ while(<>){
+ chomp;
+ push @words, lc($_);
+ }
+ print join("\n", @{max_sorted(\@words)}) . "\n";
+}