Popular recipes tagged "algorithms" but not "python"http://code.activestate.com/recipes/tags/algorithms-python/2016-09-19T18:03:09-07:00ActiveState Code RecipesReversi Othello (Python) 2016-09-19T18:03:09-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580698-reversi-othello/ <p style="color: grey"> Python recipe 580698 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/games/">games</a>). </p> <p>Reversi/Othello Board Game using Minimax, Alpha-Beta Pruning, Negamax, Negascout algorithms.</p> Simple Infix Expression Evaluation (Python) 2015-11-07T18:44:05-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579122-simple-infix-expression-evaluation/ <p style="color: grey"> Python recipe 579122 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/stack/">stack</a>). </p> <p>Simple infix expression evaluation using a stack.</p> Hopfield Artificial Neural Network (C++) 2016-07-03T21:58:49-07:00Jay ballerhttp://code.activestate.com/recipes/users/4194368/http://code.activestate.com/recipes/580688-hopfield-artificial-neural-network/ <p style="color: grey"> C++ recipe 580688 by <a href="/recipes/users/4194368/">Jay baller</a> (<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/neural_network/">neural_network</a>). </p> <p>(Discrete (Binary)) Hopfield Artificial Neural Network (ANN).</p> <p>For more info see:</p> <p><a href="http://en.wikipedia.org/wiki/Hopfield_net" rel="nofollow">http://en.wikipedia.org/wiki/Hopfield_net</a></p> <p><a href="http://www.scholarpedia.org/article/Hopfield_network" rel="nofollow">http://www.scholarpedia.org/article/Hopfield_network</a></p> Infix Expression Evaluation (Python) 2015-11-08T05:08:26-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579123-infix-expression-evaluation/ <p style="color: grey"> Python recipe 579123 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/stack/">stack</a>). </p> <p>Infix expression evaluation using two stacks.</p> Python A* Pathfinding (With Binary Heap) (Python) 2014-08-06T10:03:01-07:00Christian Careagahttp://code.activestate.com/recipes/users/4186639/http://code.activestate.com/recipes/578919-python-a-pathfinding-with-binary-heap/ <p style="color: grey"> Python recipe 578919 by <a href="/recipes/users/4186639/">Christian Careaga</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/binary_search/">binary_search</a>). Revision 3. </p> <p>Implementation of the A-star Pathfinding algorithm in Python, using Binary heap to sort the open list</p> edge-coloring of a bipartite graph (Python) 2015-07-30T13:40:41-07:00Praveenhttp://code.activestate.com/recipes/users/4192603/http://code.activestate.com/recipes/579088-edge-coloring-of-a-bipartite-graph/ <p style="color: grey"> Python recipe 579088 by <a href="/recipes/users/4192603/">Praveen</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Konig's theorem tells us that every bipartite graph with maximum vertex-degree d can be edge-colored with just d colors. This recipe relies on a previous recipe by D.Eppstein (123641) : "Hopcroft-Karp bipartite matching".</p> Priority queue dictionary (Python) 2013-08-25T00:23:12-07:00Nezar Abdennurhttp://code.activestate.com/recipes/users/4187557/http://code.activestate.com/recipes/578643-priority-queue-dictionary/ <p style="color: grey"> Python recipe 578643 by <a href="/recipes/users/4187557/">Nezar Abdennur</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/heap/">heap</a>, <a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/pqdict/">pqdict</a>, <a href="/recipes/tags/priority_queue/">priority_queue</a>). Revision 4. </p> <p>An indexed priority queue implemented in pure python as a dict-like class. It is a stripped-down version of <a href="https://pypi.python.org/pypi/pqdict/">pqdict</a>. A Priority Queue Dictionary maps dictionary keys (dkeys) to updatable priority keys (pkeys).</p> <p>The priority queue is implemented as a binary heap, which supports: </p> <ul> <li><p>O(1) access to the top priority element </p></li> <li><p>O(log n) removal of the top priority element </p></li> <li><p>O(log n) insertion of a new element</p></li> </ul> <p>In addition, an internal dictionary or "index" maps dictionary keys to the position of their entry in the heap. This index is maintained as the heap is manipulated. As a result, a PQ-dict also supports: </p> <ul> <li><p>O(1) lookup of an arbitrary element's priority key </p></li> <li><p>O(log n) removal of an arbitrary element </p></li> <li><p>O(log n) updating of an arbitrary element's priority key</p></li> </ul> <p>A data structure like this can be used to drive simulations, schedulers, certain greedy algorithms, merging streams of sorted data, and other applications where priorities may need to be changed frequently.</p> Knight's Tour using Warnsdorff Algorithm (Python) 2012-12-17T06:04:19-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578382-knights-tour-using-warnsdorff-algorithm/ <p style="color: grey"> Python recipe 578382 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Knight's Tour using Warnsdorff Algorithm.</p> Knight's Tour Map using Warnsdorff's Algorithm (Python) 2012-12-18T01:05:51-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578386-knights-tour-map-using-warnsdorffs-algorithm/ <p style="color: grey"> Python recipe 578386 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>). </p> <p>Solves Knight's Tour Problem using Warnsdorff's Algorithm for every square on the chessboard of arbitrary size.</p> <p>It colors each initial square with a color that depends on the final square coordinates. (This is a method used to create fractals.) Many different formulas maybe used for coloring (based on abs/rel x/y/dist/ang).</p> <p>Calculating each tour in full creates a highly chaotic image (and takes hours). So I added maxItPercent to cut-off tours earlier. (Using %10 creates an image that has both ordered and chaotic regions.)</p> Averaging Literal Numbers (Python) 2012-12-06T13:44:42-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578365-averaging-literal-numbers/ <p style="color: grey"> Python recipe 578365 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/numbers/">numbers</a>). </p> <p>One program that a teacher may assign to beginning programming students would be to take a list of numbers and average them together. This recipe does just that and is designed to be easy to read and follow, showing what is possible with a few lines of Python.</p> A-star Shortest Path Algorithm (C++) 2010-12-25T23:36:35-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/ <p style="color: grey"> C++ recipe 577457 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/graphs/">graphs</a>, <a href="/recipes/tags/routes/">routes</a>). Revision 4. </p> <p>A-star (A*) is a shortest path algorithm widely used for RTS games, GPS navigation etc.</p> Converts from decimal to any base ( between 2 and 26 ) (Python) 2011-02-24T00:33:07-08:00Shashwat Anandhttp://code.activestate.com/recipes/users/4172995/http://code.activestate.com/recipes/577586-converts-from-decimal-to-any-base-between-2-and-26/ <p style="color: grey"> Python recipe 577586 by <a href="/recipes/users/4172995/">Shashwat Anand</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/base/">base</a>). </p> <p>This function takes in any base-10 integer and returns the string representation of that number in its specified base-n form. The code was inspired from <a href="http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/" rel="nofollow">http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/</a> , thereby improving upon it.</p> Caching decorator with timeout, selective invalidation (Python) 2005-11-05T05:47:03-08:00Greg Steffensenhttp://code.activestate.com/recipes/users/2649990/http://code.activestate.com/recipes/442513-caching-decorator-with-timeout-selective-invalidat/ <p style="color: grey"> Python recipe 442513 by <a href="/recipes/users/2649990/">Greg Steffensen</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>A caching decorator that garbage collects in a separate thread (for performance), allows each cached function to (optionally) set a custom maximum age for entries, and allows individual cache entries to be selectively invalidated.</p> get all possible combinations of characters given a string (Python) 2011-08-15T04:15:42-07:00Yanghttp://code.activestate.com/recipes/users/4178968/http://code.activestate.com/recipes/577842-get-all-possible-combinations-of-characters-given-/ <p style="color: grey"> Python recipe 577842 by <a href="/recipes/users/4178968/">Yang</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>This will give a result that is more than a permutation, but all possible combinations. An example is when input is 'abc', the output would be: a,ab,abc,ac,acb,b,ba,bac,bc,bca,c,ca,cab,cb,cba</p> Shannon-Fano Data Compression (Python) 2010-12-18T22:09:40-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577502-shannon-fano-data-compression/ <p style="color: grey"> Python recipe 577502 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/data_compression/">data_compression</a>). Revision 3. </p> <p>Shannon-Fano Data Compression. It can compress any kind of file up to 4 GB.</p> <p>(But trying to compress an already compressed file like zip, jpg etc. can produce a (slightly) larger file though. Shannon-Fano is not the best data compression algorithm anyway.)</p> Compare algorithms for heapq.smallest (Python) 2011-12-25T23:41:23-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest/ <p style="color: grey"> Python recipe 577573 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/heaps/">heaps</a>, <a href="/recipes/tags/performance/">performance</a>). Revision 3. </p> <p>General purpose technique for counting comparisons in various searching and sorting applications.</p> Python Binary Search Tree (Python) 2011-08-08T00:15:09-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577830-python-binary-search-tree/ <p style="color: grey"> Python recipe 577830 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/maximum/">maximum</a>, <a href="/recipes/tags/minimum/">minimum</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>A data structure that holds a sorted collection of values, and supports efficient insertion, deletion, sorted iteration, and min/max finding. Values may sorted either based on their natural ordering, or on a key function (specified as an argument to the search tree's constructor). The search tree may contain duplicate values (or multiple values with equal keys) -- the ordering of such values is undefined.</p> <p>This implementation was made with efficiency in mind. In particular, it is more than twice as fast as the other native-Python implementations I tried (which all use objects to store search tree nodes).</p> <p>See also: <a href="http://en.wikipedia.org/wiki/Binary_search_tree">http://en.wikipedia.org/wiki/Binary_search_tree</a>, <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">http://en.wikipedia.org/wiki/A*_search_algorithm</a></p> Binary Search Tree (C++) 2011-08-08T00:13:44-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577828-binary-search-tree/ <p style="color: grey"> C++ recipe 577828 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Binary Search Tree.</p> SleepSort (Timer version) (Python) 2011-06-18T05:37:06-07:00wong2http://code.activestate.com/recipes/users/4178345/http://code.activestate.com/recipes/577762-sleepsort-timer-version/ <p style="color: grey"> Python recipe 577762 by <a href="/recipes/users/4178345/">wong2</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/interest/">interest</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>Python version sleep sort by using threading.Timer</p> Binary Search Tree (C++) 2011-01-26T22:48:06-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577552-binary-search-tree/ <p style="color: grey"> C++ recipe 577552 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Binary Search Tree.</p>