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