Skip to content

Instantly share code, notes, and snippets.

@UberMouse
Last active December 18, 2015 02:29
Show Gist options
  • Save UberMouse/5711126 to your computer and use it in GitHub Desktop.
Save UberMouse/5711126 to your computer and use it in GitHub Desktop.
Solves the Beautiful Strings CodeEval problem (Slightly modified version of the Facebook Hacker cup 2013 Hackathon)
Array("Good luck in the Facebook Hacker Cup this year!",
"Ignore punctuation, please :)",
"Sometimes test cases are hard to make up.",
"So I just go consult Professor Dalves")
.filter(_.length > 0)
.map(x => x.toLowerCase())
.map(x => "[^a-z]*".r replaceAllIn(x, ""))
.map(x => x.map(y => (x.count(_ == y), y)).sortWith(_._1 > _._1).distinct.map(_._1).toArray)
.map(process(_))
.map(_.map(x => x._1 * x._2))
.map(_.sum)
.foreach(println)
def process(charsByFreq:Array[Int], curCharValue:Int = 26, charValues:List[(Int, Int)] = List[(Int, Int)]()):List[(Int, Int)] = {
if(charsByFreq.length == 0) charValues
else process(charsByFreq.drop(1), curCharValue - 1, charValues ::: List((curCharValue, charsByFreq.head)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment