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