diff options
| -rw-r--r-- | challenge-111/paulo-custodio/perl/ch-1.pl | 87 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/t/test-1.yaml | 45 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/t/test-2.yaml | 6 | ||||
| -rwxr-xr-x | challenge-111/paulo-custodio/test.pl | 7 |
5 files changed, 172 insertions, 0 deletions
diff --git a/challenge-111/paulo-custodio/perl/ch-1.pl b/challenge-111/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..393c57bb1e --- /dev/null +++ b/challenge-111/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env perl + +# Challenge 111 +# +# TASK #1 - Search Matrix +# Submitted by: Mohammad S Anwar +# 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. +# +# Example +# Matrix: [ 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 ] +# +# Input: 35 +# Output: 0 since it is missing in the matrix +# +# Input: 39 +# Output: 1 as it exists in the matrix + +use Modern::Perl; + +my @data = ([ 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 $N = shift || 0; +say find($N) ? 1 : 0; + +sub find { + my($n) = @_; + my $row = find_row($n); + return 0 if $row < 0; + my $col = find_col($n, $row); + return 0 if $col < 0; + return 1; +} + +sub find_row { + my($n) = @_; + my $l = 0; + my $h = $#data; + return -1 if $n < $data[$l][0]; + return -1 if $n > $data[$h][-1]; + while ($l < $h) { + my $m = int(1+($l+$h)/2); + if ($n < $data[$m][0]) { + $h = $m-1; + } + elsif ($n > $data[$m][-1]) { + $l = $m+1; + } + else { + return $m; + } + } + return $l; +} + +sub find_col { + my($n, $row) = @_; + my $l = 0; + my $h = $#{$data[$row]}; + return -1 if $n < $data[$row][0]; + return -1 if $n > $data[$row][-1]; + while ($l < $h) { + my $m = int(1+($l+$h)/2); + if ($n < $data[$row][$m]) { + $h = $m-1; + } + elsif ($n > $data[$row][$m]) { + $l = $m+1; + } + else { + return $m; + } + } + return -1 if $n != $data[$row][$l]; + return $l; +} diff --git a/challenge-111/paulo-custodio/perl/ch-2.pl b/challenge-111/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d70c838907 --- /dev/null +++ b/challenge-111/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +# Challenge 111 +# +# TASK #2 - Ordered Letters +# Submitted by: E. Choroba +# Given a word, you can sort its letters alphabetically (case insensitive). +# For example, “beekeeper” becomes “beeeeekpr” and “dictionary” becomes +# “acdiinorty”. +# +# Write a script to find the longest English words that don’t change when +# their letters are sorted. + +use Modern::Perl; + +my @words; +my $max_len = 0; +while (<>) { + chomp; + my $word = lc($_); + my $drow = join '', (sort split //, $word); + if ($word eq $drow) { + $max_len = length($word) if $max_len < length($word); + push @words, $word; + } +} +say join "\n", grep {length($_)==$max_len} @words; diff --git a/challenge-111/paulo-custodio/t/test-1.yaml b/challenge-111/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..40700d7f1a --- /dev/null +++ b/challenge-111/paulo-custodio/t/test-1.yaml @@ -0,0 +1,45 @@ +- setup: + cleanup: + args: 0 + input: + output: 0 +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 4 + input: + output: 0 +- setup: + cleanup: + args: 7 + input: + output: 1 +- setup: + cleanup: + args: 8 + input: + output: 0 +- setup: + cleanup: + args: 21 + input: + output: 0 +- setup: + cleanup: + args: 23 + input: + output: 1 +- setup: + cleanup: + args: 25 + input: + output: 1 +- setup: + cleanup: + args: 51 + input: + output: 0 diff --git a/challenge-111/paulo-custodio/t/test-2.yaml b/challenge-111/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..3c5bbecdbf --- /dev/null +++ b/challenge-111/paulo-custodio/t/test-2.yaml @@ -0,0 +1,6 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: unlink("words.txt") + args: words.txt + input: + output: | + billowy diff --git a/challenge-111/paulo-custodio/test.pl b/challenge-111/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-111/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; |
