tag:blogger.com,1999:blog-11885757.post7865593242439055306..comments2024-03-27T12:19:23.996-04:00Comments on Medicine for the Sky: kNN, Finding the k nearest neighbors of points on a Cartesian planeCurtishttp://www.blogger.com/profile/13175947559336769793noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-11885757.post-61189834287880022652009-11-09T13:35:57.592-05:002009-11-09T13:35:57.592-05:00Hey. I figured out that part of the code was missi...Hey. I figured out that part of the code was missing after I read through your whole post. I didn't realize it was part of a Facebook coding contest right away.emily1https://www.blogger.com/profile/13436512495391302621noreply@blogger.comtag:blogger.com,1999:blog-11885757.post-63455352172725190632009-11-09T07:19:02.850-05:002009-11-09T07:19:02.850-05:00Emily,
I didn't post the entire solution to t...Emily,<br /><br />I didn't post the entire solution to the problem here, because I didn't want anyone to be able to win the Facebook puzzle (smallworld) by googling an answer instead of building one.<br /><br />There is a sub earlier in the script that parses out the input file, throwing the x coordinates into array @x, and the y to @y. It goes something like this:<br /><br />my @x = ();<br />my @y = ();<br /><br />sub read_input {<br /> while(<>) {<br />#integer space real space real<br /> if (/(\d+)\s+([\d.-]+)\s+([\d.-]+)/) {<br /> $points++;<br /> $label[$points] = $1;<br /> $x[$points] = $2;<br /> $y[$points] = $3;<br /> }<br />}Curtishttps://www.blogger.com/profile/13175947559336769793noreply@blogger.comtag:blogger.com,1999:blog-11885757.post-22267186903733538142009-11-04T22:06:07.804-05:002009-11-04T22:06:07.804-05:00it's me again.. are you using the $y in two di...it's me again.. are you using the $y in two different contexts? I understand that it is a true/false value for sorting the array by x or y coordinates for the points. What I don't get is where and how you're storing the x and y coordinates for each point.emily1https://www.blogger.com/profile/13436512495391302621noreply@blogger.comtag:blogger.com,1999:blog-11885757.post-89708366928887693702009-11-04T21:51:56.135-05:002009-11-04T21:51:56.135-05:00I'm confused by this:
my @sorted = $y ? sort ...I'm confused by this:<br /><br />my @sorted = $y ? sort {$y[$a] <=> $y[$b]} @{$tree[$node]}<br /> : sort {$x[$a] <=> $x[$b]} @{$tree[$node]};<br /><br />and <br /><br />What are $y[$a], $x[$a] in the sort algorithm? I see that $y ia a parameter to your tree building function. But where is $x defined. I don't see if declare anywhere.emily1https://www.blogger.com/profile/13436512495391302621noreply@blogger.com