Popular recipes tagged "meta:loc=17"http://code.activestate.com/recipes/tags/meta:loc=17/2016-02-21T06:00:46-08:00ActiveState Code RecipesGet the inversion number of a permutation (Python)
2016-02-21T06:00:46-08:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/579051-get-the-inversion-number-of-a-permutation/
<p style="color: grey">
Python
recipe 579051
by <a href="/recipes/users/4187478/">Samuel James Erickson</a>
(<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/discrete/">discrete</a>, <a href="/recipes/tags/inversion/">inversion</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/permutations/">permutations</a>).
Revision 2.
</p>
<p>This function takes a permutation in the form of a list and returns the number of inversions in the permutation. </p>
Image Converter (Python)
2015-01-18T19:14:38-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579007-image-converter/
<p style="color: grey">
Python
recipe 579007
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Converts an image from one format to another using PIL.</p>
eMail address list validation (JavaScript)
2014-02-05T22:21:03-08:00SoftMoon WebWarehttp://code.activestate.com/recipes/users/4189155/http://code.activestate.com/recipes/578823-email-address-list-validation/
<p style="color: grey">
JavaScript
recipe 578823
by <a href="/recipes/users/4189155/">SoftMoon WebWare</a>
.
Revision 0.
</p>
<p>A bit of Regular Expressions to test whether a given string conforms to being an eMail address or comma-separated list of them.</p>
eMail address list validation (JavaScript)
2014-02-05T22:20:41-08:00SoftMoon WebWarehttp://code.activestate.com/recipes/users/4189155/http://code.activestate.com/recipes/578822-email-address-list-validation/
<p style="color: grey">
JavaScript
recipe 578822
by <a href="/recipes/users/4189155/">SoftMoon WebWare</a>
.
Revision 0.
</p>
<p>A bit of Regular Expressions to test whether a given string conforms to being an eMail address or comma-separated list of them.</p>
Holiday Tree (Python)
2013-12-02T09:50:55-08:00Bill Picketthttp://code.activestate.com/recipes/users/4174028/http://code.activestate.com/recipes/578778-holiday-tree/
<p style="color: grey">
Python
recipe 578778
by <a href="/recipes/users/4174028/">Bill Pickett</a>
(<a href="/recipes/tags/exercise/">exercise</a>).
Revision 2.
</p>
<p>Just a little holiday tree, would make a good exercise for beginning Python programmers.</p>
heap class (Python)
2015-09-17T12:41:15-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578694-heap-class/
<p style="color: grey">
Python
recipe 578694
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/heap/">heap</a>).
Revision 2.
</p>
<p><em>heapq</em> is a nice python module, but the interface is not so clean. I found a one-liner on this <a href="http://metapython.blogspot.de/2010/10/creating-heap-class-in-one-python-line.html">blog</a>, it's as short as possible, but not really pythonic either :) Here is my contribution to a most readable heap class.</p>
find + grep (Bash)
2013-09-17T08:48:07-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578661-find-grep/
<p style="color: grey">
Bash
recipe 578661
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/zsh/">zsh</a>).
Revision 2.
</p>
<p>look for a text pattern in files defined by a pattern.
First argument is passed to the find command, second one to the grep</p>
<p>./find_n_grep.sh '*.py' 'dict'</p>
<p>more over, it sort by date.</p>
Mounted devices2 (Batch)
2013-08-19T08:46:54-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578645-mounted-devices2/
<p style="color: grey">
Batch
recipe 578645
by <a href="/recipes/users/4184115/">greg zakharov</a>
.
</p>
<p>Another way to check mounted drives (this method has a bug with cdrom).</p>
A simple KD Tree example with custom Euclidean distance ball query. (Python)
2013-01-24T10:28:24-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578434-a-simple-kd-tree-example-with-custom-euclidean-dis/
<p style="color: grey">
Python
recipe 578434
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.</p>
<p><a href="http://en.wikipedia.org/wiki/K-d_tree" rel="nofollow">http://en.wikipedia.org/wiki/K-d_tree</a></p>
Reversing string (JavaScript)
2013-02-18T09:27:33-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578464-reversing-string/
<p style="color: grey">
JavaScript
recipe 578464
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/reverse/">reverse</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Two ways reverse string.</p>
Greatest Common Divisor using Recursion in Python (Python)
2013-01-16T16:44:46-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578421-greatest-common-divisor-using-recursion-in-python/
<p style="color: grey">
Python
recipe 578421
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/gcd/">gcd</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>I wrote this little piece of code as part of my ongoing python-math projects. For more information please checkout <a href="http://thelivingpearl.com">The Living Pearl</a>. </p>
Detect screen resolution without wmi (part 2) (Batch)
2012-12-12T12:40:24-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578377-detect-screen-resolution-without-wmi-part-2/
<p style="color: grey">
Batch
recipe 578377
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/resolution/">resolution</a>, <a href="/recipes/tags/screen/">screen</a>).
</p>
<p>In previous time I wrote how to do it with <a href="http://code.activestate.com/recipes/578345-detect-screen-resolution-without-wmi/?in=user-4184115">mshta.exe</a>. Good, now I want to show you how to do it with COM-object <strong>htmlfile</strong>. It looks like similar way with mshta.exe:</p>
String to Binary (Python)
2014-07-30T07:20:22-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578291-string-to-binary/
<p style="color: grey">
Python
recipe 578291
by <a href="/recipes/users/4183895/">p@ntut$</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/to/">to</a>).
Revision 2.
</p>
<p>String to binary snippet, python3+</p>
Simple directory tree view generator (Python)
2012-03-09T09:02:21-08:00ajaymenon.khttp://code.activestate.com/recipes/users/4181225/http://code.activestate.com/recipes/578065-simple-directory-tree-view-generator/
<p style="color: grey">
Python
recipe 578065
by <a href="/recipes/users/4181225/">ajaymenon.k</a>
(<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/tree/">tree</a>).
</p>
<p>An extremely simple tree-view generator for a directory</p>
(Ab)Using Modules as Namespaces (Python)
2012-05-10T04:14:06-07:00Wolfgang Schererhttp://code.activestate.com/recipes/users/4182020/http://code.activestate.com/recipes/578122-abusing-modules-as-namespaces/
<p style="color: grey">
Python
recipe 578122
by <a href="/recipes/users/4182020/">Wolfgang Scherer</a>
(<a href="/recipes/tags/module/">module</a>, <a href="/recipes/tags/namespaces/">namespaces</a>, <a href="/recipes/tags/vars/">vars</a>).
</p>
<p>I have previously built a shar-like Python utility, which reads (marked) imported modules, gzips and base64 encodes them, then generates a python script, which is fully standalone.</p>
<p>The included module source is placed into sys.modules at runtime, making imports possible without actually having the module files installed. A very nice thing for administrative scripts that have to work in unconfigured environments.</p>
<p>At that time I discovered a lot of information about how modules in Python work.</p>
<p>Reading the very interesting recipe at <a href="http://code.activestate.com/recipes/577887-a-simple-namespace-class/" rel="nofollow">http://code.activestate.com/recipes/577887-a-simple-namespace-class/</a>
made me realize, that modules are actually very useful as generic namespaces too.</p>
Fast Sorting of n dimensional array by first dimension (Python)
2012-03-14T15:14:11-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578074-fast-sorting-of-n-dimensional-array-by-first-dimen/
<p style="color: grey">
Python
recipe 578074
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/argsort/">argsort</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sorting/">sorting</a>).
</p>
<p>I have looked far and wide for code for fast sorting of n dimensional arrays by the first element, for example if I had the array: <br />
ray = [[1,2,3,7,5][10,11,12,13,14]] </p>
<p>I would want it to come out as
ray = [[1,2,3,5,7][10,11,12,14,13]]</p>
<p>There are several ways to do this. One is
zipped = zip(<em>ray)
zipped.sort()
ray = zip(</em>zipped)</p>
<p>but this is extremely slow. Numpy has a much faster way to do it, but it wasn't immediately apparent.</p>
<p>if the above were a numpy array you could simply do the following:
indexes = numpy.argsort(ray[0])
for n in xrange(len(ray))
ray[n] = ray[n][indexes]</p>
<p>I did a time test of the two methods below.</p>
format_iter: easy formatting of arbitrary iterables (Python)
2011-08-16T11:44:59-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577845-format_iter-easy-formatting-of-arbitrary-iterables/
<p style="color: grey">
Python
recipe 577845
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/strings/">strings</a>).
</p>
<p>The <code>format_iter</code> recipe defines a simple wrapper around <code>str.join</code> and <code>str.format</code> that makes it easy to format an arbitrary iterable with a specified format string and item separator.</p>
Minimal console python (Python)
2011-03-23T20:17:53-07:00jrovegnohttp://code.activestate.com/recipes/users/4170207/http://code.activestate.com/recipes/577622-minimal-console-python/
<p style="color: grey">
Python
recipe 577622
by <a href="/recipes/users/4170207/">jrovegno</a>
.
</p>
<p>Interpretador de linea de comandos python, sólo para uso didáctico, no debe utilizarse en producción.
Referencia
<a href="http://www.programando.org/blog/2011/03/repl/" rel="nofollow">http://www.programando.org/blog/2011/03/repl/</a></p>
Pascal's triangle (Python)
2011-01-14T00:29:05-08:00Michael Pucketthttp://code.activestate.com/recipes/users/4176295/http://code.activestate.com/recipes/577542-pascals-triangle/
<p style="color: grey">
Python
recipe 577542
by <a href="/recipes/users/4176295/">Michael Puckett</a>
(<a href="/recipes/tags/pascal/">pascal</a>, <a href="/recipes/tags/triangle/">triangle</a>).
Revision 5.
</p>
<p>Pascal's triangle was a challenge on a forum I frequent today and this is what I came up with:</p>
External Counter (Python)
2010-11-07T03:25:34-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577456-external-counter/
<p style="color: grey">
Python
recipe 577456
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/counter/">counter</a>).
</p>
<p>Written as a tribute to <a href="http://code.activestate.com/recipes/440636/">recipe 440636</a>, this newest version is considerably reworked.</p>