1
You have a program which creates an index for a catalogue. The last step is missing: it must find and consolidate page ranges within the list of pages for each entry.
Each entry is currently an array of unique integers in order, e.g:
[18,19,20,21,22,47,56,57,62,67,68,69,70]
Write a function which returns a consolidated array of ranges and individual pages as appropriate.
Individual pages can be converted to strings or remain as integers. Either of these would be valid results from the example input:
["18-22",47,"56-57",62,"67-70"] // valid
["18-22","47","56-57","62","67-70"] // valid
6How are you going to measure 'fastest-code'? The trivial implementation is
O(n)
already, and I don't see a way to make it less thanO(n)
, because you have to process the entire list anyway. – marinus – 2014-01-02T15:36:19.5631Perhaps better suited for golfing? – Yves Klett – 2014-01-02T17:39:53.517
I second the golfing idea. – Tim Seguine – 2014-01-02T22:19:29.580
Yes fair comment. An implementation of this might need to be run thousands of times for a large or broad-range catalogue, but I suppose it wouldn't be possible to decrease the complexity of processing each entry. – Tom Dudman – 2014-01-03T08:21:00.227
1@TomDudman I gues for a few thousand entries, all but the most horrible implementations will work very quickly (you could post a test set). – Yves Klett – 2014-01-03T09:15:06.707
-1 since you already accepted an answer even though you don't have an objective winning criterion to distinguish answers. – Tim Seguine – 2014-01-17T12:54:15.300
I guess OP is just looking for a code. – justhalf – 2014-06-12T03:40:48.097