예제 #1
0
def test_anagram():
    tests_count = 10000
    word_length = 5
    anagrams_count = 20
    words_count = 20
    chars = string.ascii_uppercase + string.ascii_lowercase
    for _ in xrange(0, tests_count):

        def _same_letters(word1, word2):
            result = ''.join(sorted(word1.lower())) == ''.join(
                sorted(word2.lower()))
            return result

        word = ''.join(random.choice(chars) for _ in xrange(word_length))

        anagrams = [
            ''.join(random.sample(word, len(word)))
            for _ in xrange(0, anagrams_count)
        ]
        random_words = [
            ''.join(random.choice(chars) for _ in xrange(word_length))
            for _ in xrange(0, words_count)
        ]
        random_words = [w for w in random_words if not _same_letters(w, word)]

        result = anagram.find_anagrams(word, anagrams + random_words)
        assert len(result) == len(anagrams), "%s, %s, %s" % (result, anagrams,
                                                             random_words)
예제 #2
0
 def test_detects_three_anagrams(self):
     candidates = [
         "gallery", "ballerina", "regally", "clergy", "largely", "leading"
     ]
     self.assertCountEqual(
         find_anagrams("allergy", candidates),
         ["gallery", "regally", "largely"])
예제 #3
0
 def test_detects_three_anagrams(self):
     candidates = [
         "gallery", "ballerina", "regally", "clergy", "largely", "leading"
     ]
     self.assertEqual(
         find_anagrams("allergy", candidates),
         ["gallery", "regally", "largely"])
예제 #4
0
 def test_does_not_detect_an_anagram_if_the_original_word_is_repeated(self):
     candidates = ["go Go GO"]
     expected = []
     assert find_anagrams("go", candidates) == expected
예제 #5
0
 def test_does_not_detect_non_anagrams_with_identical_checksum(self):
     candidates = ["last"]
     expected = []
     assert find_anagrams("mass", candidates) == expected
예제 #6
0
 def test_detects_three_anagrams(self):
     candidates = [
         "gallery", "ballerina", "regally", "clergy", "largely", "leading"
     ]
     expected = ["gallery", "regally", "largely"]
     assert find_anagrams("allergy", candidates) == expected
예제 #7
0
 def test_does_not_detect_anagram_subsets(self):
     candidates = ["dog", "goody"]
     expected = []
     assert find_anagrams("good", candidates) == expected
예제 #8
0
 def test_detects_two_anagrams(self):
     candidates = ["stream", "pigeon", "maters"]
     self.assertEqual(
         find_anagrams("master", candidates), ["stream", "maters"])
예제 #9
0
 def test_anagrams_must_use_all_letters_exactly_once(self):
     candidates = ["patter"]
     expected = []
     self.assertCountEqual(find_anagrams("tapper", candidates), expected)
예제 #10
0
 def test_detects_multiple_anagrams_with_different_case(self):
     candidates = ["Eons", "ONES"]
     expected = ["Eons", "ONES"]
     self.assertCountEqual(find_anagrams("nose", candidates), expected)
예제 #11
0
 def test_anagrams_must_use_all_letters_exactly_once(self):
     self.assertEqual(find_anagrams("tapper", ["patter"]), [])
예제 #12
0
 def test_does_not_detect_a_anagram_if_the_original_word_is_repeated(self):
     self.assertEqual(find_anagrams("go", ["go Go GO"]), [])
예제 #13
0
 def test_detects_anagrams_using_case_insensitive_possible_matches(self):
     candidates = ["cashregister", "Carthorse", "radishes"]
     self.assertEqual(
         find_anagrams("orchestra", candidates), ["Carthorse"])
예제 #14
0
 def test_detects_anagrams_using_case_insensitive_subject(self):
     candidates = ["cashregister", "carthorse", "radishes"]
     self.assertEqual(
         find_anagrams("Orchestra", candidates), ["carthorse"])
예제 #15
0
 def test_does_not_detect_non_anagrams_with_identical_checksum(self):
     self.assertEqual(find_anagrams("mass", ["last"]), [])
예제 #16
0
 def test_detects_anagram(self):
     candidates = ["enlists", "google", "inlets", "banana"]
     self.assertEqual(find_anagrams("listen", candidates), ["inlets"])
예제 #17
0
 def test_capital_word_is_not_own_anagram(self):
     self.assertEqual(find_anagrams("BANANA", ["Banana"]), [])
예제 #18
0
 def test_does_not_detect_anagram_subsets(self):
     candidates = ["dog", "goody"]
     expected = []
     self.assertCountEqual(find_anagrams("good", candidates), expected)
예제 #19
0
 def test_no_matches(self):
     candidates = ["hello", "world", "zombies", "pants"]
     self.assertEqual(find_anagrams("diaper", candidates), [])
예제 #20
0
 def test_detects_anagrams_using_case_insensitive_possible_matches(self):
     candidates = ["cashregister", "Carthorse", "radishes"]
     expected = ["Carthorse"]
     self.assertCountEqual(find_anagrams("orchestra", candidates), expected)
예제 #21
0
def test_find_anagrams():
    ans = ['emit', 'time', 'mite']
    valid_words = ['emit', 'time', 'mite', 'car']
    assert ans == list(find_anagrams('time', valid_words))
예제 #22
0
 def test_words_other_than_themselves_can_be_anagrams(self):
     candidates = ["Listen", "Silent", "LISTEN"]
     expected = ["Silent"]
     self.assertCountEqual(find_anagrams("LISTEN", candidates), expected)
예제 #23
0
def test_find_anagrams_empty_valid_words():
    valid_words = []
    assert [] == list(find_anagrams('time', valid_words))
예제 #24
0
 def test_detects_two_anagrams(self):
     candidates = ["stream", "pigeon", "maters"]
     expected = ["stream", "maters"]
     assert find_anagrams("master", candidates) == expected
예제 #25
0
def test_find_anagrams_missing_word():
    valid_words = ['emit', 'time', 'mite', 'car']
    with pytest.raises(ValueError):
        list(find_anagrams(None, valid_words))
예제 #26
0
 def test_detects_anagram(self):
     candidates = ["enlists", "google", "inlets", "banana"]
     expected = ["inlets"]
     assert find_anagrams("listen", candidates) == expected
예제 #27
0
def test_find_anagrams_missing_valid_words():
    with pytest.raises(ValueError):
        list(find_anagrams('time', None))
예제 #28
0
 def test_detects_multiple_anagrams_with_different_case(self):
     candidates = ["Eons", "ONES"]
     expected = ["Eons", "ONES"]
     assert find_anagrams("nose", candidates) == expected
예제 #29
0
 def test_anagrams_must_use_all_letters_exactly_once(self):
     candidates = ["patter"]
     expected = []
     assert find_anagrams("tapper", candidates) == expected
예제 #30
0
 def test_detects_anagrams_using_case_insensitive_subject(self):
     candidates = ["cashregister", "carthorse", "radishes"]
     expected = ["carthorse"]
     assert find_anagrams("Orchestra", candidates) == expected
예제 #31
0
 def test_base(self):
     anagrams = anagram.find_anagrams(
         'abc', ('abc', 'bcd', 'test', 'cba', 'ab'))
     self.assertSetEqual(set(anagrams), {'abc', 'cba'})
예제 #32
0
 def test_no_matches(self):
     candidates = ["hello", "world", "zombies", "pants"]
     expected = []
     assert find_anagrams("diaper", candidates) == expected
예제 #33
0
 def test_does_not_detect_anagram_subsets(self):
     self.assertEqual(find_anagrams("good", ["dog", "goody"]), [])
예제 #34
0
 def test_words_other_than_themselves_can_be_anagrams(self):
     candidates = ["Listen", "Silent", "LISTEN"]
     expected = ["Silent"]
     assert find_anagrams("LISTEN", candidates) == expected
예제 #35
0
def main(word, filepath):
    valid_words = load_words_file(filepath)
    for anagram in find_anagrams(word, valid_words):
        print anagram
예제 #36
0
 def test_detects_two_anagrams(self):
     candidates = ["stream", "pigeon", "maters"]
     expected = ["stream", "maters"]
     self.assertCountEqual(find_anagrams("master", candidates), expected)
예제 #37
0
 def test_does_not_detect_an_anagram_if_the_original_word_is_repeated(self):
     self.assertEqual(find_anagrams("go", ["go Go GO"]), [])
예제 #38
0
 def test_detects_anagram(self):
     candidates = ["enlists", "google", "inlets", "banana"]
     expected = ["inlets"]
     self.assertCountEqual(find_anagrams("listen", candidates), expected)
예제 #39
0
 def test_anagrams_must_use_all_letters_exactly_once(self):
     self.assertEqual(find_anagrams("tapper", ["patter"]), [])
예제 #40
0
 def test_does_not_detect_non_anagrams_with_identical_checksum(self):
     candidates = ["last"]
     expected = []
     self.assertCountEqual(find_anagrams("mass", candidates), expected)
예제 #41
0
 def test_detects_two_anagrams(self):
     candidates = ["lemons", "cherry", "melons"]
     expected = ["lemons", "melons"]
     self.assertCountEqual(find_anagrams("solemn", candidates), expected)
예제 #42
0
 def test_does_not_detect_an_anagram_if_the_original_word_is_repeated(self):
     candidates = ["go Go GO"]
     expected = []
     self.assertCountEqual(find_anagrams("go", candidates), expected)
예제 #43
0
 def test_words_are_not_anagrams_of_themselves(self):
     candidates = ["BANANA"]
     expected = []
     self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
예제 #44
0
 def test_words_are_not_anagrams_of_themselves_case_insensitive(self):
     candidates = ["BANANA", "Banana", "banana"]
     expected = []
     self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
예제 #45
0
 def test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different(
     self, ):
     candidates = ["banana"]
     expected = []
     self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
예제 #46
0
 def test_no_matches(self):
     candidates = ["hello", "world", "zombies", "pants"]
     expected = []
     self.assertCountEqual(find_anagrams("diaper", candidates), expected)
예제 #47
0
 def test_words_are_not_anagrams_of_themselves_case_insensitive(self):
     candidates = ["BANANA", "Banana", "banana"]
     self.assertEqual(find_anagrams("BANANA", candidates), [])