diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-05-06 21:48:45 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-05-06 21:48:45 +0100 |
| commit | 452818bb316b536bd0e8ff494885362bdcfe33b1 (patch) | |
| tree | a1606037a99c1bc558668c5ef0ec105b3fa5762e /challenge-111 | |
| parent | aeab49e5d77b0262cc7bc86186ed37b12ff939a1 (diff) | |
| download | perlweeklychallenge-club-452818bb316b536bd0e8ff494885362bdcfe33b1.tar.gz perlweeklychallenge-club-452818bb316b536bd0e8ff494885362bdcfe33b1.tar.bz2 perlweeklychallenge-club-452818bb316b536bd0e8ff494885362bdcfe33b1.zip | |
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-111')
| -rw-r--r-- | challenge-111/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-111/laurent-rosenfeld/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-111/laurent-rosenfeld/perl/ch-2.pl | 21 | ||||
| -rw-r--r-- | challenge-111/laurent-rosenfeld/raku/ch-1.raku | 28 | ||||
| -rw-r--r-- | challenge-111/laurent-rosenfeld/raku/ch-2.raku | 16 |
5 files changed, 99 insertions, 0 deletions
diff --git a/challenge-111/laurent-rosenfeld/blog.txt b/challenge-111/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..9bd6e14f6a --- /dev/null +++ b/challenge-111/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2021/05/perl-weekly-challenge-111-search-matrix-and-ordered-letters.html diff --git a/challenge-111/laurent-rosenfeld/perl/ch-1.pl b/challenge-111/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..4259c2283e --- /dev/null +++ b/challenge-111/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,33 @@ +use strict; +use warnings; +use feature "say"; + +my @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 ] + ); + +sub A2AoA { + my $index = shift; + my ($i, $j) = (int $index / 5, $index % 5); +} +sub bin_search { + my $in = shift; + my ($min, $max) = (0, 24); + while ($max > $min) { + my $pivot = int (($max + $min) /2); + my ($i, $j) = A2AoA $pivot; + my $val = $matrix[$i][$j]; + # say "val = $val, $i, $j"; + return 1 if $val == $in; + if ($in > $val) { + $min = $pivot + 1; + } else { + $max = $pivot; + } + } + return 0; +} +say "$_ => ", bin_search $_ for 0..54; diff --git a/challenge-111/laurent-rosenfeld/perl/ch-2.pl b/challenge-111/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..f17b280103 --- /dev/null +++ b/challenge-111/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature "say"; + +my @long_words; +my $max_length = 0; + +my $word_list = "./words.txt"; +open my $IN, "<", $word_list or die "Cannot open $word_list $!"; +while (my $word = <$IN>) { + chomp $word; + next unless $word eq join '', sort split //, $word; + my $length = length $word; + if ($length > $max_length) { + @long_words = ($word); + $max_length = $length; + } elsif ($length == $max_length) { + push @long_words, $word; + } +} +say "@long_words"; diff --git a/challenge-111/laurent-rosenfeld/raku/ch-1.raku b/challenge-111/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..16cfbeffad --- /dev/null +++ b/challenge-111/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,28 @@ +use v6; + +my @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 ); + +sub A2AoA ($index) { + my ($i, $j) = $index.polymod(5).reverse; +} +sub binary ($in) { + my ($min, $max) = 0, 24; + while $max > $min { + my $pivot = (($max + $min) /2).Int; + my ($i, $j) = A2AoA $pivot; + my $val = @matrix[$i][$j]; + # say "val = $val, $i, $j"; + return 1 if $val == $in; + if $in > $val { + $min = $pivot + 1; + } else { + $max = $pivot; + } + } + return 0; +} +say "$_ => ", binary $_ for 0..54; diff --git a/challenge-111/laurent-rosenfeld/raku/ch-2.raku b/challenge-111/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..3f5f6537cb --- /dev/null +++ b/challenge-111/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,16 @@ +use v6; + +my @long-words; +my $max-length = 0; + +for './words.txt'.IO.lines -> $word { + next unless [le] $word.comb; + my $length = $word.chars; + if $length > $max-length { + @long-words = $word,; + $max-length = $length; + } elsif $length == $max-length { + push @long-words, $word; + } +} +say @long-words.join(", "); |
