Popular recipes tagged "range"http://code.activestate.com/recipes/tags/range/2015-03-05T15:59:30-08:00ActiveState Code RecipesEqually-spaced numbers (linspace) (Python) 2015-01-12T22:16:37-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/579000-equally-spaced-numbers-linspace/ <p style="color: grey"> Python recipe 579000 by <a href="/recipes/users/4184316/">Andrew Barnert</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/linspace/">linspace</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>An equivalent of <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html"><code>numpy.linspace</code></a>, but as a pure-Python lazy sequence.</p> <p>Like NumPy's <code>linspace</code>, but unlike the <a href="http://code.activestate.com/recipes/577068/"><code>spread</code></a> and <a href="http://code.activestate.com/recipes/577068/"><code>frange</code></a> recipes listed here, the <code>num</code> argument specifies the number of values, not the number of intervals, and the range is closed, not half-open.</p> <p>Although this is primarily designed for floats, it will work for <code>Fraction</code>, <code>Decimal</code>, NumPy arrays (although this would be silly) and even <code>datetime</code> values.</p> <p>This recipe can also serve as an example for creating lazy sequences.</p> <p>See the discussion below for caveats.</p> Simple way to find number of perfect square numbers in a range. (Python) 2015-03-05T15:59:30-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/579031-simple-way-to-find-number-of-perfect-square-number/ <p style="color: grey"> Python recipe 579031 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/complex/">complex</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/interview/">interview</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>The strategy here is not to iterate through the set of possible integer values and check for is_perfect_square() each time but to translate the upper and lower values to either complex or real space of square numbers.</p> <pre class="prettyprint"><code> # O(n) complexity len([x for x in range(0, 100) if x!= 0 and float(math.sqrt(x)).is_integer()]) so if a and b positive then we count the number of integer values between upper and lower sqrt() values if either a or b are negative then we need to use the complex number space for the sqrt() results. In this case we are still counting integer values either along the imaginary or real axis, the result is then a simple sum if both a and b are negative we need to make sure that when walking down the same imaginary axis we dont count the same inteters twice, in this case we can take the min(a,b) to get max distinct set of integer values. </code></pre> Numbers as Ranges - iterable integers (C++) 2014-10-02T12:13:52-07:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578945-numbers-as-ranges-iterable-integers/ <p style="color: grey"> C++ recipe 578945 by <a href="/recipes/users/4187847/">elazar</a> (<a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/range/">range</a>). Revision 2. </p> <p>This header file makes simple integers iterable. Note that it works only on C++11 or above.</p> <p>Usage:</p> <pre class="prettyprint"><code>#include "num_range.h" for (int i : 3) cout &lt;&lt; i &lt;&lt; endl; </code></pre> <p>Output: 0 1 2</p> <p>Implementation note: This code is far too generic. We only need <code>DerefableInt</code>. The templates are there for no practical reason.</p> <p>Cons: pollutes namespace std; nonstandard idiom;</p> Factorial (Python) 2014-03-08T13:09:41-08:00Johnhttp://code.activestate.com/recipes/users/4189390/http://code.activestate.com/recipes/578848-factorial/ <p style="color: grey"> Python recipe 578848 by <a href="/recipes/users/4189390/">John</a> (<a href="/recipes/tags/factorial/">factorial</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/loops/">loops</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/simple/">simple</a>). </p> <p>Just a simple factorial program I made in Python 3.</p> Wav audio file dynamic range compressor (C) 2013-04-18T15:04:25-07:00Granning Stolinehttp://code.activestate.com/recipes/users/4186069/http://code.activestate.com/recipes/578510-wav-audio-file-dynamic-range-compressor/ <p style="color: grey"> C recipe 578510 by <a href="/recipes/users/4186069/">Granning Stoline</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/compressor/">compressor</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/wav/">wav</a>). Revision 4. </p> <p>Wav audio file dynamic range compressor</p> Generate equally-spaced floats (Python) 2011-09-24T18:49:39-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577878-generate-equally-spaced-floats/ <p style="color: grey"> Python recipe 577878 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>Generating a list of equally-spaced floats can surprising due to floating point rounding. See, for example, the recipe for a <a href="http://code.activestate.com/recipes/577068">floating point range</a>. One way of avoiding some surprises is by changing the API: instead of specifying a start, stop and step values, instead use a start, stop and count:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(spread(0.0, 2.1, 7)) [0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8] </code></pre> <p>Like <a href="http://code.activestate.com/recipes/577068">frange</a> <code>spread</code> takes an optional mode argument to select whether the start and end values are included. By default, start is included and end is not, and exactly count values are returned.</p> Equally-spaced floats part 2 (Python) 2011-09-27T15:48:00-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577881-equally-spaced-floats-part-2/ <p style="color: grey"> Python recipe 577881 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>See the recipe <a href="http://code.activestate.com/recipes/577878">here</a> for a description of the problem.</p> <p>This variant allows the user to choose between two APIs, either by supplying the count, start value and end value:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(spread(5, 1.0, end=2.0)) [1.0, 1.2, 1.4, 1.6, 1.8] </code></pre> <p>or the count, start value and step size:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(spread(5, 1.0, step=-0.25)) [1.0, 0.75, 0.5, 0.25, 0.0] </code></pre> <p>As before, the count argument specifies how many subdivisions are made. The optional argument <code>mode</code> selects whether the start and end values are included. By default, start is included and end is not, and exactly count values are returned.</p> Special Range Function for Different Kinds of Ranges (int, float, character) (Python) 2011-03-30T16:42:47-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577583-special-range-function-for-different-kinds-of-rang/ <p style="color: grey"> Python recipe 577583 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/character/">character</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/special/">special</a>). Revision 3. </p> <p>This module allows the user to create a more verbose set of ranges. Simple character ranges, and float ranges are supported.</p> <p>Supported Ranges:</p> <ul> <li>Basic Integer Ranges</li> <li>Float Ranges (as accurate as a float range can get)</li> <li>Simple character ranges (lowercase to lowercase, uppercase to uppercase, etc.)</li> </ul> <p>It should work in Python 2 and Python 3.</p> <p><strong>If you tested this for speed, or want to test this for speed, please post the results! (And your system specs)</strong></p> <p><strong>Edit:</strong> Found a really silly error of mine when using range instead of xrange in these functions!</p> Generate List of Numbers from Hyphenated and comma separeted string like "1-5,25-30,4,5" (Python) 2010-07-01T02:08:09-07:00Siddhant Sanyamhttp://code.activestate.com/recipes/users/4174317/http://code.activestate.com/recipes/577279-generate-list-of-numbers-from-hyphenated-and-comma/ <p style="color: grey"> Python recipe 577279 by <a href="/recipes/users/4174317/">Siddhant Sanyam</a> (<a href="/recipes/tags/hypen/">hypen</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>This function takes a range in form of "a-b" and generate a list of numbers between a and b inclusive. Also accepts comma separated ranges like "a-b,c-d,f" will build a list which will include numbers from a to b, a to d and f Example: hyphen_range('54-78') hyphen_range('57-78,454,45,1-10') hyphen_range('94-100,1052,2-50')</p> Improved range function (Python) 2010-06-07T01:50:20-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577256-improved-range-function/ <p style="color: grey"> Python recipe 577256 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/replace/">replace</a>). Revision 2. </p> <p>While using the built-in range function a while ago. I found an odd (perhaps bug) where the range function couldn't use float steps. I am not sure if that was intended for simplicity or not, but I wrote my own range function that goes beyond that anyway.</p> Floating point range (Python) 2010-02-24T07:12:55-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577068-floating-point-range/ <p style="color: grey"> Python recipe 577068 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>Generator that produces floats, equivalent to range for integers, minimising rounding errors by using only a single multiplication and addition for each number, and no divisions.</p> <p>This generator takes an optional argument controlling whether it produces numbers from the open, closed, or half-open interval.</p> Reversed Ranges (Python) 2014-03-29T12:43:26-07:00Tal Einathttp://code.activestate.com/recipes/users/2892534/http://code.activestate.com/recipes/576801-reversed-ranges/ <p style="color: grey"> Python recipe 576801 by <a href="/recipes/users/2892534/">Tal Einat</a> (<a href="/recipes/tags/iteration/">iteration</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/reverse/">reverse</a>). Revision 16. </p> <p>A simple function for efficiently iterating over ranges in reverse.</p> <p>This is equivalent to reversed(range(...)) but somewhat more efficient.</p>