Popular recipes by Paddy McCarthy http://code.activestate.com/recipes/users/398009/popular/2017-07-17T05:53:45-07:00ActiveState Code RecipesShoelace Formula for polygonal area (Python)
2017-07-17T05:53:45-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/580812-shoelace-formula-for-polygonal-area/
<p style="color: grey">
Python
recipe 580812
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/2d/">2d</a>, <a href="/recipes/tags/area/">area</a>).
</p>
<p>Copied, by author from "Paddy3118 Go deh!: Python investigation of the Shoelace Formula for polygonal area <a href="http://paddy3118.blogspot.com/2017/07/python-investigation-of-shoelace.html#ixzz4n43Dqhaa" rel="nofollow">http://paddy3118.blogspot.com/2017/07/python-investigation-of-shoelace.html#ixzz4n43Dqhaa</a> " Where there is more meat on the bone (under a different license though).</p>
Generate the parity or sign of a permutation (Python)
2012-07-28T07:21:49-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578227-generate-the-parity-or-sign-of-a-permutation/
<p style="color: grey">
Python
recipe 578227
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>).
</p>
<p>The <a href="http://en.wikipedia.org/wiki/Parity_of_a_permutation">parity</a> of a given permutation is whether an odd or even number of swaps between any two elements are needed to transform the given permutation to the first permutation.</p>
<p>The sign of the permutation is +1 for an even parity and -1 for an odd parity.</p>
<p>This python function returns the sign of a permutation of all the integers 0..N.
When the program is run it shows the sign of permutations as generated by the standard function itertools.permutations.</p>
<p>The function uses a modified <a href="http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort#Python">selection sort</a> to compute the parity.</p>
Alternatve generation of the parity or sign of a permutation (Python)
2012-08-07T08:45:23-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578236-alternatve-generation-of-the-parity-or-sign-of-a-p/
<p style="color: grey">
Python
recipe 578236
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>).
</p>
<p>Although <a href="http://code.activestate.com/recipes/578227/">Recipe 578227</a> was straight-forward to derive, I came across <a href="http://stackoverflow.com/questions/337664/counting-inversions-in-an-array/6424847#6424847">this Stack Overflow answer</a> that gives another method for generating the sign based on comparing the perm and the sorted perm.</p>
Topological Sort (Python)
2010-09-28T19:22:27-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/577413-topological-sort/
<p style="color: grey">
Python
recipe 577413
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/topological/">topological</a>).
</p>
<p>Given items that depend on other items, a topological sort arranges items in order that no one item precedes an item it depends on.
In this example items are strings and dependencies are expressed in a dictionary whose keys are items and whose values are a set of dependent items. Note that the dict may contain self-dependencies (which are ignored), and dependent items that are not also dict keys, such as the item 'ieee'.</p>
McCarthy's amb operator in Python (Python)
2010-08-23T22:41:53-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/577368-mccarthys-amb-operator-in-python/
<p style="color: grey">
Python
recipe 577368
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/declarative/">declarative</a>, <a href="/recipes/tags/style/">style</a>).
</p>
<p>From reading about amb it seems that it allows for a declaritive style of programming where the one amb "operator" can
* Set the ranges of variables
* Set a constraint function
* And iterate over all solutions </p>
<p>(See <a href="http://paddy3118.blogspot.com/2010/08/mccarthys-amb-operator-in-python.html" rel="nofollow">http://paddy3118.blogspot.com/2010/08/mccarthys-amb-operator-in-python.html</a> for a more descriptive intro).</p>
Extension to Python 3 Counter class (Python)
2010-08-18T21:02:41-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/
<p style="color: grey">
Python
recipe 577362
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/bag/">bag</a>, <a href="/recipes/tags/counter/">counter</a>, <a href="/recipes/tags/multiset/">multiset</a>, <a href="/recipes/tags/python/">python</a>).
Revision 3.
</p>
<p>Adds symmetric difference and cartesian product to the Counter class. I had a use case for the former and Raymond H. asked about the latter so I coded the latter from its Wikipedia description.</p>
Luhn test for credit card numbers (Python)
2010-03-01T22:19:44-08:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/577078-luhn-test-for-credit-card-numbers/
<p style="color: grey">
Python
recipe 577078
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Implements the <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Luhn test</a> succinctly.</p>
SEDOL code validator and checksum generator (Python)
2008-08-03T01:00:51-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/576405-sedol-code-validator-and-checksum-generator/
<p style="color: grey">
Python
recipe 576405
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/security/">security</a>).
</p>
<p>Will check <a href="http://www.londonstockexchange.com/NR/rdonlyres/52511C77-4BF3-43E5-A4AF-F86C871EA7C1/0/SMFTechnicalSpecificationV6Clean.doc">SEDOL numbers</a> as well as generate a SEDOL checksum digit.</p>
Another generator for an arbitrary number of 'for' loops (Python)
2007-01-31T13:31:26-08:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/502199-another-generator-for-an-arbitrary-number-of-for-l/
<p style="color: grey">
Python
recipe 502199
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Saw <a href="http://code.activestate.com/recipes/502194/">recipe 502194</a> and wondered if I could do similar.
I initially came up with function comb that enumerates all combinations. I wanted a generator however and so abandoned that approach for comb2 which is a little more complex.</p>
Optimising Regression Testing on COmpute Farms (Python)
2004-04-25T18:53:23-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/280030-optimising-regression-testing-on-compute-farms/
<p style="color: grey">
Python
recipe 280030
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Collects statistics on different algorithms for grouping thousands of tests for execution on a compute farm where you are limited to much less job slots, (tens).</p>