Created
October 10, 2015 00:27
-
-
Save groz/d4d053819e4ff661688c to your computer and use it in GitHub Desktop.
Facebook's how to crush your coding interview example in Scala.
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 FBSampleApp extends App { | |
// sample implementations | |
def nearbyChars(c: Char): Set[Char] = Set(c + 0, c + 1).map(_.toChar) | |
def isWord(s: String): Boolean = true | |
def distSoFar(a: String, b: String) = a.zip(b).count(ab => ab._1 != ab._2) | |
def nearbyWords(input: String, maxDist: Int = 1): Set[String] = | |
input.foldLeft(Set("")) { (words, char) => | |
for { | |
c <- nearbyChars(char) | |
w <- words | |
word = w + c | |
if distSoFar(input, word) <= maxDist | |
} yield word | |
}.filter(isWord) | |
println(nearbyWords("Hello")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment