Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How would you code this?

by Marshall (Canon)
on Apr 06, 2016 at 21:37 UTC ( [id://1159762]=note: print w/replies, xml ) Need Help??


in reply to How would you code this?

Here's my go at it. I did a compare file and it looks like this produces an identical output to your code.
#! perl -slw use strict; use List::Util qw[ reduce ]; use Data::Dump qw[ pp ]; printf "%s", scalar <DATA>; my @points = map[ split ' ', $_ ], <DATA>; my $i=0; while ($i < @points-1) { my ($x1,$y1) = @{$points[$i]}; my ($x2,$y2) = @{$points[$i+1]}; last unless defined $x2; if ( ($y2 - $y1 ) / ( $x2 - $x1 ) < 0 ) { splice @points,$i,2; #remove both points $i=-2; #backup an extra point to restart $i=0 if $i<0; } $i++; } print join ' ', @$_ for @points; __DATA__ X Y 0.0036759 0.4018006 0.0036962 0.4074616 0.0037064 0.4124646 0.0036962 0.4170003 0.0037064 0.4216399 0.0037166 0.4351084 0.0037064 0.4396787 0.0037166 0.4438854 0.0037268 0.4518142 0.0037166 0.4593275 0.0037268 0.4628417 0.0037370 0.4730210 0.0037268 0.4764141 0.0037370 0.4832176 0.0037268 0.4864203 0.0037370 0.4894152 0.0037471 0.4952320 0.0037675 0.4979326 0.0037879 0.5014988 0.0038082 0.5057747 0.0038184 0.5166984 0.0038286 0.5332830 0.0038184 0.5387016 0.0038082 0.5500408 0.0038184 0.5558402 0.0038286 0.5613627 0.0038388 0.6026338 0.0038591 0.6216075 0.0038490 0.6257104 0.0038693 0.6343489 0.0038490 0.6382094 0.0038693 0.6420872 0.0038490 0.6455669 0.0038693 0.6536515 0.0038591 0.6673797 0.0038693 0.6709805 0.0038795 0.6808655 0.0038897 0.6866130 0.0039202 0.6981425 0.0039406 0.7057251 0.0039610 0.7105550 0.0039508 0.7161121 0.0039610 0.7216518 0.0039712 0.7329564 0.0039610 0.7381326 0.0039712 0.7433434 0.0039813 0.7482426 0.0039712 0.7529860 0.0039813 0.7577987 0.0039712 0.7622998 0.0039915 0.7713018 0.0040119 0.7911064 0.0039915 0.7945342 0.0040017 0.7981869 0.0040119 0.8014242 0.0040221 0.8074314 0.0040119 0.8101147 0.0040221 0.8130404 0.0040119 0.8156025 0.0040221 0.8210730 0.0040119 0.8237044 0.0040221 0.8264223 0.0040526 0.8290191 0.0040628 0.8323083 0.0040933 0.8361688 0.0041035 0.8409814 0.0041239 0.8466942 0.0041137 0.8527880 0.0041239 0.8591068 0.0041341 0.864785 0.0041239 0.8703767 0.0041443 0.8760895 0.0041544 0.8858707 0.0041646 0.8995296 0.0041748 0.9034420 0.0041646 0.9072853 0.0041748 0.9111804 0.0041646 0.9148159 0.0041748 0.9187110 0.0041850 0.9221387 0.0041748 0.9254799 0.0041850 0.9288903 0.0041748 0.9322834 0.0041850 0.9357977 0.0041952 0.9453537
Output:
X Y 0.0036759 0.4018006 0.0036962 0.4074616 0.0037064 0.4216399 0.0037166 0.4438854 0.0037268 0.4628417 0.0037370 0.4894152 0.0037471 0.4952320 0.0037675 0.4979326 0.0037879 0.5014988 0.0038082 0.5057747 0.0038184 0.5558402 0.0038286 0.5613627 0.0038388 0.6026338 0.0038693 0.6709805 0.0038795 0.6808655 0.0038897 0.6866130 0.0039202 0.6981425 0.0039406 0.7057251 0.0039610 0.7216518 0.0039712 0.7433434 0.0039915 0.7713018 0.0040017 0.7981869 0.0040119 0.8014242 0.0040221 0.8264223 0.0040526 0.8290191 0.0040628 0.8323083 0.0040933 0.8361688 0.0041035 0.8409814 0.0041239 0.8591068 0.0041443 0.8760895 0.0041544 0.8858707 0.0041646 0.8995296 0.0041748 0.9187110 0.0041850 0.9357977 0.0041952 0.9453537

Replies are listed 'Best First'.
Re^2: How would you code this?
by BrowserUk (Patriarch) on Apr 07, 2016 at 15:01 UTC

    I concur that this produces the same output. My main objections to are: a) it uses more decision points to achieve it; b) it messes with the input array (curable).

    Thanks for your input.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I made a couple of slight revisions (eliminate the "last" statement and fixed a boundary condition issue at index 0). This still produces the same output.

      Appreciated your thanks. I leave "what is better" up to you.

      #! perl -slw use strict; printf "%s", scalar <DATA>; my @points = map[ split ' ', $_ ], <DATA>; my $i=0; while ($i < @points-2) #quit when there aren't 2 points left { my ($x1,$y1) = @{$points[$i]}; my ($x2,$y2) = @{$points[$i+1]}; if ( ($y2 - $y1 ) / ( $x2 - $x1 ) < 0 ) { splice @points,$i,2; #remove both points $i--; #backup a point to restart $i=0 if $i<0; #boundary condition at index 0 } else { $i++; } } print join ' ', @$_ for @points;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found