Disclaimer: while I have been on this site for entertainment purposes for a little while now, this is my first question, so please forgive any minor errors.
When assigning us homework, my teacher is really annoying and writes out all the problems we have to do individually. As such, it takes me forever to copy down which problems I have to do. I thought to make my life easier, I would send him a program that could make the list of problems take up less space.
While writing down a list of page or problem numbers, we use a dash to denote a range. For example,
19, 20, 21. If there is a gap in between, two comma-separated ranges are used:
19-21, 27-31 becomes
19, 20, 21, 27, 28, 29, 30, 31.
Right about now, you're probably thinking: "this seems pretty trivial". In fact, this has already been answered here and here.
However, there is a catch. If we have a range with equal consecutive digits, the repeated digits can be left out. For example:
15, 16, 17 becomes
107, 108, 109 becomes
107-9. For a bonus, if the last consecutive equal digit is 1 greater and the upper limit's last digit is less than or equal to that of the lower, the following can be omitted (sorry if that sounded confusing; perhaps some examples will clear it up).
109-3, as a lower last digit implies increasing the 10s place.
Your program should take a list of integers via input (whatever is standard for your language, or a function). You can decide whether this list is comma-separated, space-separated, or as an actual list/array.
Output the shortest way (first sorted by number of ranges, then the sum of the characters included in the ranges) to represent that list using this notation. Each dashed range must be on the same line, but the ranges can be separated by commas or newlines (trailing newlines or commas are allowed). These ranges must be in order.
Since our school Wi-Fi is terrible, I have to make the file as small as possible in order to send it to him. The shortest code (in bytes) wins.
My teacher is sloppy, so there are a few things that would help him out. Multiple bonuses stack through multiplication, e.g. a -10% bonus (x 90%) and a -25% (x 75%) bonus = 90% * 75% = x 67.5% (-32.5% bonus).
- Sometimes he puts them in the wrong order (he's not a math teacher). Take a -20% bonus if your program can accept integers that are not sorted least-to-greatest.
- Our book is weird, and each section starts counting the problems at -10. If your program can accept negative numbers, take a -25%.
- If it accepts the bonus of a lower last digit increasing 10's place, e.g.
25-2, take a -50% bonus.
In: 1, 2, 3, 4, 5 Out: 1-5 In: 3, 4, 5, 9, 10, 11, 12 Out: 3-5, 9-12 In: 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160 Out: 149-60 In: 1 2 3 4 Out: 1-4 For bonuses: In: 109, 110, 111, 112, 113 Out: 109-3 In: 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 Out: 19-9 In: -3, -2, -1, 0, 1, 2 Out: -3-2 In: -3, -2, -1 Out: -3--1
An answer will be accepted on Saturday, 19 December 2015.