Popular recipes by Sander Evers http://code.activestate.com/recipes/users/4173111/2014-02-19T12:53:03-08:00ActiveState Code RecipesString templates with adaptive indenting (Python) 2014-02-19T12:53:03-08:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578835-string-templates-with-adaptive-indenting/ <p style="color: grey"> Python recipe 578835 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/formatter/">formatter</a>, <a href="/recipes/tags/indent/">indent</a>, <a href="/recipes/tags/templating/">templating</a>). </p> <p>An extension of Python's <code>'Hello {fieldname}!'.format(fieldname='world')</code> functionality for multi-line strings. When <code>{fieldname}</code> is indented, all the lines in the inserted <code>fieldvalue</code> are indented to the same amount.</p> Finding sets in the card game SET! (Python) 2013-04-05T12:49:36-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578508-finding-sets-in-the-card-game-set/ <p style="color: grey"> Python recipe 578508 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/setgame/">setgame</a>). Revision 2. </p> <p>In the card game <a href="http://en.wikipedia.org/wiki/Set_%28game%29">SET!</a>, players are shown an array of 12 (or more) symbol cards and try to identify a so-called 3-card <strong>set</strong> among these cards as quickly as possible.</p> <p>A card has four attributes (number, shape, color and shading), each of which can take 3 possible values. In a <strong>set</strong>, for each attribute, all three cards should have either the same value, or the three different values.</p> <p>This recipe solves the problem of finding <em>all</em> sets within an array of an arbitrary number of cards, showing some clever optimizations and celebrating the clarity of Python in expressing the algorithms.</p> Hash collision probability / Birthday problem (Python) 2012-12-21T09:32:54-08:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578387-hash-collision-probability-birthday-problem/ <p style="color: grey"> Python recipe 578387 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/birthday/">birthday</a>, <a href="/recipes/tags/collision/">collision</a>, <a href="/recipes/tags/hash/">hash</a>). </p> <p>Calculates the probability that, when making <em>k</em> random selections out of <em>n</em> possibilities, at least two of the selections are the same. See: <a href="http://en.wikipedia.org/wiki/Birthday_problem" rel="nofollow">http://en.wikipedia.org/wiki/Birthday_problem</a></p> <p>What is the probability that (at least) two people in a class of 30 share their birthday?</p> <pre class="prettyprint"><code>&gt;&gt;&gt; collide(30,365) 0.7063162427192688 </code></pre> <p>What is the probability that ORA_HASH generates the same hash when hashing 25000 values?</p> <pre class="prettyprint"><code>&gt;&gt;&gt; collide(25000,int(4.3e9)) 0.07009388771353198 </code></pre> Sharing-aware tree transformations (Python) 2012-05-07T08:20:58-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578117-sharing-aware-tree-transformations/ <p style="color: grey"> Python recipe 578117 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/fold/">fold</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/sharing/">sharing</a>, <a href="/recipes/tags/tree/">tree</a>, <a href="/recipes/tags/yaml/">yaml</a>). Revision 2. </p> <p>The function <code>foldsh</code> in this recipe is a general purpose tool for transforming tree-like recursive data structures while keeping track of shared subtrees.</p> <pre class="prettyprint"><code># By default, a branch is encoded as a list of subtrees; each subtree can be a # branch or a leaf (=anything non-iterable). Subtrees can be shared: &gt;&gt;&gt; subtree = [42,44] &gt;&gt;&gt; tree = [subtree,[subtree]] # We can apply a function to all leaves: &gt;&gt;&gt; foldsh(tree, leaf= lambda x: x+1) [[43, 45], [[43, 45]]] # Or apply a function to the branches: &gt;&gt;&gt; foldsh(tree, branch= lambda t,c: list(reversed(c))) [[[44, 42]], [44, 42]] # The sharing is preserved: &gt;&gt;&gt; _[0][0] is _[1] True # Summing up the leaves without double counting of shared subtrees: &gt;&gt;&gt; foldsh(tree, branch= lambda t,c: sum(c), shared= lambda x: 0) 86 </code></pre> <p>In particular, it is useful for transforming YAML documents. An example of this is given below.</p> Cycle-aware tree transformations (Python) 2012-06-20T08:09:13-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578118-cycle-aware-tree-transformations/ <p style="color: grey"> Python recipe 578118 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/cyclic/">cyclic</a>, <a href="/recipes/tags/fold/">fold</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/yaml/">yaml</a>). </p> <p>A variation on <a href="http://code.activestate.com/recipes/578117/">Recipe 578117</a> that can deal with cycles. A cycle means that a tree has itself as a subtree somewhere. A fold over such a data structure has a chicken-and-egg-problem: it needs its own result in order to construct its own result. To solve this problem, we let <code>branch</code> construct a <em>part</em> of its result before going into recursion. After the recursion, <code>branch</code> gets a chance to complete its result using its children's results. Python's support for coroutines (using <code>yield</code>) provides a nice way to define such a two-stage <code>branch</code> function.</p> Nest a flat list (Python) 2010-02-25T03:50:09-08:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/577061-nest-a-flat-list/ <p style="color: grey"> Python recipe 577061 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/nest/">nest</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/unflatten/">unflatten</a>). Revision 2. </p> <p>Turn a flat list into a nested list, with a specified number of lists per nesting level.</p>