Created
June 22, 2015 20:39
-
-
Save paralax/07861e7f9291da1eaabf to your computer and use it in GitHub Desktop.
sentence mangler
This file contains hidden or 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
| def getNonLetters(s:String): List[Int] = s.map(_.isLetter).zipWithIndex.filter(_._1==false).map(_._2).toList | |
| def getUpperCase(s:String): List[Int] = s.map(_.isUpper).zipWithIndex.filter(_._1==true).map(_._2).toList | |
| def makeLetters(s:String): List[Char] = s.toList.filter(_.isLetter == true) | |
| def mkUpper(upps:List[Int], s:String): String = { | |
| def loop(is:List[Int], s:String): String = { | |
| is match { | |
| case Nil => s | |
| case x::xs => loop(xs, s.slice(0,x)+s(x).toUpper+s.slice(x+1,s.length)) | |
| } | |
| } | |
| loop(upps, s) | |
| } | |
| def addPunc(punc:List[Int], orig:String, s:String): String = { | |
| def loop(is:List[Int], orig:String, s:String): String = { | |
| is match { | |
| case Nil => s | |
| case x::xs => loop(xs, orig, s.slice(0,x)+orig(x)+s.slice(x,s.length)) | |
| } | |
| } | |
| loop(punc, orig, s) | |
| } | |
| def arrange(s:String): String = { | |
| val nons = getNonLetters(s) | |
| val upps = getUpperCase(s) | |
| mkUpper(upps, s.toLowerCase.split(" ").map(makeLetters(_).sorted).toList.map(_.mkString).mkString(" ")) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment