-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram_detector.rb
96 lines (72 loc) · 2.93 KB
/
anagram_detector.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#JumpstartLab warmup-exercise #31
require "rspec"
class AnagramDetector
attr_accessor :test_word, :test_array
def initialize(test_word, test_array)
@test_word = test_word
@test_array = test_array
end
def return_anagrams
downcase_test_word
downcase_test_array
delete_word_from_test_array_if_it_is_not_an_anagram
end
def downcase_test_word
@test_word = test_word.downcase
end
def downcase_test_array
@test_array.map! {|word| word = word.downcase}
end
def delete_word_from_test_array_if_it_is_not_an_anagram
@test_array.delete_if {|word| word.each_char.sort != @test_word.each_char.sort}
end
end
describe "AnagramDetector" do
RSpec.configure do |config|
config.color_enabled = true
end
before :each do
@anagram_detector = AnagramDetector.new("listen", %w(enlists google inlets banana))
end
it "should exist" do
expect(AnagramDetector.is_a? Class).to eq(true)
end
it "should take in a test word and array of words upon creation" do
expect(@anagram_detector).not_to be_nil
end
it "should respond to the test_word attribute" do
expect(@anagram_detector).to respond_to(:test_word)
end
it "should respond to the test_array attribute" do
expect(@anagram_detector).to respond_to(:test_array)
end
it "should have a method downcase_test_word" do
expect(@anagram_detector).to respond_to(:downcase_test_word)
end
it "should downcase the test word via downcase_test_word" do
@anagram_detector_with_capalized_test_word = AnagramDetector.new("Word", %w(dowr, door))
expect(@anagram_detector_with_capalized_test_word.downcase_test_word).to eq("word")
end
it "should have a method downcase_test_array" do
expect(@anagram_detector).to respond_to(:downcase_test_array)
end
it "should have a downcase all the words in the test array via downcase_test_array" do
@anagram_detector_with_capalized_test_array = AnagramDetector.new("word", %w(Dowr, doOr))
expect(@anagram_detector_with_capalized_test_array.downcase_test_array).to eq(%w(dowr, door))
end
it "should have a method called delete_word_if_it_is_not_an_anagram" do
expect(@anagram_detector).to respond_to(:delete_word_from_test_array_if_it_is_not_an_anagram)
end
it "should remove_words that are not anagrams via delete_word_if_it_is_not_an_anagram" do
expect(@anagram_detector.delete_word_from_test_array_if_it_is_not_an_anagram).to eq(["inlets"])
end
it "should have a method return anagrams" do
expect(@anagram_detector).to respond_to(:return_anagrams)
end
it "should have method return_anagrams that calls the appropriate methods and returns the matching anagrams" do
@difficult_anagram_detector = AnagramDetector.new("Word", %w(Dowr doOr roDw))
expect(@difficult_anagram_detector.return_anagrams).to eq(["dowr", "rodw"])
end
end
anagram_detector = AnagramDetector.new("listen", %w(enlists google inlets banana)).return_anagrams
p anagram_detector # => ["inlets"]