diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-05-04 17:41:33 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-05-04 17:41:33 -0500 |
| commit | c59dd0f995e409ea342bec6cf33b77261d19e8fd (patch) | |
| tree | 8f52c710e83e7840a2875a7003efc5cee089a273 /challenge-111 | |
| parent | ed79a8c5ff1bf684fba727525b7281b3457c4903 (diff) | |
| download | perlweeklychallenge-club-c59dd0f995e409ea342bec6cf33b77261d19e8fd.tar.gz perlweeklychallenge-club-c59dd0f995e409ea342bec6cf33b77261d19e8fd.tar.bz2 perlweeklychallenge-club-c59dd0f995e409ea342bec6cf33b77261d19e8fd.zip | |
Remove some comments
Diffstat (limited to 'challenge-111')
| -rwxr-xr-x | challenge-111/wlmb/perl/ch-1.pl | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/challenge-111/wlmb/perl/ch-1.pl b/challenge-111/wlmb/perl/ch-1.pl index cd59e58134..fd4b2da227 100755 --- a/challenge-111/wlmb/perl/ch-1.pl +++ b/challenge-111/wlmb/perl/ch-1.pl @@ -1,31 +1,29 @@ #!/usr/bin/env perl # Perl weekly challenge 111 - # Task 1: Search matrix - # - # See https://wlmb.github.io/2021/05/03/PWC111/#task-1-search-matrix - use strict; - use warnings; - use v5.12; - use POSIX qw(floor); - # Input matrix from STDIN as space and newline separated columns and rows. - # Assume it is ordered as stated above -# my @rows=map {chomp; [split ' ']} <STDIN>; # Not ARGV, as it is used for the numbers -# my @elements=map {(@$_)} @rows; # flatten matrix - my @elements= map {chomp; (split ' ')} <STDIN>; - say "Input: $_ Output: ", binary_search($_, @elements) foreach @ARGV; +# Task 1: Search matrix +# +# See https://wlmb.github.io/2021/05/03/PWC111/#task-1-search-matrix +use strict; +use warnings; +use v5.12; +use POSIX qw(floor); +# Input matrix from STDIN (not ARGV) as space and newline separated columns and rows. +# Assume it is ordered as stated above +my @elements= map {chomp; (split ' ')} <STDIN>; # no need for organizing in rows +say "Input: $_ Output: ", binary_search($_, @elements) foreach @ARGV; - sub binary_search { # binary search an ordered list - my $number=shift @_; - my @array=@_; - return 0 if $number<$array[0] or $number > $array[-1]; # trivial case - my $low=0; - my $high=@array; #beyond highest number - while($low<$high){ - my $mid=floor(($low+$high)/2); - return 1 if $array[$mid]==$number; - return 0 if $mid==$low; - $low=$mid if $number > $array[$mid]; - $high=$mid if $number < $array[$mid]; - } - return 0; - } +sub binary_search { # binary search an ordered list + my $number=shift @_; + my @array=@_; + return 0 if $number<$array[0] or $number > $array[-1]; # trivial case + my $low=0; + my $high=@array; #beyond highest number + while($low<$high){ + my $mid=floor(($low+$high)/2); + return 1 if $array[$mid]==$number; + return 0 if $mid==$low; + $low=$mid if $number > $array[$mid]; + $high=$mid if $number < $array[$mid]; + } + return 0; +} |
