要过的测试:
https://gist.github.com/qingwang/5c33fbcfc0e58cd9bbc4
需求:
Write a program that, given a word and a list of possible anagrams, selects the correct one(s).
Given `"listen"` and `%w(enlists google inlets banana)` the program should return "inlets".
我的做法:
class Anagram
def initialize(word)
@word = word
end
def match(list)
remove_identicals_in(list).select{ |item| anagram?(item) }
end
private
attr_accessor :word
def anagram?(other_word)
sort_chars(word) == sort_chars(other_word)
end
def remove_identicals_in(list)
list.reject{ |item| item.downcase == word.downcase }
end
def sort_chars(word)
word.downcase.chars.sort
end
end
求板砖,求毁三观...