Created
April 14, 2014 01:41
-
-
Save simonexmachina/10610071 to your computer and use it in GitHub Desktop.
CoffeeScript is beautiful
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
fs = require 'fs' | |
_ = require 'underscore' | |
input = '/Users/simonwade/Downloads/wl.txt' | |
dictionary = fs.readFileSync(input).toString().split('\n') | |
anagram = (w)-> | |
anagrams = [] | |
chars = countChars w | |
for word in _.filter dictionary, (word)-> word.length == w.length | |
different = false | |
for char, count of countChars word | |
if chars[char] != count | |
different = true | |
break | |
unless different | |
anagrams.push word | |
anagrams | |
countChars = (w)-> | |
chars = {} | |
for i in [0..w.length] | |
l = w[i] | |
if chars[l] then chars[l]++ else chars[l] = 1 | |
chars | |
if word = process.argv[2] | |
console.log anagram word | |
else | |
console.log "Usage: #{process.argv[0]} {word}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment