## Collect Sets Of Ranges

### August 25, 2015

A frequent idiom in data processing is the control-break idiom, where some processing must be done every time there is a change in some value. A simple example comes from collecting ranges, for instance, converting the sequence 0, 1, 2, 7, 21, 22, 108, 109 to the ranges 0-2, 7, 21-22, 108-109, where a break occurs whenever two numbers aren’t consecutive.

Writing control-break programs can be difficult, for two reasons. First, you don’t know there is a break until you see the next record after the break, so you either need to look ahead in the input or keep track of what you have seen. Second, there is an implied break at the end of the input, which occurs when there is no record at all. Depending on the situation, either or both of those can be tricky.

Your task is to write a program that converts a sequence to a set of ranges, as shown above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

### 25 Responses to “Collect Sets Of Ranges”

1. Rutger said

Keeps track of what is visited, using a Python generator to yield each range found.

```def set_of_ranges(seq):
if not seq:
raise StopIteration
a_range = [seq]
for i,value in enumerate(seq[1:]):
if value == a_range[-1] + 1:
a_range.append(value)
else:
yield "%i - %i"%(a_range, a_range[-1])
a_range = [value]
yield "%i - %i"%(a_range, a_range[-1])

test_sequences = [[], [0,1,2,3,4,-1,0,1], [0, 1, 2, 7, 21, 22, 108, 109]]

for seq in test_sequences:
print "Test sequence:", seq
for r in set_of_ranges(seq):
print r

# output
# Test sequence: []
# Test sequence: [0, 1, 2, 3, 4, -1, 0, 1]
# 0 - 4
# -1 - 1
# Test sequence: [0, 1, 2, 7, 21, 22, 108, 109]
# 0 - 2
# 7 - 7
# 21 - 22
# 108 - 109
```
2. Paul said

In Python.

```def collected_ranges(seq):
it = iter(seq)
last = (next(it),)
for e in it:
if e - last[-1] == 1:
last = (last, e)
else:
yield last
last = (e,)
yield last

def str_range(r):
return str(r) if len(r) == 1 else str(r) + "-" + str(r)

seq = [0, 1, 2, 7, 21, 22, 108, 109 ]
print(", ".join(str_range(r) for r in collected_ranges(seq)))
```
3. matthew said
```import Data.List
g [] = []
g (n:s) = f n n s where
f m n [] = [(m,n)]
f m n (p:s)
| n+1 == p = f m p s
| otherwise = (m,n):f p p s
h (m,n)
| m == n = show m
| otherwise = show m ++ "-" ++ show n
ranges = concat . intersperse "," . map h . g
main = putStrLn \$ ranges [0,1,2,7,21,22,108,109]
```
4. Dennis Decker Jensen said

It’s a classic problem that Jackson suggested a simple solution for.
You structure the control flow after the data, i.e. one outer loop for the break points, and one inner loop for a range. The point is to have the state implicitly coded into the control flow, rather than manipulating an explicit state in a single loop. In this case this can be done by “reading ahead” before the loop, and then “reading” at the end of the loop.

To put it simple: Follow the structure of the data. This makes the loops very simple and easy to understand.

```def collect(seq):
result = []
it = iter(seq)
e = next(it, None)
while e is not None:
fst, snd = e, e
e = next(it, None)
while e is not None and e == snd + 1:
snd = e
e = next(it, None)
range = (fst, snd)
result.append(range)
return result

print(collect([0, 1, 2, 7, 21, 22, 108, 109]))
print(collect([]))
print(collect([0, 1, 2, 3, 4, -1, 0, 1, 10]))
print(collect([0, 2, 3, 4, -3, -2, -1, 10]))
```
5. FA said
```  def collectRanges(in: Stream[Int], left: Option[Int] = None, right: Option[Int] = None): Stream[Tuple2[Int, Int]] = {
println(in.toList+" "+left+" "+right)
if (left == None)
if (in.isEmpty) Stream.Empty
else
if (in.isEmpty) Stream((left.get, right.get))
else if (in.head == right.get + 1) collectRanges(in.tail, left, Some(right.get + 1))
else Stream((left.get, right.get)) #::: collectRanges(in)
}
```
6. FA said

Oh yeah, the previous comment is the solution as a Scala Stream

7. klettier said

F# solution :

```let set = [-1;0; 1; 2; 7; 21; 22; 108; 109; 110; 115 ; 116 ; 117]

let f ar =
let rec loop arr last range isInRange (acc: string list) =
match arr with
| a :: tail when range = "" -> loop tail a (a.ToString()) false acc
| a :: tail when last = (a - 1) -> loop tail a range true acc
| a :: tail -> if(not isInRange) then
range :: acc
|> loop tail a (a.ToString()) false
else (range + "-" + last.ToString()) :: acc
|> loop tail a (a.ToString()) false
| [] -> if(not isInRange) then
range :: acc
else (range + "-" + last.ToString()) :: acc
loop ar -1 "" false [] |> List.rev

let result = f set;;

//val result : string list = ["-1-2"; "7"; "21-22"; "108-110"; "115-117"]

```
8. matthew said

Here’s another Haskell solution, no fancy output this time, but using a general splitting function with foldl operating on the partially constructed ranges:

import Control.Arrow ((&&&))
split p (x:s) = foldl aux [[x]] s where
aux (s@(x:_):acc) y
| p x y = (y:s):acc
| otherwise = [y]:s:acc
ranges = reverse . map (last &&& head) . split ((==).(+1))
main = print \$ ranges [0,1,2,7,21,22,108,109]
— [(0,2),(7,7),(21,22),(108,109)]

9. matthew said

Here’s another Haskell solution, no fancy output this time, but using a general splitting function with foldl operating on the partially constructed ranges:

```import Control.Arrow ((&&&))
split p (x:s) = foldl aux [[x]] s where
aux (s@(x:_):acc) y
| p x y = (y:s):acc
| otherwise = [y]:s:acc
ranges = reverse . map (last &&& head) . split ((==).(+1))
main = print \$ ranges [0,1,2,7,21,22,108,109]
-- [(0,2),(7,7),(21,22),(108,109)]
```

(No Haskell highlighter – using “text” here)

10. bitchef said

Just as I would do with a pen and paper, I track the consecutive numbers; their differences is one. Anything other than that marks a breakpoint.
Here is the implementation in JavaScript:

```
function makeRange(nums){
// store differences betweens the numbers.
// This is a way to know consequtive numbers
var diffs = [];
for (var i = 1; i < nums.length; i++){
diffs.push (nums[i] - nums[i-1]);
}

// For example, for the numbers : 1,2,3,5,6,9
// The differences are :          1,1,2,1,3
// Numbers that are not 1 mark the break points.
// Will now use these break points to construct the ranges
var res = [];
for (var j = 0, i = 0; j < nums.length; j++){
// for each break point, get the indexes up to, but not including, that point.
if (diffs[j] != 1){
var range = nums.slice(i,j+1);
res.push(range);
i = j + 1;
}
}

return res;
}
```

Sample run:

```> makeRange([1,2,3,4,5])
[ [ 1, 2, 3, 4, 5 ] ]
> makeRange([1,2,23,45,46,47])
[ [ 1, 2 ], [ 23 ], [ 45, 46, 47 ] ]
> makeRange([-1,-2,-23,-45,-46,-47])
[ [ -1 ], [ -2 ], [ -23 ], [ -45 ], [ -46 ], [ -47 ] ]
> makeRange([-3,-2,-1])
[ [ -3, -2, -1 ] ]
> makeRange([-3,-2,-1,0,1,3,4,5,100,101,102])
[ [ -3, -2, -1, 0, 1 ], [ 3, 4, 5 ], [ 100, 101, 102 ] ]
```

public class Range {

public static String getRange(int a[], int index)
{

if(index == a.length-1)
return “”+a[index];
else{

int i=index;
int start=a[i];
int end=0;

while(a[i+1]==a[i]+1)
i++;

end=a[i];

if(end!=start)
return start+”-“+end+”,”+getRange(a,i+1);
else
return start+”,”+getRange(a,i+1);
}
}

public static void main(String[] args) {

int a[]={1,2,3,4,5,6,8,9,10,-34,-33,36,37,4,8,9,0,1,1,1};

System.out.println(getRange(a,0));
}
}

12. Globules said

Another Haskell solution. We allow finding ranges for types other than numbers.

```-- True if and only if x is either the predecessor of y or equal to it.
isPreq :: (Bounded a, Enum a, Eq a) => a -> a -> Bool
x `isPreq` y = x == y || y /= minBound && x == pred y

-- Condense a list into a sequence of ranges.  The range of a single element
-- x is (x,x).
ranges :: (Bounded a, Enum a, Eq a) => [a] -> [(a, a)]
ranges = foldr step []
where step x [] = [(x,x)]
step x ((a,b):cs) | x `isPreq` a =       (x,b):cs
| otherwise    = (x,x):(a,b):cs

-------------------------------------------------------------------------------

data Spectrum = Red | Orange | Yellow | Green | Blue | Indigo | Violet
deriving (Bounded, Enum, Eq, Show)

main :: IO ()
main = do
-- The list of numbers from the problem.
let ts = [0, 1, 2, 7, 21, 22, 108, 109] :: [Int]
putStrLn \$ show ts ++ " =>\n  " ++ show (ranges ts)

-- The numbers can be repeated and don't have to be increasing.
let us = [108, 109, 0, 1, 1, 2, 2, 21, 22, 7] :: [Int]
putStrLn \$ show us ++ " =>\n  " ++ show (ranges us)

-- We can generate ranges for types other than numbers.  The ordering is
-- determined by the declaration of Spectrum.
let ss = [Indigo, Violet, Red, Yellow, Green, Blue, Orange]
putStrLn \$ show ss ++ " =>\n  " ++ show (ranges ss)
```
```\$ ./ranges
[0,1,2,7,21,22,108,109] =>
[(0,2),(7,7),(21,22),(108,109)]
[108,109,0,1,1,2,2,21,22,7] =>
[(108,109),(0,2),(21,22),(7,7)]
[Indigo,Violet,Red,Yellow,Green,Blue,Orange] =>
[(Indigo,Violet),(Red,Red),(Yellow,Blue),(Orange,Orange)]
```
13. matthew said

@Globules: nice. Interesting that neither of our fold-based solutions work for infinite lists, whereas my original non-fold-based one does.

14. matthew said

So, here is another Haskell solution, using scanl as the looping construct rather than a fold – scanl is like foldl but returns a list of the intermediate results, so we can map over that to extract the actual ranges. To deal with the end of input case, we need to munge the data a little, so we can’t just use concat. Infinite lists are dealt with just fine:

```ranges (x:s) = munge \$ scanl f ([],(x,x)) s where
f (_,(a,b)) c
| b+1 == c = ([],(a,c))
| otherwise = ([(a,b)],(c,c))
munge [(x,a)] = x ++ [a]
munge ((x,_):s) = x ++ munge s

s = 0:1:2:map (+4) s

main =
(print \$ ranges [0,1,2,7,21,22,108,109]) >>
(print \$ ranges ) >>
(print \$ take 10 \$ ranges s)
```
15. Globules said

@matthew: Thanks. Yeah, I wasn’t even thinking about infinite lists when I wrote it. (Bad programmer!)

Another annoyance is that in generalizing it to Enum I had to also use Bounded to ensure that pred didn’t throw. But of course now it doesn’t work for Integer. Having maybePred and maybeSucc would be nice.

16. matthew said

Aha, yes, I didn’t see that problem. Here’s another iterative solution, this time using unfoldr. It’s rather like Dennis’s nested loop above. Once again, we have to do some jiggerypokery to terminate properly:

```import Data.List

ranges :: (Eq t, Num t) => [t] -> [(t, t)]
ranges [] = []
ranges (a:s) = unfoldr (fmap aux) \$ Just(a,a,s) where
aux (a,b,[]) = ((a,b),Nothing)
aux (a,b,c:s)
| b+1 == c = aux (a,c,s)
| otherwise = ((a,b),Just(c,c,s))

ss :: [Integer]
ss = 0:1:2:map (+4) ss

main :: IO()
main =
(print \$ ranges ([0,1,2,7,21,22,108,109] :: [Int])) >>
(print \$ ranges ( :: [Int])) >>
(print \$ take 10 \$ ranges ss)
```
17. Mike said

itertools.groupby() from the standard library creates an iterator that breaks an iterable into (key, group) pairs.

Here, a class is used to maintain state–the previous value and the current group number. The class implements the __call__ method to be used as the key func for groupby(). The method applies a predicate to the previous and current items. If the predicate is true, the group number is returned. If the predicate is false, the group number is changed (incremented) first. As a result, each group gets a sequential number assigned.

```def collect(seq, predicate):
class Grouper:
def __init__(self, predicate):
self.group_no = 0
self.predicate = predicate
self.prev_value = None

def __call__(self, new_value):
if self.prev_value is not None and not self.predicate(self.prev_value, new_value):
self.group_no += 1
self.prev_value = new_value
return self.group_no

yield from (g for _,g in it.groupby(seq, Grouper(predicate)))

[list(g) for g in collect(seq, lambda a,b: a + 1 == b)]
```

This can also be implemented using a generator/coroutine. The generator’s send() method is used as the key func for groupby().

```def collect(seq, predicate):
def grouper(predicate):
group_no = 0
new_value = yield
while True:
prev_value, new_value = (new_value, (yield group_no))
if not predicate(prev_value, new_value):
group_no += 1

gpr = grouper(predicate)
next(gpr)
yield from (g for _,g in it.groupby(seq, gpr.send))

[list(g) for g in collect(seq, lambda a,b: a + 1 == b)]
```
18. matthew said

Definitely the last Haskell solution from me. The pattern here is a sort of map with state, with some special handling of the final state; this can all be nicely captured by the mapAccumL function, as in:

```import Data.List

ranges [] = []
ranges (x:s) = concat t ++ [a] where
(a,t) = mapAccumL f (x,x) s
f (m,n) p = if n+1 == p then ((m,p),[])
else ((p,p),[(m,n)])

main = print \$ ranges [0,1,2,7,21,22,108,109]
```
19. Will Ness said

Haskell. A job for a right fold with a lazy combining function, creating the shape of the return value first, and filling it up with data from the recursive result later, like any well-behaved tail recursive “modulo cons” program would do.

```ranges = foldr (\x r-> let (b:c) = [ [] | null r || x+1 /= (head\$head r)]++r in ((x:b):c)) []
```

(the list comprehension is there just to avoid the verbal and verbose “case” statement).

20. matthew said

@Will Ness: Very ingenious. We can rewrite Globules original foldr solution (using a case statement here, I like pattern matching) as:

```ranges = foldr step [] where
step x r = (x,y):s where
(_,y):s = case r of
[] -> (x,x):r
(a,_):_ -> if x+1 == a then r else (x,x):r
```

and get proper behaviour on infinite lists. It does all seem a bit awkward though – I’m not convinced I like any of the functional solutions more than the original nice and simple recursive method.

21. Will Ness said

@matthew: YMMV. I learned to stop worrying, and love the fold. :) It’s easier for me that way: the recursion is already there, `x` is for the value, `r` is for recursive result, and all that’s left for me to do is to decide what goes where. It’s all about the information flow anyway. Here’s (http://stackoverflow.com/a/14403413/849891) where I saw this technique first, in the context of Haskell. In Prolog it’s commonplace, but there we only have the direct recursion (in plain language, that is).

Turnes out there’s also my answer on that SO question, with another solution, a paramophism which might feel more natural.

Also, your “jiggerypokery” `unfoldr` might be related to this (http://stackoverflow.com/q/24519530/849891) “slightly generalizing unfold”, which just might be an apomorphism.

22. matthew said

Well, I like a nice fold too, but sometimes things don’t always fit nicely into that pattern. Foldr has nice algebraic properties, but it seems difficult to get right, particularly with lazy lists (and there seems to be a correlation between not working with lazy lists and having a space leak) – maybe that gets easier with practice though, as you suggest. Apomorphism sounds like just the ticket, thanks for that; disjoint unions are an underused datatype.

Incidentally, I came up with this for finding the end points of the ranges, but I’m not sure what to make of it:

```ranges (x:s) = foldr (\p g (m,n) -> if n+1 == p then g(m,p) else (m,n) : g(p,p)) (:[]) s (x,x)
```
23. matthew said

So, my original function, which was more or less:

```ranges (x:s) = f (x,x) s where
f (m,n) [] = [(m,n)]
f (m,n) (p:s)
| n+1 == p = f (m,p) s
| otherwise = (m,n) : f (p,p) s
```

works nicely with infinite lists and all that, but doesn’t easily fit the foldr recursion scheme because of that extra state parameter – sometimes the recursion is ‘f (m,p) s’ and sometimes it is ‘f (p,p) s’. But we can swap the order of the parameters to f and get:

```ranges2 (x:s) = f s (x,x) where
f [] (m,n) = [(m,n)]
f (p:s) (m,n)
| n+1 == p = f s (m,p)
| otherwise = (m,n) : f s (p,p)
```

and this does fit the foldr pattern, giving:

```ranges3 (x:s) = foldr (\p g (m,n) -> if n+1 == p then g(m,p) else (m,n) : g(p,p)) (:[]) s (x,x)
```

where the function on the input list is returning another function that is then applied to initial state. Actually running this with eg. ‘ranges [1..100000000]’ and we get the answer ‘[(1,100000000)]’ in a few seconds, with no memory bloat. Once again, I am very impressed with the Haskell compiler.

24. matthew said

On the other hand, all the compiler has really done is inline the definition of foldr, so maybe it’s not that clever.

25. r. clayton said

A solution in Racket Scheme.