Skip to content

Instantly share code, notes, and snippets.

@williamho
Created June 19, 2015 21:05
Show Gist options
  • Save williamho/1faeb3143d47854c1705 to your computer and use it in GitHub Desktop.
Save williamho/1faeb3143d47854c1705 to your computer and use it in GitHub Desktop.
find words that can be typed by calculator
// usage: `scala beghilosz.scala < /usr/share/dict/words`
object Program {
val mask: Int = toBitmask("beghilosz")
def main(args: Array[String]): Unit = {
val lines: Iterator[String] = io.Source.stdin.getLines()
val filtered: Iterator[String] = lines.filter { (l: String) =>
val b: Int = toBitmask(l)
(b & mask) == b
}
filtered.foreach(println)
}
def toBitmask(s: String): Int = s.foldLeft(0) {
(acc: Int, c: Char) => acc | (1 << (c.toLower - 'a'))
}
}
// minified inefficient ver:
// io.Source.stdin.getLines.filter(_.forall("beghilosz"contains _.toLower))foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment