Last active
August 4, 2023 13:14
-
-
Save pgblu/7ada1006a52374dfa486de559e157ff0 to your computer and use it in GitHub Desktop.
Barden's tool
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
dictionary = IO.readlines("path/to/your/dictionary").map {|word| word.chomp} | |
# you can replace "path/to/your/dictionary" with "/usr/share/dict/words", but I | |
# recommend a smaller dictionary, such as the one I've saved here: | |
# https://github.com/pgblu/dryphtwords/blob/master/app/fixtures/main_dictionary.txt | |
needed_length = ARGV[0].to_i | |
dropped_letter = ARGV[1] | |
intermediate_result = dictionary.select {|word| word.length == needed_length && word.split('').include?(dropped_letter)} | |
def drop_letter(word, letter) | |
ar = word.split('') | |
droppables = ar.each_index.select {|ix| ar[ix] == letter} | |
return droppables.map {|index| v = ar.dup; v.delete_at(index); v.join} | |
end | |
result = intermediate_result.select do |word| | |
drop_letter(word, dropped_letter).any? do |new_word| | |
dictionary.include? new_word | |
end | |
end | |
result.map {|word| puts word} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment