Last active
December 22, 2015 00:18
-
-
Save follesoe/6387816 to your computer and use it in GitHub Desktop.
Find variable length anagrams based on dictionary
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
words = Hash.new([]) | |
File.open("words.txt", "r") do |file| | |
while line = file.gets | |
word = line.chomp.downcase | |
words[word.split('').sort!.join('')] += [word] | |
end | |
end | |
File.open("names.txt", "r") do |file| | |
while line = file.gets | |
names = line.downcase.chomp.split(' ') | |
names.each do |name| | |
puts "Navn: #{name}" | |
combinations = [] | |
for counter in 3..name.length | |
name.split('').combination(counter).each do |comb| | |
combinations << comb.sort! | |
end | |
end | |
combinations.each do |combo| | |
words[combo.join('')].each do |anagram| | |
puts "\t#{anagram}" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maskin