Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4634

How to compress arrays of integers into an array of ranges and integers in Ruby

$
0
0

I have a sorted array of integers, and want all subsets of consecutive integers to be replaced with a range, so that:

[1,2,3,4,5,6,7,8,9,10,42]

is replaced with:

[1..10,42]

It's a list of rows from a Google spreadsheet that the user did not enter properly, and that could not be imported into another system's database. There are a lot of rows, and often dozens in sequence. It's much easier for a user to see that rows 3201-3379 are all bad, rather than having to read every single row number individually.

I'm not asking for "Array of integers into array of ranges".

I know how to write the code, and I'm happy to do so, but not if someone has already done it. My current code is not elegant and I'm not going to bother refactoring it if there's something well tested and optimized already out there.


Viewing all articles
Browse latest Browse all 4634

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>