Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created November 14, 2016 16:24
Show Gist options
  • Save railsstudent/f7d99481f1785fb672a322afd7c2402d to your computer and use it in GitHub Desktop.
Save railsstudent/f7d99481f1785fb672a322afd7c2402d to your computer and use it in GitHub Desktop.
solution = (list) ->
# TODO: complete solution
console.log (list)
result = ""
# make list of intervals
intervals = [[list[0]]]
for i in [1...list.length]
lastInterval = intervals[intervals.length - 1]
if list[i] is lastInterval[lastInterval.length - 1] + 1 then lastInterval.push list[i]
else intervals.push [list[i]]
for interval in intervals
if result isnt '' then result += ','
if interval.length is 1 then result += interval[0]
else if interval.length is 2 then result += interval[0] + "," + interval[1]
else result += interval[0] + "-" + interval[interval.length - 1]
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment