Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Uninitialized warnings trouble

by Anonymous Monk
on Dec 14, 2022 at 04:47 UTC ( [id://11148851]=note: print w/replies, xml ) Need Help??


in reply to Uninitialized warnings trouble

I changed my code pretty radically using " List::Util". I now get the right answer :)

#!/usr/bin/perl use strict; use warnings; use List::Util qw(all); #use Time::HiRes qw(usleep); sub usleep {}; my @forest; my $current = 0; my $visible; my $found = 0; open(my $lines, '<', 'input8.txt'); while (<$lines>) { chomp; push @forest, [ split // ]; } close $lines; foreach my $current (0..@forest-1) { my $row = $forest[$current]; foreach my $i (0..@$row-1) { if ($current == 0 or $current == @forest-1 or $i == 0 or $i == + @$row-1) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } if (all { $_ < $row->[$i] } @$row[0..$i-1] or all { $_ < $row- +>[$i] } @$row[$i+1..@$row-1]) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } if (all { $_ < $row->[$i] } map { $forest[$_][$i] } (0..$curre +nt-1)) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } if (all { $_ < $row->[$i] } map { $forest[$_][$i] } ($current+ +1..@forest-1)) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } syswrite STDOUT, $forest[$current][$i] . ' '; usleep 100000; } syswrite STDOUT, "\n"; } print "There are $visible visible trees.\n";

Replies are listed 'Best First'.
Re^2: Uninitialized warnings trouble
by hv (Prior) on Dec 14, 2022 at 06:08 UTC

    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.

Re^2: Uninitialized warnings trouble
by tybalt89 (Monsignor) on Dec 14, 2022 at 05:46 UTC

    Fun little problem. Here's another entry in my "The only data structure needed is a multi-line string" collection.

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11148831 use warnings; sub quarterturn { my $new = ''; $new .= "\n" while s/.$/ $new .= $&; '' /gem; return $new; } my $forest = do { local (@ARGV, $/) = 'input8.txt'; <> }; my $visible = ''; for ( 0 .. 3 ) { $visible |= $forest =~ s{(.)(?=(.*))}{ $2 =~ /[$1-9]/ ^ 1 }ger; $_ = quarterturn for $forest, $visible; } my $totalvisible = $visible =~ tr/1//; # count ones print "$forest\n$visible\ntotal visible: $totalvisible\n";
      Nice code of yours!

      I will share an alternative to your regex, which is longer and uses (?{ ... }), but avoids repeated lookahead:
      for ( 0 .. 3 ) { my $max; $visible |= $forest =~ s { ^ (?{ $max = -1; }) (*FAIL) | (\d) } { length( $1 > $max and $max = $1 ) }germx; $_ = quarterturn for $forest, $visible; }
      $new .= "\n" while s/.$/ $new .= $&; '' /gem;

      Did you test this?

      $ time perl -e' use Data::Dumper; $Data::Dumper::Useqq = 1; my $x = "123456789\n"; print Dumper $x; my $new = ""; $new .= "\n" while $x =~ s/.$/ $new .= $&; '' /gem; print Dumper $new; ' $VAR1 = "123456789\n"; Out of memory! real 1m54.230s user 0m3.216s sys 0m9.332s

      BTW, my system has 32GB of RAM     ouch!

      Sorry, my mistake was leaving in the single quotes, oops.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found