Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yclim95/2186869f09cba3151e6f to your computer and use it in GitHub Desktop.
Save yclim95/2186869f09cba3151e6f to your computer and use it in GitHub Desktop.
# 1. - Read the word in the array
# 1.1 Check the length of words(array) are the same
# 1.2 Check capatilization
# 1.3 Break the words into individual array
# 1.4 Sort the alpabatical order (both, array&words)
# 1.5 Compare the list
# 2. - print out the same words that = array[word]
# include anagram_for method here
def canonical(word1,word2)
word1==word2
end
def is_anagram?(word1,word2)
return false if (word1.length != word2.length)
word1_temp=word1.downcase.split("").sort!
word2_temp=word2.downcase.split("").sort!
return true if canonical(word1_temp, word2_temp)
false
end
def anagrams_for(word,array_word)
list=[]
empty_list=[]
array_word.each{|word_in_list|
if is_anagram?(word,word_in_list)
#print word_in_list + " "
list << word_in_list
end
}
puts list.inspect
end
# initialization
dictionary=['acres','cares','Cesar','races','smelt','melts','etlsm']
#If the input word happens to be in the dictionary, it should be in the returned array too.
#The list should also be case-insensitive
anagrams_for('acres',dictionary)
puts anagrams_for('Acres',dictionary)
puts anagrams_for('Cesar',dictionary)
#Although "sacre" is not *in* the dictionary, several words in the dictionary are anagra,s of "sacre"
puts anagrams_for('sacre',dictionary)
#Neither the input word nor the words in the dictionary need to be valid English words
#puts anagrams_for('etlsm',dictionary)
puts anagrams_for('unicorn',dictionary) # => []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment