Popular recipes tagged "sequence"http://code.activestate.com/recipes/tags/sequence/2017-01-08T17:48:57-08:00ActiveState Code RecipesA utility like Unix seq (command-line), in Python (Python)
2017-01-08T17:48:57-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580744-a-utility-like-unix-seq-command-line-in-python/
<p style="color: grey">
Python
recipe 580744
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/seq/">seq</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>).
</p>
<p>This recipe shows how to create a utility like Unix seq (command-line), in Python.
seq is described here: </p>
<p><a href="https://en.wikipedia.org/wiki/Seq_%28Unix%29" rel="nofollow">https://en.wikipedia.org/wiki/Seq_(Unix)</a></p>
<p>but briefly, it is a command-line utility that takes 1 to 3 arguments (some being optional), the start, stop and step, and prints numbers from the start value to the stop value, on standard output. So seq has many uses in bigger commands or scripts; a common category of use is to quickly generate multiple filenames or other strings that contain numbers in them, for exhaustive testing, load testing or other purposes. A similar command called jot is found on some Unix systems.</p>
<p>This recipe does not try to be exactly the same in functionality as seq. It has some differences. However the core functionality of generating integer sequences is the same (but without steps other than 1 for the range).</p>
<p>More details and sample output are here:</p>
<p><a href="https://jugad2.blogspot.in/2017/01/an-unix-seq-like-utility-in-python.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/an-unix-seq-like-utility-in-python.html</a></p>
<p>The code is below.</p>
Shortest Common Supersequence algorithms (Python)
2013-10-02T12:52:23-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578678-shortest-common-supersequence-algorithms/
<p style="color: grey">
Python
recipe 578678
by <a href="/recipes/users/4187940/">Rutger Saalmink</a>
(<a href="/recipes/tags/approximation/">approximation</a>, <a href="/recipes/tags/bound/">bound</a>, <a href="/recipes/tags/breadth_first_search/">breadth_first_search</a>, <a href="/recipes/tags/common/">common</a>, <a href="/recipes/tags/depth_first_search/">depth_first_search</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/shortest/">shortest</a>, <a href="/recipes/tags/super/">super</a>).
</p>
<p>The Shortest Common Supersequence (SCS) problem is an NP-hard problem (<a href="https://en.wikipedia.org/wiki/Shortest_common_supersequence" rel="nofollow">https://en.wikipedia.org/wiki/Shortest_common_supersequence</a>), which occurs in problems originating from various domains, e.g. Bio Genetics. Given a set of strings, the common supersequence of minimal length is sought after. Below a set of algorithms is given, which I used in approximating and/or backtracking the optimal solution(s). </p>
Split a sequence or generator using a predicate (Python)
2013-01-21T16:20:17-08:00Paul McGuirehttp://code.activestate.com/recipes/users/1377254/http://code.activestate.com/recipes/578416-split-a-sequence-or-generator-using-a-predicate/
<p style="color: grey">
Python
recipe 578416
by <a href="/recipes/users/1377254/">Paul McGuire</a>
(<a href="/recipes/tags/partition/">partition</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/splitting/">splitting</a>).
Revision 3.
</p>
<p>Split a sequence or generator into two iterators, each iterating over the elements that either pass or fail a predicate function.</p>
reshape a sequence (Python)
2012-11-02T04:01:06-07:00Chris Smithhttp://code.activestate.com/recipes/users/2725752/http://code.activestate.com/recipes/578262-reshape-a-sequence/
<p style="color: grey">
Python
recipe 578262
by <a href="/recipes/users/2725752/">Chris Smith</a>
(<a href="/recipes/tags/group/">group</a>, <a href="/recipes/tags/reshape/">reshape</a>, <a href="/recipes/tags/sequence/">sequence</a>).
Revision 4.
</p>
<p>This function accepts a sequence and a template of how the sequence should be reshaped. </p>
Sequence Builder (Python)
2012-03-03T17:50:03-08:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578061-sequence-builder/
<p style="color: grey">
Python
recipe 578061
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/builder/">builder</a>, <a href="/recipes/tags/sequence/">sequence</a>).
Revision 2.
</p>
<p><strong>Why?</strong></p>
<ul>
<li>Thinking about a sequence of odd numbers where numbers divisible by 3 are not part of it I came to a sequence starting with 5, 7, 11, 13, 17, 19, ...</li>
<li>The interesting property of this sequence is that the sequence of differences between the individual elements are 2,4,2,4,2,...</li>
</ul>
<p><strong>How?</strong></p>
<ul>
<li>I'm not a math expert (unfortunately) but I could imagine that there is a quite simple formula to get this.</li>
<li>However I thought that using different combinations of defined functions a script could do the favour for me.</li>
</ul>
<p><strong>Result?</strong></p>
<ul>
<li>Formula: <code>(((-1)**(x-1))+1)+2</code> -> Simplified -> <code>-1**(x-1) + 3</code> (now clear to you?)</li>
<li>You have to look for the sequence [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]</li>
</ul>
<p><strong>Out of scope:</strong></p>
<ul>
<li>Does not check for using lambda functions only and "x" only in the lambda functions.</li>
<li>The Python script does not simplify functions like changing ((x+1)+1) to x+2 - would be an interesting recipe - by the way :)</li>
</ul>
<p><strong>Please note</strong></p>
<ul>
<li>You should not add other functions than lambda.</li>
<li>You should always use "x" in your formular.</li>
<li>Be aware that the actual setup runs a few minutes (about 3 minutes) and you can imagine - surely - that adding further functions will definitely increase the runtime.</li>
</ul>
<p><strong>Side effects?</strong></p>
<ul>
<li>Quite funny to produce a sequence of nines with <code>(((-1)**(2*x))+2)**2</code>.</li>
<li>If you apply the first binomial theorem (a+b)^2 = a^2 + 2ab + b^2 then you see why!</li>
</ul>
<p><strong>What I have learned from this?</strong></p>
<ul>
<li>The biggest nightmare I did have with the Sequence class because of not knowing how to generate unique elements of this in a set (__eq__ and __hash__ are required); and I have to ensure that the hash code is calculated once only.</li>
<li>The 'combineFunctions' was interesting to me. I have never used 'inspect.getsource' before.</li>
<li>A few minutes does not sound much but for developing all times with more than 30 seconds are not comfortable. There are just 325 sequences and to investigate for certain sequences you probably have to have some more formula. Maybe I would have to take another approach for that.</li>
</ul>
Search sequences for sub-sequence (Python)
2011-08-19T05:17:00-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577850-search-sequences-for-sub-sequence/
<p style="color: grey">
Python
recipe 577850
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/searching/">searching</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/sublist/">sublist</a>, <a href="/recipes/tags/substring/">substring</a>).
</p>
<p>The list and tuple index() method and <code>in</code> operator test for element containment, unlike similar tests for strings, which checks for sub-strings:</p>
<pre class="prettyprint"><code>>>> "12" in "0123"
True
>>> [1, 2] in [0, 1, 2, 3]
False
</code></pre>
<p>These two functions, search and rsearch, act like str.find() except they operate on any arbitrary sequence such as lists:</p>
<pre class="prettyprint"><code>>>> search([1, "a", "b", 2, 3], ["b", 2])
2
</code></pre>
Partition a sequence (Python)
2011-05-03T17:52:47-07:00Anoop.Khttp://code.activestate.com/recipes/users/4177869/http://code.activestate.com/recipes/577682-partition-a-sequence/
<p style="color: grey">
Python
recipe 577682
by <a href="/recipes/users/4177869/">Anoop.K</a>
(<a href="/recipes/tags/sequence/">sequence</a>).
</p>
<p>Using simple recursive logic.</p>
Fast flatten() with depth control and oversight over which subtrees to expand (Python)
2010-11-26T11:10:01-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577470-fast-flatten-with-depth-control-and-oversight-over/
<p style="color: grey">
Python
recipe 577470
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/tuple/">tuple</a>).
</p>
<p>Extremely fast, non-recursive, depth limited flatten with powerful control over which subtrees are to be expanded. If this is what you need then look no further.</p>
Named Sequences for environments containing large numbers of POD instances (Python)
2010-11-27T13:55:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577473-named-sequences-for-environments-containing-large-/
<p style="color: grey">
Python
recipe 577473
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/unittests/">unittests</a>).
</p>
<p>Generate classes with named data attributes that can be sequenced.
Useful for POD classes for which many records will exist concurrently.</p>
<p>Compare the feature set to NamedTuples by Raymond Hettinger:
<a href="http://code.activestate.com/recipes/500261-named-tuples/" rel="nofollow">http://code.activestate.com/recipes/500261-named-tuples/</a></p>
Weighted random choice (Python)
2010-08-19T08:40:38-07:00Carlos Valientehttp://code.activestate.com/recipes/users/4174637/http://code.activestate.com/recipes/577363-weighted-random-choice/
<p style="color: grey">
Python
recipe 577363
by <a href="/recipes/users/4174637/">Carlos Valiente</a>
(<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sequence/">sequence</a>).
</p>
<p>This function returns a random element from a sequence. The probability for each element in the sequence to be selected can be weighted by a user-provided callable</p>
Regular Expression for generic sequences of symbols (Python)
2009-06-13T09:51:38-07:00Emanuele Ruffaldihttp://code.activestate.com/recipes/users/4170726/http://code.activestate.com/recipes/576806-regular-expression-for-generic-sequences-of-symbol/
<p style="color: grey">
Python
recipe 576806
by <a href="/recipes/users/4170726/">Emanuele Ruffaldi</a>
(<a href="/recipes/tags/adt/">adt</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/sequence/">sequence</a>).
Revision 2.
</p>
<p>Python regular expression are very powerful and efficient and they can be applied to the recognition of different types of sequences. This recipe shows how to match sequences of generic symbol set with the power of regular expression. The code uses a mapping from every entity into a character. The mapping is used both at level of sequence and in the compilation of the regular expression. When the symbol set is small it is possible to efficiently use 8 bit strings instead of full unicode.</p>
Base Conversion decimal to base = len(map) (Python)
2008-08-20T05:01:39-07:00Mark Zitnikhttp://code.activestate.com/recipes/users/4166559/http://code.activestate.com/recipes/576435-base-conversion-decimal-to-base-lenmap/
<p style="color: grey">
Python
recipe 576435
by <a href="/recipes/users/4166559/">Mark Zitnik</a>
(<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/web_site/">web_site</a>).
Revision 7.
</p>
<p>This code enable decimal base conversion according map length and a different char set.</p>
<p>Example:</p>
<ul>
<li>map = ['0','1'] base 2 10 -> 1010</li>
<li>map = ['a','b'] base 2 10 -> baba</li>
<li>map = ['a','b','c','d','e','f','g','h','i','j','k','l'] base 12 10 -> k</li>
<li>map = ['a','b','c','d','e','f','g','h','i','j','k','l'] base 12 100 -> ie</li>
</ul>
<p>this simple method can be used in web sites to hide a well known decimal sequence like user ids.</p>