From ba89e70ef2a481c5c50b0efa712f94c999ca31ba Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Wed, 5 May 2021 23:31:48 -0400 Subject: initial commit --- challenge-111/adam-russell/blog.txt | 0 challenge-111/adam-russell/blog1.txt | 0 challenge-111/adam-russell/perl/ch-1.pl | 67 ++++++++++++++++++++++++++++++++ challenge-111/adam-russell/perl/ch-2.pl | 29 ++++++++++++++ challenge-111/adam-russell/prolog/ch-1.p | 0 challenge-111/adam-russell/prolog/ch-2.p | 0 6 files changed, 96 insertions(+) create mode 100644 challenge-111/adam-russell/blog.txt create mode 100644 challenge-111/adam-russell/blog1.txt create mode 100644 challenge-111/adam-russell/perl/ch-1.pl create mode 100644 challenge-111/adam-russell/perl/ch-2.pl create mode 100644 challenge-111/adam-russell/prolog/ch-1.p create mode 100644 challenge-111/adam-russell/prolog/ch-2.p (limited to 'challenge-111') diff --git a/challenge-111/adam-russell/blog.txt b/challenge-111/adam-russell/blog.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-111/adam-russell/blog1.txt b/challenge-111/adam-russell/blog1.txt new file mode 100644 index 0000000000..e69de29bb2 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..1f2e49e27b --- /dev/null +++ b/challenge-111/adam-russell/perl/ch-1.pl @@ -0,0 +1,67 @@ +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; + +use Data::Dump q/pp/; + +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"; +} diff --git a/challenge-111/adam-russell/prolog/ch-1.p b/challenge-111/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-111/adam-russell/prolog/ch-2.p b/challenge-111/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..e69de29bb2 -- cgit From 72b10866a570b146b6194ffb7d092a67c764cb34 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 9 May 2021 01:38:09 -0400 Subject: no time for Prolog this week --- challenge-111/adam-russell/blog1.txt | 0 challenge-111/adam-russell/prolog/ch-1.p | 0 challenge-111/adam-russell/prolog/ch-2.p | 0 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 challenge-111/adam-russell/blog1.txt delete mode 100644 challenge-111/adam-russell/prolog/ch-1.p delete mode 100644 challenge-111/adam-russell/prolog/ch-2.p (limited to 'challenge-111') diff --git a/challenge-111/adam-russell/blog1.txt b/challenge-111/adam-russell/blog1.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/challenge-111/adam-russell/prolog/ch-1.p b/challenge-111/adam-russell/prolog/ch-1.p deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/challenge-111/adam-russell/prolog/ch-2.p b/challenge-111/adam-russell/prolog/ch-2.p deleted file mode 100644 index e69de29bb2..0000000000 -- cgit From a599750c59eca7a89b33f57d2f75eb1c743cc1de Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 9 May 2021 09:36:54 -0400 Subject: blog link --- challenge-111/adam-russell/blog.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'challenge-111') diff --git a/challenge-111/adam-russell/blog.txt b/challenge-111/adam-russell/blog.txt index e69de29bb2..356d4e2844 100644 --- a/challenge-111/adam-russell/blog.txt +++ b/challenge-111/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/05/09 -- cgit From 615a4698dc8356fc36eff71746564ee5da8cb6ec Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 9 May 2021 09:42:49 -0400 Subject: removed debugging code --- challenge-111/adam-russell/perl/ch-1.pl | 2 -- 1 file changed, 2 deletions(-) (limited to 'challenge-111') diff --git a/challenge-111/adam-russell/perl/ch-1.pl b/challenge-111/adam-russell/perl/ch-1.pl index 1f2e49e27b..4b2153fb72 100644 --- a/challenge-111/adam-russell/perl/ch-1.pl +++ b/challenge-111/adam-russell/perl/ch-1.pl @@ -10,8 +10,6 @@ use warnings; use boolean; use constant MATRIX_SIZE => 5; -use Data::Dump q/pp/; - sub matrix_search{ my($matrix, $search) = @_; unless(@{$matrix} == 1){ -- cgit