Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Excellent, well done. :)

I see you are already writing quite different code from what you started with. Next it might be worth thinking about whether you can rearrange things to avoid having to repeat this block over and over:

{ $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; }

Note also that you don't need syswrite STDOUT here, print is quite enough - you just need to set $| = 1; at the start to disable buffering.

For what it's worth, here is my version which uses almost the same approach:

#!/usr/bin/perl use strict; use warnings; use List::Util qw{ none any }; # parse the input (piped in, or from filename on command line) my @a = map { chomp; [ split // ] } <>; # determine the dimensions my $ni = @a; my $nj = @{ $a[0] }; # sanity check die "mismatched lengths" if any { @$_ != $nj } @a; # assume all visible to start with my $v = $ni * $nj; for my $i (0 .. $ni - 1) { for my $j (0 .. $nj - 1) { my $t = $a[$i][$j]; # target next if none { $_ >= $t } map $a[$i][$_], 0 .. $j - 1; # loo +k left next if none { $_ >= $t } map $a[$i][$_], $j + 1 .. $nj - 1; # loo +k right next if none { $_ >= $t } map $a[$_][$j], 0 .. $i - 1; # loo +k up next if none { $_ >= $t } map $a[$_][$j], $i + 1 .. $ni - 1; # loo +k down # if not visible in any direction, then invisible --$v; } } print "Visible $v of $ni x $nj\n";

If the speed of this algorithm ever becomes an issue, it also becomes interesting to consider a more dynamic approach: with a companion array of "visible/invisible" booleans, you can make just two passes over each row (once in each direction, or combined) and two over each column, then read the result out of the companion array. That reduces the algorithmic complexity from O(n^3) to O(n^2) for an n * n array.


In reply to Re^2: Uninitialized warnings trouble by hv
in thread Uninitialized warnings trouble by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found