Skip to content

Instantly share code, notes, and snippets.

@fayimora
Created January 5, 2013 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fayimora/4459253 to your computer and use it in GitHub Desktop.
Save fayimora/4459253 to your computer and use it in GitHub Desktop.
car truck 8 4 bus 6 1
object Sort {
def main(args: Array[String]){
def merge(ints: List[String], strings: List[String], line: List[String])(isInt: String => Boolean): List[String] = (ints, strings) match{
case (ints, Nil) => ints
case (Nil, strings) => strings
case (i :: iTails, s :: sTails) =>
if(isInt(line.head)) i :: merge(iTails, strings, line.tail)(isInt)
else s :: merge(ints, sTails, line.tail)(isInt)
}
val toCells = (_: String).trim.split(" +").toList
val isInt = (_:String).matches("""\d+""")
io.Source.stdin.getLines.map(toCells).foreach{ line =>
val (ints, strings) = line.partition(isInt(_))
println(merge(ints.sortWith(_<_), strings.sortWith(_<_), line)(isInt).mkString(", "))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment