Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)

by CountZero (Bishop)
on Sep 24, 2011 at 20:10 UTC ( #927679=note: print w/ replies, xml ) Need Help??


in reply to An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)

This seems to work as well:

use Modern::Perl; my @array = ( [ qw/ 1 1 1 1 1 / ], [ qw/ 0 1 1 0 0 / ], [ qw/ 0 0 0 0 1 / ], [ qw/ 0 0 0 0 0 / ], [ qw/ 0 1 0 1 0 / ], ); my $rownumber = 0; for my $row (@array) { do {say "row $rownumber has all zeroes"; last} unless (join '', @$ +row)+0; $rownumber++ }

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


Comment on Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
Download Code
Re^2: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
by davido (Archbishop) on Sep 24, 2011 at 20:18 UTC

    I like that because it only does one comparison per row.

    I considered a join approach too, but had two concerns. First, while it works in the specific case, if the NxN matrix held more complex strings, it could fail, so I saw it as not general enough to a broader application of the problem. Nevertheless, the problem itself is specific, and your version works great.

    The other concern is that it lacks the optimization of failing as soon as the first non-zero entry is found on a given row. By joining the row, we're already implicitly touching the entire row. On the other hand, it does have the merit of only doing one comparison per row, which is nice. The shortcoming is that it has to join the entire row even if the first element of the row would result in a rejected row.

    Fun!


    Dave

Re^2: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
by moritz (Cardinal) on Sep 24, 2011 at 20:24 UTC

    That's slightly dangerous, because it doesn't work with all possible number formats. An entry of 0e0 (which is a valid zero) causes the comparison to disregard all non-zeros after it, as does two 0.0s (though it warns).

    Of course if one knows that the entries are always exactly 0 or 1, it's quite an elegant solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://927679]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2014-08-30 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls