Last active
September 21, 2017 04:43
-
-
Save KevinSia/e9f22ebae5984e7489ac14296b86d24e to your computer and use it in GitHub Desktop.
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
def canonical(word) | |
word.downcase.chars.sort | |
end | |
def is_anagram?(word1, word2) | |
canonical(word1) == canonical(word2) | |
end | |
puts "One word" | |
word1 = gets.chomp | |
puts "Another word" | |
word2 = gets.chomp | |
puts "#{word1} and #{word2} are #{'not ' if !is_anagram?(word1, word2)}anagrams" |
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
def canonical(word) | |
word.downcase.chars.sort | |
end | |
def is_anagram?(word1, word2) | |
canonical(word1) == canonical(word2) | |
end | |
# def anagrams_for?(word, array) | |
# result = [] | |
# array.each do |arr_word| | |
# result << word if is_anagram?(word, arr_word) | |
# end | |
# result | |
# end | |
def anagrams_for?(word, array) | |
array.select { |element| is_anagram?(word, element) } | |
end | |
# dictionary = [] | |
# puts "Please give a word to fill the dictionary" | |
# input = gets.chomp | |
# until input == "" | |
# arr << input | |
# puts "Please put another word in the dictionary" | |
# input = gets.chomp | |
# end | |
# puts "Put a word to match anagrams in the dictionary" | |
# word = gets.chomp | |
dictionary = ['acres', 'cares', 'Cesar', 'races', 'smelt', 'melts', 'etlsm'] | |
word = 'acres' | |
anagrams_for?(word, dictionary) # => ['acres', 'cares', 'Cesar', 'races'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment