Most viewed recipes tagged "meta:requires=math"http://code.activestate.com/recipes/tags/meta:requires=math/views/2017-07-22T15:52:01-07:00ActiveState Code RecipesUno (Text-Based) (Python)
2017-07-15T00:46:59-07:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580811-uno-text-based/
<p style="color: grey">
Python
recipe 580811
by <a href="/recipes/users/4194238/">Brandon Martin</a>
(<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/text_game/">text_game</a>, <a href="/recipes/tags/uno/">uno</a>).
</p>
<p>A text based recreation of the classic card game featuring functional AIs to play with. Some rules have been modified. User interface is text based, non-curses, using only simple python commands to draw it. </p>
Binary floating point summation accurate to full precision (Python)
2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/
<p style="color: grey">
Python
recipe 393090
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p>
Simple Web Crawler (Python)
2011-01-31T21:57:58-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/576551-simple-web-crawler/
<p style="color: grey">
Python
recipe 576551
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>NOTE: This recipe has been updated with suggested improvements since the last revision.</p>
<p>This is a simple web crawler I wrote to
test websites and links. It will traverse
all links found to any given depth.</p>
<p>See --help for usage.</p>
<p>I'm posting this recipe as this kind of
problem has been asked on the Python
Mailing List a number of times... I
thought I'd share my simple little
implementation based on the standard
library and BeautifulSoup.</p>
<p>--JamesMills</p>
Size of Python objects (revised). (Python)
2017-07-22T15:52:01-07:00Jean Brouwershttp://code.activestate.com/recipes/users/2984142/http://code.activestate.com/recipes/546530-size-of-python-objects-revised/
<p style="color: grey">
Python
recipe 546530
by <a href="/recipes/users/2984142/">Jean Brouwers</a>
(<a href="/recipes/tags/getsizeof/">getsizeof</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>).
Revision 21.
</p>
<p>This recipe determines the size of Python objects in bytes and has been further enhanced to handle ints, namedtuples, arrays and NumPy types better. Functions <em>alen</em> and <em>itemsize</em> have been updated. Support for Python 2.5 and earlier and the tests/examples have been removed. See project <a href="https://github.com/pympler/pympler">Pympler</a> for unit tests.</p>
<p>See also other, simpler recipes like this <a href="http://code.activestate.com/recipes/577504">Compute memory footprint of an object and its contents</a>.</p>
Efficient Running Median using an Indexable Skiplist (Python)
2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/
<p style="color: grey">
Python
recipe 576930
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>).
Revision 10.
</p>
<p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</p>
Calculating the distance between zip codes (Python)
2006-04-25T20:40:00-07:00Kevin Ryanhttp://code.activestate.com/recipes/users/1654599/http://code.activestate.com/recipes/393241-calculating-the-distance-between-zip-codes/
<p style="color: grey">
Python
recipe 393241
by <a href="/recipes/users/1654599/">Kevin Ryan</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>I came across the mention of a formula labeled "The Great Circle Distance Formula" that purported to calculate the distance between any two points on the earth given their longitude and latitude points (the reference was in a Linux Magazine article). So, I looked up some information and cooked up a Python version of the calculation. There are references in the code where you can obtain approximate zip code data for free (e.g., if you wanted to enhance your website by adding a "Search within x mi's" feature), as well as references to the GCDF if you have further interest. Enjoy!</p>
<p>04/25/2006 update: I've decided to update this recipe with an object oriented bent where the information is cached once the object is instantiated. I've also added command line access to automatically download the zipcode file from the census website (just use 'python zips.py -d' and it will download a copy to your harddrive under 'zips.txt'). Lastly, I've added some unit testing so that if any future changes are made this will automatically run and tell me if anything pops out as wrong.</p>
Simple Back-propagation Neural Network in Python source code (Python)
2012-05-30T17:09:49-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578148-simple-back-propagation-neural-network-in-python-s/
<p style="color: grey">
Python
recipe 578148
by <a href="/recipes/users/4182015/">David Adler</a>
(<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/back_propagation/">back_propagation</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_network/">neural_network</a>, <a href="/recipes/tags/propagation/">propagation</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is a slightly different version of this <a href="http://arctrix.com/nas/python/bpnn.py" rel="nofollow">http://arctrix.com/nas/python/bpnn.py</a></p>
Genetic Algorithm in Python source code - AI-Junkie tutorial (Python)
2012-06-19T12:59:13-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578128-genetic-algorithm-in-python-source-code-ai-junkie-/
<p style="color: grey">
Python
recipe 578128
by <a href="/recipes/users/4182015/">David Adler</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/artificial/">artificial</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/python/">python</a>).
Revision 5.
</p>
<p>A simple genetic algorithm program. I followed this tutorial to make the program <a href="http://www.ai-junkie.com/ga/intro/gat1.html." rel="nofollow">http://www.ai-junkie.com/ga/intro/gat1.html.</a></p>
<p>The objective of the code is to evolve a mathematical expression which calculates a user-defined target integer.</p>
<hr />
<p>KEY:</p>
<p>chromosome = binary list (this is translated/decoded into a protein in the format number --> operator --> number etc, any genes (chromosome is read in blocks of four) which do not conform to this are ignored.</p>
<p>protein = mathematical expression (this is evaluated from left to right in number + operator blocks of two)</p>
<p>output = output of protein (mathematical expression)</p>
<p>error = inverse of difference between output and target</p>
<p>fitness score = a fraction of sum of of total errors</p>
<hr />
<p>OTHER:</p>
<p>One-point crossover is used.</p>
<p>I have incorporated <strong>elitism</strong> in my code, which somewhat deviates from the tutorial but made my code more efficient (top ~7% of population are carried through to next generation)</p>
Analog Clock (Python)
2014-05-14T20:53:39-07:00Paulo Cavalcantihttp://code.activestate.com/recipes/users/4189949/http://code.activestate.com/recipes/578875-analog-clock/
<p style="color: grey">
Python
recipe 578875
by <a href="/recipes/users/4189949/">Paulo Cavalcanti</a>
(<a href="/recipes/tags/clock/">clock</a>).
Revision 3.
</p>
<p>A python-tkinter analog clock</p>
Comparing two images (Python)
2011-04-03T11:13:24-07:00Charlie Clarkhttp://code.activestate.com/recipes/users/4171013/http://code.activestate.com/recipes/577630-comparing-two-images/
<p style="color: grey">
Python
recipe 577630
by <a href="/recipes/users/4171013/">Charlie Clark</a>
(<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/images/">images</a>).
</p>
<p>Compare two images using the root mean squared analysis. A result close to 0 means a good match.</p>
Finding the percentile of the values (Python)
2011-04-25T03:41:08-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/511478-finding-the-percentile-of-the-values/
<p style="color: grey">
Python
recipe 511478
by <a href="/recipes/users/2382677/">Wai Yip Tung</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This function find the percentile of a list of values. Note that the list must be sorted already.</p>
Python RGB and HSV Conversion (Python)
2009-10-01T06:33:56-07:00Michael Foglemanhttp://code.activestate.com/recipes/users/4171845/http://code.activestate.com/recipes/576919-python-rgb-and-hsv-conversion/
<p style="color: grey">
Python
recipe 576919
by <a href="/recipes/users/4171845/">Michael Fogleman</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/hsv/">hsv</a>, <a href="/recipes/tags/rgb/">rgb</a>).
</p>
<p>Python code to convert RGB to HSV and vice-versa.</p>
Python reader + writer for spss sav files (Linux, Mac & Windows) (Python)
2013-02-20T22:07:27-08:00Albert-Jan Roskamhttp://code.activestate.com/recipes/users/4177640/http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/
<p style="color: grey">
Python
recipe 577811
by <a href="/recipes/users/4177640/">Albert-Jan Roskam</a>
(<a href="/recipes/tags/reader/">reader</a>, <a href="/recipes/tags/sav/">sav</a>, <a href="/recipes/tags/spss/">spss</a>, <a href="/recipes/tags/writer/">writer</a>).
Revision 12.
</p>
<p><strong>Python Program to READ & WRITE Spss system files (.sav) (Linux,Mac or Windows)</strong></p>
<ul>
<li><em>Check <a href="https://pypi.python.org/pypi/savReaderWriter/" rel="nofollow">https://pypi.python.org/pypi/savReaderWriter/</a> for the latest version (including the libraries!)</em></li>
<li>Requires libspssdio.so.1 (LINUX) or libspssdio.dylib (MAC) or spssio32.dll (WINDOWS) plus associated libaries, which can be freely downloaded from:
<a href="https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%2520for%2520IBM%25C2%25AE%2520SPSS%25C2%25AE%2520Statistics?lang=en" rel="nofollow">https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%20for%20IBM%C2%AE%20SPSS%C2%AE%20Statistics?lang=en</a></li>
<li>It is recommended to download the v21 I/O files (required for writing zlib (.zsav) compressed files)</li>
<li>December 2012 (complete rewrite):</li>
<li>Added support for slicing, indexing, array slicing + other special methods</li>
<li>Added support for writing spss date fields</li>
<li>Added support for almost all meta data (missing values, sets, roles, etc.)</li>
<li>Added support for 64 bit Windows (tested with Win7) and other OSs
(z/Linux, Solaris, HP Linux, IBM AIX (untested though)</li>
<li>Added support for reading and writing zlib compressed (.zsav) files</li>
<li>Removed pesky segfault error when freeing memory</li>
<li>Removed errors related to encoding</li>
<li>Changed some Reader defaults (verbose=False, returnHeader=False)</li>
<li>Renamed SavDataDictionaryReader into SavHeaderReader</li>
</ul>
<p><strong>LINUX:</strong></p>
<p><em>Installation (tested on Linux Ubuntu 10):</em></p>
<ul>
<li>additional packages/files needed are: intel-icc8-libs_8.0-1_i386.deb,libicu32_3.2-3_i386.deb, libstdc++5_3.3.6-20_i386.deb, libirc.so.</li>
<li>Run the following commands in your terminal: sudo apt-get install intel-icc8-libs; sudo apt-get install libicu32; sudo apt-get install libstdc++5.</li>
<li>Then convert libirc.a (static) to libirc.so (dynamic), save in same location as libspssdio.so.1:
ar vx intel-icc8-libs_8.0-1_i386.deb; tar -xzvf data.tar.gz ./usr/lib/libirc.a; ar -x libirc.a.</li>
</ul>
<p><em>Calling the program:</em></p>
<ul>
<li>In the TERMINAL type: export LD_LIBRARY_PATH=/path/of/additional/sofiles; python /some/path/wrapperToProgram.py. You may also add ld_library_path to .bashrc</li>
<li>The wrapper starts with "from SavReaderWriter import *", followed by e.g. stuff from the if __name__ == '__main__' section</li>
</ul>
<p><strong>MAC OS:</strong></p>
<ul>
<li>you must put all the dylib files that come with the IBM
SPSS_Statistics_InputOutput_Modules_* package in the macos
directory somewhere that OS X can find them</li>
<li>one simple way to accomplish this is to copy them to /usr/lib</li>
</ul>
<p><strong>WINDOWS:</strong></p>
<ul>
<li>You can also find this dll in the installation directory of SPSS (although SPSS is _not_ needed!)</li>
<li>The .dll should be saved in the same location as this program.</li>
</ul>
<p><strong>USAGE:</strong>
See docstrings + __main__ section</p>
Property decorator for python 2.4 (Python)
2005-04-25T23:32:11-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/410698-property-decorator-for-python-24/
<p style="color: grey">
Python
recipe 410698
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>This recipe refines an older recipe on creating class properties (
<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183</a>). The refinement consists of:
- Using a decorator (introduced in python 2.4) to "declare" a function in class scope as property.
- Using a trace function to capture the locals() of the decorated function, instead of requiring the latter to return locals(), as in the older recipe.</p>
tkMath, convert between pixels, inches, cm and mm (Python)
2008-04-12T02:25:41-07:00Ronald Longohttp://code.activestate.com/recipes/users/4093058/http://code.activestate.com/recipes/552745-tkmath-convert-between-pixels-inches-cm-and-mm/
<p style="color: grey">
Python
recipe 552745
by <a href="/recipes/users/4093058/">Ronald Longo</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 6.
</p>
<p>If you're writing some Tkinter software and you're sizing something in inches or centimeters and tkinter only gives you feedback in pixel distances then you may need a way to get back to your prefered units of measure. I recently found myself in this situation. There's nothing fancy about the set of functions here, it's more about the little known winfo_fpixels() function. Once you know about this everything else is a piece of cake. These functions are simple, but convenient.</p>
Use wxPython/OpenGL to draw a sphere (Python)
2004-10-27T12:53:47-07:00Rick Mullerhttp://code.activestate.com/recipes/users/1461790/http://code.activestate.com/recipes/325392-use-wxpythonopengl-to-draw-a-sphere/
<p style="color: grey">
Python
recipe 325392
by <a href="/recipes/users/1461790/">Rick Muller</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>A simple wxPython/OpenGL application to draw a sphere.</p>
Numerical Integration using Monte Carlo method (Python)
2010-06-16T19:31:17-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577263-numerical-integration-using-monte-carlo-method/
<p style="color: grey">
Python
recipe 577263
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>).
Revision 2.
</p>
<p>Numerical Integration using Monte Carlo method.</p>
Cache decorator in python 2.4 (Python)
2004-11-01T19:11:21-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/
<p style="color: grey">
Python
recipe 325205
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p>
Table indentation (Python)
2004-02-14T03:50:24-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/267662-table-indentation/
<p style="color: grey">
Python
recipe 267662
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 7.
</p>
<p>A function for pretty-printing a table.</p>
Covert color space from HSV to RGB and RGB to HSV (Python)
2008-11-04T02:13:01-08:00Victor Linhttp://code.activestate.com/recipes/users/4167779/http://code.activestate.com/recipes/576554-covert-color-space-from-hsv-to-rgb-and-rgb-to-hsv/
<p style="color: grey">
Python
recipe 576554
by <a href="/recipes/users/4167779/">Victor Lin</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/color_space/">color_space</a>, <a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/graph/">graph</a>).
</p>
<p>Functions for converting bwtween RGB and HSV color space.</p>