Created
January 5, 2013 02:17
-
-
Save fayimora/4459253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
car truck 8 4 bus 6 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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