Skip to content

Instantly share code, notes, and snippets.

@simonexmachina
Created April 14, 2014 01:41
Show Gist options
  • Save simonexmachina/10610071 to your computer and use it in GitHub Desktop.
Save simonexmachina/10610071 to your computer and use it in GitHub Desktop.
CoffeeScript is beautiful
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