Skip to main content
deleted 1 character in body
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

Scala, 207204 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;(Seq[Seq[Int]](Seq(s(0)))/:s.sliding(2)){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+ p+p(1)) else c:+ Seq+Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r(0)}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}

Scala, 207 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;(Seq[Seq[Int]](Seq(s(0)))/:s.sliding(2)){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+ p(1)) else c:+ Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r(0)}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}

Scala, 204 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;(Seq[Seq[Int]](Seq(s(0)))/:s.sliding(2)){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+p(1))else c:+Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r(0)}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}

deleted 12 characters in body
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

Scala, 218207 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;s.sliding(2).foldLeftsorted;(Seq[Seq[Int]](Seq(s.head(0)))/:s.sliding(2)){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+ p(1)) else c:+ Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r.head(0)}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}

Scala, 218 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;s.sliding(2).foldLeft(Seq[Seq[Int]](Seq(s.head))){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+ p(1)) else c:+ Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r.head}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}

Scala, 207 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;(Seq[Seq[Int]](Seq(s(0)))/:s.sliding(2)){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+ p(1)) else c:+ Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r(0)}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}

Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

Scala, 218 bytes

Modified from @steenslag's Ruby answer.


Golfed version. Try it online!

def s(a:Int*):Seq[String]={val s=a.sorted;s.sliding(2).foldLeft(Seq[Seq[Int]](Seq(s.head))){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+ p(1)) else c:+ Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r.head}-${r.last}")}

Ungolfed version. Try it online!

object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }

  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}