Popular recipes tagged "recursive"http://code.activestate.com/recipes/tags/recursive/2016-03-01T20:41:13-08:00ActiveState Code RecipesComputing simple list functions recursively (Python) 2016-03-01T20:41:13-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580617-computing-simple-list-functions-recursively/ <p style="color: grey"> Python recipe 580617 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/recursive/">recursive</a>). </p> <p>This recipe shows how to compute some simple list functions (functions that operate on lists) using simple recursive algorithms. Simple non-recursive algorithms exist for these tasks; this recipe is just for illustrative purposes. However, the principles described can be used for more complex recursive algorithms, for which non-recursive algorithms might be more complex than the recursive ones.</p> Traverse dotted attribute of an object using built-in function reduce (Python) 2013-01-05T05:49:27-08:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578398-traverse-dotted-attribute-of-an-object-using-built/ <p style="color: grey"> Python recipe 578398 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bif/">bif</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/reduce/">reduce</a>). </p> <p>Making good use of reduce() to traverse dotted attribute of an object.</p> Super Simple Sudoku Solver in Python source code (Python) 2012-06-23T14:56:05-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578140-super-simple-sudoku-solver-in-python-source-code/ <p style="color: grey"> Python recipe 578140 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/recurssion/">recurssion</a>, <a href="/recipes/tags/sodoku/">sodoku</a>, <a href="/recipes/tags/sudoku/">sudoku</a>). Revision 5. </p> <p>A simple algorithm which uses a recursive function to solve the puzzle.</p> <hr /> <p>THE ALGORITHM</p> <p>The credit for this algorithm must go to Richard Buckland: <a href="http://www.youtube.com/watch?v=bjObm0hxIYY&amp;feature=autoplay&amp;list=PL6B940F08B9773B9F&amp;playnext=1" rel="nofollow">http://www.youtube.com/watch?v=bjObm0hxIYY&amp;feature=autoplay&amp;list=PL6B940F08B9773B9F&amp;playnext=1</a></p> <p>Takes a partially filled in grid, inserts the min value in a cell (could be a random cell, in this case the first free cell). If the min value is not legal it will increment until the max value is reached (number 9), checking each time if the incremented value is legal in that cell (ie does not clash with any already entered cells in square, col or row). If it is legal, it will call itself (the hasSolution function) thus using this slightly more filled in grid to find a new cell and check which value is legal in this next cell. If no values are legal in the next cell, it will clear the previous grid entry and try incrementing the value.</p> <p>isLegal = does not conflict with any other numbers in the same row, column or square</p> Recursively walk Python objects (Python) 2011-12-23T22:10:38-08:00Yaniv Akninhttp://code.activestate.com/recipes/users/4180246/http://code.activestate.com/recipes/577982-recursively-walk-python-objects/ <p style="color: grey"> Python recipe 577982 by <a href="/recipes/users/4180246/">Yaniv Aknin</a> (<a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/recursive_iterator/">recursive_iterator</a>, <a href="/recipes/tags/walk/">walk</a>). Revision 2. </p> <p>A small function that walks over pretty much any Python object and yields the objects contained within (if any) along with the path to reach them. I wrote it and am using it to validate a deserialized data-structure, but you can probably use it for many things.</p> Recursively defined, Haskell-style infinite lists (Python) 2012-05-04T14:09:14-07:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578119-recursively-defined-haskell-style-infinite-lists/ <p style="color: grey"> Python recipe 578119 by <a href="/recipes/users/4181975/">John Crichton</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/recursive/">recursive</a>). Revision 2. </p> <p>A decorator to simplify the creation of recursively defined, Haskell-style infinite lists -- ie. recursive generators -- inspired by Raymond Hettinger's "Technique for cyclical iteration" [*]. </p> <p>[*] <a href="http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/" rel="nofollow">http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/</a> </p> Simple recursive function to non-recursive function (Python) 2011-05-27T01:47:37-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577724-simple-recursive-function-to-non-recursive-functio/ <p style="color: grey"> Python recipe 577724 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/substitute/">substitute</a>). </p> <p>This recipe is a simple solution for turning a recursive function into a non-recursive function.</p>