Пример #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), [])