Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are a few thoughts:</p> <ul> <li>If you want to return only unique results, you should probably use a set and convert it to a list when you return. The one catch is that <code>self.data</code> has to be immutable, for instance a tuple instead of a list.</li> <li>Because you're threading <code>result</code> through the recursion <em>and</em> adding appending the results of the recursive calls to it, you're explicitly adding each hit to the result at least twice. Threading the result through the recursion will keep you from creating and throwing away data structures, so you can probably just do that.</li> <li>As mgilson points out, because of the way Python handles default arguments, setting <code>result</code> to an empty list in the function declaration <strong>won't do what you think</strong>. Every time you call <code>within_radius</code> without explicitly passing in <code>result</code>, the hits will be accumulated for every call, not just for the individual call. (Did that make sense? See <a href="http://www.deadlybloodyserious.com/2008/05/default-argument-blunders/" rel="nofollow">this</a> ). mgilson's answer points this out too.</li> </ul> <p>With all that in mind, I'd probably do something like this:</p> <pre class="lang-python prettyprint-override"><code>def within_radius(self, point, radius, result=None): d = self.discriminator result = set() if result is None else result if in_circle(point, radius, self.data): result.add(self.data) if point[d] - radius &lt; self.data[d] and self.l_child: self.l_child.within_radius(point, radius, result) if point[d] + radius &gt; self.data[d] and self.r_child: self.r_child.within_radius(point, radius, result) return list(result) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload