Created
June 19, 2015 21:05
-
-
Save williamho/1faeb3143d47854c1705 to your computer and use it in GitHub Desktop.
find words that can be typed by calculator
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
// 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