Popular Python recipes tagged "meta:requires=none"http://code.activestate.com/recipes/langs/python/tags/meta:requires=none/2014-11-15T09:06:05-08:00ActiveState Code RecipesGuess a number (Python) 2014-11-15T09:06:05-08:00Benoithttp://code.activestate.com/recipes/users/4191135/http://code.activestate.com/recipes/578962-guess-a-number/ <p style="color: grey"> Python recipe 578962 by <a href="/recipes/users/4191135/">Benoit</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sample/">sample</a>). </p> <p>This is just a little game to explain some very basic function in Python for beginner. RANDOM, Loop, Try and except... </p> Continous Fractions (Python) 2014-09-23T14:30:00-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578936-continous-fractions/ <p style="color: grey"> Python recipe 578936 by <a href="/recipes/users/4190606/">juan</a> . Revision 2. </p> <p>Calculate the continous fraction from a fraction or decimal number</p> Prime Factorization (Python) 2014-09-19T08:11:54-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578938-prime-factorization/ <p style="color: grey"> Python recipe 578938 by <a href="/recipes/users/4190606/">juan</a> . </p> <p>Calculates the prime factorization from a number.</p> division digit-by digit calculation (Python) 2014-09-19T08:33:35-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578939-division-digit-by-digit-calculation/ <p style="color: grey"> Python recipe 578939 by <a href="/recipes/users/4190606/">juan</a> . </p> <p>Calculate a division digit by digit with t decimals</p> Greatest common divisor (Python) 2014-09-19T08:03:02-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578937-greatest-common-divisor/ <p style="color: grey"> Python recipe 578937 by <a href="/recipes/users/4190606/">juan</a> . </p> <p>Calculate the Greatest common divisor from two numbers</p> First n primes numbers (Python) 2014-09-20T19:33:41-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578923-first-n-primes-numbers/ <p style="color: grey"> Python recipe 578923 by <a href="/recipes/users/4190606/">juan</a> . Revision 6. </p> <p>Using the Sieve of Eratosthenes find the first n primes numbers.</p> wrist friendly dictionary (Python) 2013-08-18T22:54:26-07:00Ariel Keselmanhttp://code.activestate.com/recipes/users/4187559/http://code.activestate.com/recipes/578644-wrist-friendly-dictionary/ <p style="color: grey"> Python recipe 578644 by <a href="/recipes/users/4187559/">Ariel Keselman</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/nested/">nested</a>). </p> <p>this dictionary allows easy manual creation of nested hierarchies, like so:</p> <p>window.style.width=5</p> <p>or... </p> <p>window['background-color'].rgb= 255,255,255</p> Type checking using Python 3.x annotations (Python) 2013-05-23T22:46:19-07:00David Mertzhttp://code.activestate.com/recipes/users/4173018/http://code.activestate.com/recipes/578528-type-checking-using-python-3x-annotations/ <p style="color: grey"> Python recipe 578528 by <a href="/recipes/users/4173018/">David Mertz</a> (<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/type/">type</a>, <a href="/recipes/tags/typecheck/">typecheck</a>). Revision 5. </p> <p>Some other recipes have been suggested to allow type checking by various means. Some of these require the use of type specification in a decorator itself. Others try to be much more elaborate in processing a large variety of annotations (but hence require much more and more convoluted code).</p> <p>The recipe provided below is very short, and simply provides actual <strong>type</strong> checking of arguments and return values. It utilizes an unadorned decorator, rather than manufacture one that is parameterized by types or other arguments.</p> Easy to use, easy to read, python based HTML generation (Python) 2013-06-21T14:47:21-07:00Pavloshttp://code.activestate.com/recipes/users/4185038/http://code.activestate.com/recipes/578436-easy-to-use-easy-to-read-python-based-html-generat/ <p style="color: grey"> Python recipe 578436 by <a href="/recipes/users/4185038/">Pavlos</a> (<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/template/">template</a>, <a href="/recipes/tags/text/">text</a>). Revision 4. </p> <p>I was looking for a simple way to generate HTML directly in python that does not require learning a new template 'language' nor requires the installation of a big complex package. Closest thing I found was James Casbon's attempt(https://gist.github.com/1461441). This is my version of the same idea. </p> <p>(2013-04-21) added some simplifications and support for switching off string interpolation. Added to github:</p> <p><a href="https://github.com/pavlos-christoforou/web" rel="nofollow">https://github.com/pavlos-christoforou/web</a></p> Recipe With No Name Yet (Python) 2012-08-22T17:57:12-07:00Cyrilhttp://code.activestate.com/recipes/users/4182937/http://code.activestate.com/recipes/578221-recipe-with-no-name-yet/ <p style="color: grey"> Python recipe 578221 by <a href="/recipes/users/4182937/">Cyril</a> (<a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). Revision 9. </p> <p>Not a very new recipe, but a short one and (I hope) useful :</p> <ul> <li>wrapping any function "f" with two decorators "enter_event" and "exit_event" that will trigger calling of user functions when, ... hum ... evidently, <strong>just before entering and just after exiting the "f" function</strong>.</li> </ul> <p>Typical usages :</p> <ul> <li>debugging on a function by function basis : <ul> <li>emit a trace in log file to see when functions are called and check sequences correctness (very usefull when programming by events)</li> <li>feed a profile analyzer (by fine tuning which functions are enabled)</li> <li>feed a code coverage analyzer ( " )</li> </ul></li> <li>kind of validator on function calling : <ul> <li>implement programming by contracts : <ul> <li>check that parameters values of "f" function will not have an unexpected value or be of an unexpected type</li> <li>this allow to increase code robustness by narrowing </li> </ul></li> <li>implement invariants (eg. check that returned value is always in the excepted range, ...)</li> <li>insure that a function follow specifications by meta-checking that has always predictable results (eg. return the fixed expected value for each possible input value, ...)</li> </ul></li> <li>minimum modification of existing code</li> <li><strong>in the same thinking line as the "monkey patching" concept</strong></li> </ul> <p>Notes on usage :</p> <ul> <li>recipe works on functions and any kind of methods (methods, class methods, and static methods)</li> <li>the usage order of "@enter_event" and "@exit_event" decorators doesn't matter : the result will be the same</li> </ul> <ul> <li><em>PLEASE VOTE FOR THIS RECIPE if you like it !</em></li> </ul> Determinant of matrix of any order (Python) 2012-04-24T10:49:00-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578108-determinant-of-matrix-of-any-order/ <p style="color: grey"> Python recipe 578108 by <a href="/recipes/users/4181845/">Sachin Joglekar</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/determinant/">determinant</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/order/">order</a>). </p> <p>A small snipet of code to find the determinant of a mtrix of any order.Input must be a list like [[1,2,3],[4,5,6],[7,8,9]] (for a matrix of order 3). Works fine.</p> Output data in tabular form (Python) 2012-04-20T19:27:57-07:00srikantkthttp://code.activestate.com/recipes/users/4181723/http://code.activestate.com/recipes/578105-output-data-in-tabular-form/ <p style="color: grey"> Python recipe 578105 by <a href="/recipes/users/4181723/">srikantkt</a> (<a href="/recipes/tags/tabular/">tabular</a>). Revision 4. </p> <p>Often times, data lists need to be output in tabular format. Here is a sample that allows specifying table heading and data rows. Came across <a href="http://code.activestate.com/recipes/577615/">recipe 577615</a> after I posted but I am still biased.</p> Silmultaneous linear equation solver(any order) (Python) 2012-04-24T12:29:30-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578109-silmultaneous-linear-equation-solverany-order/ <p style="color: grey"> Python recipe 578109 by <a href="/recipes/users/4181845/">Sachin Joglekar</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/crammer/">crammer</a>, <a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/equation/">equation</a>, <a href="/recipes/tags/linear/">linear</a>, <a href="/recipes/tags/simultaneous/">simultaneous</a>). </p> <p>Solves simultaneous linear equations of any order using Crammer's rule. Required input is two lists..one for coefficients, and other for constants eg. 2x+3y=8 x+4y=6 will be written as simul([[2,3],[1,4]],[8,6])</p> Finding complete loops in a map of connected nodes (Python) 2012-04-24T18:47:21-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578110-finding-complete-loops-in-a-map-of-connected-nodes/ <p style="color: grey"> Python recipe 578110 by <a href="/recipes/users/4181845/">Sachin Joglekar</a> (<a href="/recipes/tags/circuit/">circuit</a>, <a href="/recipes/tags/connection/">connection</a>, <a href="/recipes/tags/loops/">loops</a>). </p> <p>This module finds loops in a given map.Input is a dictionary like</p> <p>d={1:[2,4,5,6],2:[1,3],3:[2,4,5,6],4:[1,3,5],5:[1,3,4],6:[1,3]}</p> <p>this means node 1 is connected to nodes 2,4,5 and 6 and so on..</p> <p>Output is a list of complete loops. for above examples,output is</p> <p>[[1, 4, 5, 1], [3, 4, 5, 3], [1, 2, 3, 4, 1], [1, 2, 3, 5, 1], [1, 2, 3, 6, 1], [1, 4, 3, 5, 1], [1, 4, 3, 6, 1], [1, 5, 3, 6, 1], [1, 2, 3, 4, 5, 1], [1, 4, 5, 3, 6, 1]]</p> Finding the GCD of a list of numbers (a.k.a. Reducing numbers in a list) (Python) 2010-07-06T18:52:55-07:00Stephen Akikihttp://code.activestate.com/recipes/users/4172143/http://code.activestate.com/recipes/577282-finding-the-gcd-of-a-list-of-numbers-aka-reducing-/ <p style="color: grey"> Python recipe 577282 by <a href="/recipes/users/4172143/">Stephen Akiki</a> (<a href="/recipes/tags/common/">common</a>, <a href="/recipes/tags/denominator/">denominator</a>, <a href="/recipes/tags/gcd/">gcd</a>, <a href="/recipes/tags/greatest/">greatest</a>, <a href="/recipes/tags/lisst/">lisst</a>, <a href="/recipes/tags/reduce/">reduce</a>). </p> <p><a href="http://akiscode.com/articles/gcd_of_a_list.shtml" rel="nofollow">http://akiscode.com/articles/gcd_of_a_list.shtml</a></p> <p>This python code snippet allows you to find the GCD of a list of numbers, after this it is a simple matter of diving all the numbers in the list by the GCD to reduce it.</p> <p>Why this works...</p> <p>The GCD of a list of numbers [a, b, c] is GCD(GCD(a, b), c). The reduce function does exactly this and thus gives you the GCD of all the numbers in the list.</p> Infinite Stream Divisor (Python) 2010-07-21T12:50:17-07:00Narayana Chikkamhttp://code.activestate.com/recipes/users/4174427/http://code.activestate.com/recipes/577326-infinite-stream-divisor/ <p style="color: grey"> Python recipe 577326 by <a href="/recipes/users/4174427/">Narayana Chikkam</a> (<a href="/recipes/tags/divisor/">divisor</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/stream/">stream</a>). </p> <p>Maintain an F.S.A to keep track of the consequent remainders as states, input symbols as driving actions on each state. O(N) is the time complexity to find the given large string [in some radix(R), for some specific divisor(D)], where N is the length of the Input String which confirms to the Language Rules under the alphabet. O(R*D) is the space complexity to keep the F.S.A in memory for lookup!</p> Eight Queens With out Permutations (Python) 2010-07-21T10:11:20-07:00Narayana Chikkamhttp://code.activestate.com/recipes/users/4174427/http://code.activestate.com/recipes/577325-eight-queens-with-out-permutations/ <p style="color: grey"> Python recipe 577325 by <a href="/recipes/users/4174427/">Narayana Chikkam</a> (<a href="/recipes/tags/eight/">eight</a>, <a href="/recipes/tags/queens/">queens</a>). </p> <p>Eight Queens is one of the popular algorithms in backtracking. The solution given below uses simple math to reduce the processing. The logic is keep placing the coins on the board with below rules:</p> <ol> <li>Don't place the coin if there is another coin present in the same row</li> <li>Don't place the coin if there is another coin present in the same col</li> <li>Don't place the coin if there is another coin present in any of the diagonal lines.</li> </ol> <p>Keep repeating the above 3 rules recursively until we keep all the coins. Problem Definition: <a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle" rel="nofollow">http://en.wikipedia.org/wiki/Eight_queens_puzzle</a></p> iter_except -- a useful variant of __builtin__.iter() (Python) 2010-03-27T02:04:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577155-iter_except-a-useful-variant-of-__builtin__iter/ <p style="color: grey"> Python recipe 577155 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). </p> <p>Variant of iter(func, sentinel) that looks for an exception rather than for a sentinel value. Good for making iterators from of APIs that advance over a data and return an exception when they are done.</p> Pi_Approximation (Python) 2011-01-27T12:50:56-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577067-pi_approximation/ <p style="color: grey"> Python recipe 577067 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/mathematics/">mathematics</a>). Revision 3. </p> <p>Pi is a constant, an irrational number and its notation represent the sixteenth letter of the Greek alphabet, though its value is equal to the ratio of a circle’s circumference to its diameter.</p> <p>However, <strong>a Muslim scientist Abu-Abdullah Muhammad Ibn Musa al-Khwarizmi</strong> the founder of algebra ( derived from the Arabic word El-Jabr ), arithmetic, and trigonometry, an astronomer, and a geographer C850 studied and proved Pi’s irrationality.(<strong>algorithm is derived from his name Khwarizmi</strong> ).</p> <p>I used the circle equation x²+y² = 1 of radius 1 centred at (0,0) and the circle area’s formula to approximate Pi value. y²= 1-x² and y = √(1-x²) is the upper semicircle which will extend from x = -1 to x = 1 and the rectangle method is used to approximate this area of an interval [-1,1]. </p> <p>The area of a circle is A = πr², thus the semicircle of radius 1 will have an area A = ½ π(1²).</p> <p>Using my Pi_Approximation program allows you to represent Pi to infinity of digits, and this will depend of the project type and the precision needed, though space programs require to approximate pi at its maximum precision. </p> <p>And my Pi_Approximation program allows you to make a better Pi’s approximation than the scientific Calculators </p> Pi_Approximation (Python) 2010-02-25T04:50:58-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577066-pi_approximation/ <p style="color: grey"> Python recipe 577066 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Pi is a constant, an irrational number and its notation represent the sixteenth letter of the Greek alphabet, though its value is equal to the ratio of a circle’s circumference to its diameter.</p> <p>However,<strong>a Muslim scientist Abu-Abdullah Muhammad Ibn Musa al-Khwarizmi</strong> the founder of algebra ( derived from the Arabic word El-Jabr ), arithmetic, and trigonometry, an astronomer, and a geographer C850 studied and proved Pi’s irrationality.(algorithm is derived from his name Khwarizmi ).</p> <p>I used the circle equation x²+y² = 1 of radius 1 centred at (0,0) and the circle area’s formula to approximate Pi value. y²= 1-x² and y = square-root(1-x²) is the upper semicircle which will extend from x = -1 to x = 1 and the rectangle method is used to approximate this area of an interval [-1,1].</p> <p>The area of a circle is A = π r², thus the semicircle of radius 1 will have an area A = ½ π(1²).</p> <p>Using my Pi_Approximation program allows you to represent Pi to infinity of digits, and this will depend of the project type and the precision needed, though space programs require to approximate pi at its maximum precision.</p> <p>And my Pi_Approximation program allows you to make a better Pi’s approximation than the scientific Calculators </p>