Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by ambrus (Abbot)
on Sep 25, 2011 at 07:47 UTC ( #927717=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)

I am quite partial to the while with stop variable solution. This can be coded easily in almost any language in a goto-less way, and C compilers these days should be able to optimize the stop variables away so it's as efficient as the solution with goto. It is also easy to derive by hand from the version using last.

Here's a perl implementation, but written in such a style that it's easy to translate to other languages. I show two versions. Either version of the function returns the index of the first all-zero row, or -1 if none of the rows are non-zero.

use warnings; use 5.014; use strict; sub first_allzero_row_a { my($a) = @_; my($r, $q, $c, $f, $o); $o = -1; $r = 0; $q = 0; while (!$q && $r < @$a) { $c = 0; $f = 0; while (!$f && $c < @{$$a[$r]}) { if (0 != ${$$a[$r]}[$c]) { $f = 1; } else { $c++; } } if (!$f) { $q = 1; $o = $r; } else { $r++; } } $o; } sub first_allzero_row_b { my($a) = @_; my($r, $q, $o); $o = -1; $r = 0; $q = 0; while (!$q && $r < @$a) { if (is_allzero($$a[$r])) { $q = 1; $o = $r; } else { $r++; } } $o; } sub is_allzero { my($a) = @_; my($c, $f); $c = 0; $f = 0; while (!$f && $c < @$a) { if (0 != $$a[$c]) { $f = 1; } else { $c++; } } !$f; }
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 / ], ); say first_allzero_row_a(\@array); say first_allzero_row_b(\@array);

A more difficult example for using this style can be found in the notebook info1-gy4 (fallback if previous link is broken), near the middle. This I wrote for an introduction to programming course (though here we're interested in the extra material we haven't really covered on the course). The programs are in baby-Python.

The problem solved is the following. (I encourage you all to try to write your own solutions for this one.) We would like to demonstrate a theorem of Gauss that says any natural number can be written as a sum of three triangular numbers. The triangular numbers are the numbers of form n*(n+1)/2 where n is an integer. For this, we want to print one such decomposition to three triangular numbers for each number up to 80. You must not print more than one solution per number, because that would degenerate to a wall of text, and it would also be hard to see whether any number is skipped.

The two relevant solutions, translated to perl for your convenience, can be found in Write any natural number as sum of three triangular numbers (second and third solutions). (The output of the two solutions differ, but they're equally valid.)

Update: moved triangular numbers example code from this reply to separate meditation.


Comment on Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
Select or 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 BrowserUk (Pope) on Sep 25, 2011 at 08:11 UTC

    I'm not arguing with your preference here -- personal preference, and the right to it, is a very strong instinct of mine -- but I simply do not understand how you can prefer this level of complexity to this level of simplicity?

    Can you offer any thoughts that might help me understand your preference?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-07-12 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls