Questions tagged [range]

A range is an extent of values between its lower and upper bound. It can refer to a DOM Range, the Ruby Range class, the Python range function, Perl 5's `..` operator, Perl 6's Range Class, or PostgreSQL's range types.

A range is an extent of values between its lower and upper bound. Examples include a DOM Range, the Ruby Range class, the Python range function, Perl 5's .. operator (in list context), Perl 6's Range Class and PostgreSQL's range types.

In statistics, range is the size of the smallest interval which contains all the data and provides an indication of statistical dispersion. It is measured in the same units as the data. Since it only depends on two of the observations, it is most useful in representing the dispersion of small data sets.

10562 questions
2964
votes
12 answers

Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator. This being the case, I would have expected the following line to take an inordinate amount of…
Rick
  • 43,029
  • 15
  • 76
  • 119
2230
votes
78 answers

How to create an array containing 1...N

I'm looking for any alternatives to the below for creating a JavaScript array containing 1 through to N where N is only known at runtime. var foo = []; for (var i = 1; i <= N; i++) { foo.push(i); } To me it feels like there should be a way of…
Godders
  • 22,776
  • 4
  • 19
  • 19
1277
votes
21 answers

How do I check if a string contains another string in Objective-C?

How can I check if a string (NSString) contains another smaller string? I was hoping for something like: NSString *string = @"hello bla bla"; NSLog(@"%d",[string containsSubstring:@"hello"]); But the closest I could find was: if ([string…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
999
votes
34 answers

How do I use a decimal step value for range()?

How do I iterate between 0 and 1 by a step of 0.1? This says that the step argument cannot be zero: for i in range(0, 1, 0.1): print(i)
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
842
votes
28 answers

What is the difference between range and xrange functions in Python 2.X?

Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about for i in range(0, 20): for i in xrange(0, 20):
Teifion
  • 108,121
  • 75
  • 161
  • 195
508
votes
20 answers

Print a list in reverse order with range()?

How can you produce the following list with range() in Python? [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
ramesh.mimit
  • 9,445
  • 4
  • 21
  • 23
484
votes
24 answers

How does String substring work in Swift

I've been updating some of my old code and answers with Swift 3 but when I got to Swift Strings and Indexing with substrings things got confusing. Specifically I was trying the following: let str = "Hello, playground" let prefixRange =…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
462
votes
12 answers

Should you always favor xrange() over range()?

Why or why not?
mbac32768
  • 11,453
  • 9
  • 34
  • 40
417
votes
11 answers

Why does range(start, end) not include end?

>>> range(1,11) gives you [1,2,3,4,5,6,7,8,9,10] Why not 1-11? Did they just decide to do it like that at random or does it have some value I am not seeing?
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
391
votes
6 answers

NameError: global name 'xrange' is not defined in Python 3

I am getting an error when running a python program: Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in File "C:\Program Files (x86)\Wing IDE 101…
Pip
  • 4,387
  • 4
  • 23
  • 31
388
votes
16 answers

Best way to extract a subvector from a vector?

Suppose I have a std::vector (let's call it myVec) of size N. What's the simplest way to construct a new vector consisting of a copy of elements X through Y, where 0 <= X <= Y <= N-1? For example, myVec [100000] through myVec [100999] in a vector…
An̲̳̳drew
  • 13,375
  • 13
  • 47
  • 46
388
votes
17 answers

What's the most efficient way to test if two ranges overlap?

Given two inclusive ranges [x1:x2] and [y1:y2], where x1 ≤ x2 and y1 ≤ y2, what is the most efficient way to test whether there is any overlap of the two ranges? A simple implementation is as follows: bool testOverlap(int x1, int x2, int y1, int y2)…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
291
votes
5 answers

How to loop backwards in python?

I'm talking about doing something like: for(i=n; i>=1; --i) { //do something with i } I can think of some ways to do so in python (creating a list of range(1,n+1) and reverse it, using while and --i, ...) but I wondered if there's a more elegant…
snakile
  • 52,936
  • 62
  • 169
  • 241
284
votes
13 answers

Is there a way to iterate over a range of integers?

Go's range can iterate over maps and slices, but I was wondering if there is a way to iterate over a range of numbers, something like this: for i := range [1..10] { fmt.Println(i) } Or is there a way to represent range of integers in Go like…
Vishnu
  • 4,377
  • 7
  • 26
  • 40
274
votes
8 answers

What does the "map" method do in Ruby?

What does .map do in: params = (0...param_count).map
bigpotato
  • 26,262
  • 56
  • 178
  • 334
1
2 3
99 100