ruby - anagram
This commit is contained in:
parent
eb41f1b49f
commit
0923955efb
37
ruby/anagram/README.md
Normal file
37
ruby/anagram/README.md
Normal file
@ -0,0 +1,37 @@
|
||||
# Anagram
|
||||
|
||||
Given a word and a list of possible anagrams, select the correct sublist.
|
||||
|
||||
Given `"listen"` and a list of candidates like `"enlists" "google"
|
||||
"inlets" "banana"` the program should return a list containing
|
||||
`"inlets"`.
|
||||
|
||||
* * * *
|
||||
|
||||
For installation and learning resources, refer to the
|
||||
[Ruby resources page](http://exercism.io/languages/ruby/resources).
|
||||
|
||||
For running the tests provided, you will need the Minitest gem. Open a
|
||||
terminal window and run the following command to install minitest:
|
||||
|
||||
gem install minitest
|
||||
|
||||
If you would like color output, you can `require 'minitest/pride'` in
|
||||
the test file, or note the alternative instruction, below, for running
|
||||
the test file.
|
||||
|
||||
Run the tests from the exercise directory using the following command:
|
||||
|
||||
ruby anagram_test.rb
|
||||
|
||||
To include color from the command line:
|
||||
|
||||
ruby -r minitest/pride anagram_test.rb
|
||||
|
||||
|
||||
## Source
|
||||
|
||||
Inspired by the Extreme Startup game [https://github.com/rchatley/extreme_startup](https://github.com/rchatley/extreme_startup)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
14
ruby/anagram/anagram.rb
Normal file
14
ruby/anagram/anagram.rb
Normal file
@ -0,0 +1,14 @@
|
||||
# Anagram class
|
||||
class Anagram
|
||||
def initialize(word)
|
||||
@word = word.downcase
|
||||
@sorted = @word.chars.sort.join
|
||||
end
|
||||
|
||||
def match(candidates)
|
||||
candidates.map do |candidate|
|
||||
sorted_candidate = candidate.downcase.chars.sort.join
|
||||
candidate if @sorted == sorted_candidate && @word != candidate.downcase
|
||||
end.compact.sort
|
||||
end
|
||||
end
|
101
ruby/anagram/anagram_test.rb
Normal file
101
ruby/anagram/anagram_test.rb
Normal file
@ -0,0 +1,101 @@
|
||||
require 'minitest/autorun'
|
||||
require_relative 'anagram'
|
||||
|
||||
# Common test data version: 1.4.0 baaf092
|
||||
class AnagramTest < Minitest::Test
|
||||
def test_no_matches
|
||||
# skip
|
||||
detector = Anagram.new('diaper')
|
||||
anagrams = detector.match(["hello", "world", "zombies", "pants"])
|
||||
expected = []
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_detects_two_anagrams
|
||||
# skip
|
||||
detector = Anagram.new('master')
|
||||
anagrams = detector.match(["stream", "pigeon", "maters"])
|
||||
expected = ["maters", "stream"]
|
||||
assert_equal expected, anagrams.sort
|
||||
end
|
||||
|
||||
def test_does_not_detect_anagram_subsets
|
||||
# skip
|
||||
detector = Anagram.new('good')
|
||||
anagrams = detector.match(["dog", "goody"])
|
||||
expected = []
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_detects_anagram
|
||||
# skip
|
||||
detector = Anagram.new('listen')
|
||||
anagrams = detector.match(["enlists", "google", "inlets", "banana"])
|
||||
expected = ["inlets"]
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_detects_three_anagrams
|
||||
# skip
|
||||
detector = Anagram.new('allergy')
|
||||
anagrams = detector.match(["gallery", "ballerina", "regally", "clergy", "largely", "leading"])
|
||||
expected = ["gallery", "largely", "regally"]
|
||||
assert_equal expected, anagrams.sort
|
||||
end
|
||||
|
||||
def test_does_not_detect_non_anagrams_with_identical_checksum
|
||||
# skip
|
||||
detector = Anagram.new('mass')
|
||||
anagrams = detector.match(["last"])
|
||||
expected = []
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_detects_anagrams_case_insensitively
|
||||
# skip
|
||||
detector = Anagram.new('Orchestra')
|
||||
anagrams = detector.match(["cashregister", "Carthorse", "radishes"])
|
||||
expected = ["Carthorse"]
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_detects_anagrams_using_case_insensitive_subject
|
||||
# skip
|
||||
detector = Anagram.new('Orchestra')
|
||||
anagrams = detector.match(["cashregister", "carthorse", "radishes"])
|
||||
expected = ["carthorse"]
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_detects_anagrams_using_case_insensitive_possible_matches
|
||||
# skip
|
||||
detector = Anagram.new('orchestra')
|
||||
anagrams = detector.match(["cashregister", "Carthorse", "radishes"])
|
||||
expected = ["Carthorse"]
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_does_not_detect_a_anagram_if_the_original_word_is_repeated
|
||||
# skip
|
||||
detector = Anagram.new('go')
|
||||
anagrams = detector.match(["go Go GO"])
|
||||
expected = []
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_anagrams_must_use_all_letters_exactly_once
|
||||
# skip
|
||||
detector = Anagram.new('tapper')
|
||||
anagrams = detector.match(["patter"])
|
||||
expected = []
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
|
||||
def test_words_are_not_anagrams_of_themselves_case_insensitive
|
||||
# skip
|
||||
detector = Anagram.new('BANANA')
|
||||
anagrams = detector.match(["BANANA", "Banana", "banana"])
|
||||
expected = []
|
||||
assert_equal expected, anagrams
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user