Created
October 6, 2017 10:53
-
-
Save Pablo-R/5c35a0f668dfda671232c4189bc7df57 to your computer and use it in GitHub Desktop.
Scrabble Stems
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
#!/usr/bin/ruby | |
#word file used: /usr/share/dict/words | |
WORD_LENGTH = 7 | |
$stems_list = {} | |
cut_off = ARGV.shift | |
$unique_word_list = [] | |
def word_exists?(sorted_word) | |
$unique_word_list.include? sorted_word | |
end | |
def add_stem_combinations(set_of_letters, sorted_word) | |
set_of_letters.each do |letter| | |
current_stem = sorted_word.sub(letter, '') | |
$stems_list[current_stem] = [] if !$stems_list.include?(current_stem) | |
$stems_list[current_stem] << letter | |
end | |
end | |
ARGF.each_line do |word| | |
word.strip! | |
next if word.length != WORD_LENGTH | |
sorted_word = word.chars.sort.join | |
unique_letters = word.chars.sort.uniq | |
next if word_exists?(sorted_word) | |
$unique_word_list.push(sorted_word) | |
add_stem_combinations(unique_letters, sorted_word) | |
end | |
$stems_list.select! { |key, value| $stems_list[key] = value.sort if value.size >= cut_off.to_i } | |
result = $stems_list.each.sort_by { |_, value| value.size }.reverse | |
result.each { |stem, valid_letters_set| puts "#{stem} => #{valid_letters_set.size}, #{valid_letters_set} " } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment