title: How to do an inverse `range`, i.e. create a compact range based on a set of numbers?
body: <p>Python has a <code>range</code> method, which allows for stuff like:</p>
  
  <pre><code>&gt;&gt;&gt; range(1, 6)
  [1, 2, 3, 4, 5]
  </code></pre>
  
  <p>What I’m looking for is kind of the opposite: take a list of numbers, and return the start and end.</p>
  
  <pre><code>&gt;&gt;&gt; magic([1, 2, 3, 4, 5])
  [1, 5] # note: 5, not 6; this differs from `range()`
  </code></pre>
  
  <p>This is easy enough to do for the above example, but <strong>is it possible to allow for gaps or multiple ranges as well, returning the range in a PCRE-like string format?</strong> Something like this:</p>
  
  <pre><code>&gt;&gt;&gt; magic([1, 2, 4, 5])
  ['1-2', '4-5']
  &gt;&gt;&gt; magic([1, 2, 3, 4, 5])
  ['1-5']
  </code></pre>
  
  <p><strong>Edit:</strong> I’m looking for a Python solution, but I welcome working examples in other languages as well. It’s more about figuring out an elegant, efficient algorithm. Bonus question: is there any programming language that has a built-in method for this?</p>