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)
def test_detects_three_anagrams(self): candidates = [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ] self.assertCountEqual( find_anagrams("allergy", candidates), ["gallery", "regally", "largely"])
def test_detects_three_anagrams(self): candidates = [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ] self.assertEqual( find_anagrams("allergy", candidates), ["gallery", "regally", "largely"])
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
def test_does_not_detect_non_anagrams_with_identical_checksum(self): candidates = ["last"] expected = [] assert find_anagrams("mass", candidates) == expected
def test_detects_three_anagrams(self): candidates = [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ] expected = ["gallery", "regally", "largely"] assert find_anagrams("allergy", candidates) == expected
def test_does_not_detect_anagram_subsets(self): candidates = ["dog", "goody"] expected = [] assert find_anagrams("good", candidates) == expected
def test_detects_two_anagrams(self): candidates = ["stream", "pigeon", "maters"] self.assertEqual( find_anagrams("master", candidates), ["stream", "maters"])
def test_anagrams_must_use_all_letters_exactly_once(self): candidates = ["patter"] expected = [] self.assertCountEqual(find_anagrams("tapper", candidates), expected)
def test_detects_multiple_anagrams_with_different_case(self): candidates = ["Eons", "ONES"] expected = ["Eons", "ONES"] self.assertCountEqual(find_anagrams("nose", candidates), expected)
def test_anagrams_must_use_all_letters_exactly_once(self): self.assertEqual(find_anagrams("tapper", ["patter"]), [])
def test_does_not_detect_a_anagram_if_the_original_word_is_repeated(self): self.assertEqual(find_anagrams("go", ["go Go GO"]), [])
def test_detects_anagrams_using_case_insensitive_possible_matches(self): candidates = ["cashregister", "Carthorse", "radishes"] self.assertEqual( find_anagrams("orchestra", candidates), ["Carthorse"])
def test_detects_anagrams_using_case_insensitive_subject(self): candidates = ["cashregister", "carthorse", "radishes"] self.assertEqual( find_anagrams("Orchestra", candidates), ["carthorse"])
def test_does_not_detect_non_anagrams_with_identical_checksum(self): self.assertEqual(find_anagrams("mass", ["last"]), [])
def test_detects_anagram(self): candidates = ["enlists", "google", "inlets", "banana"] self.assertEqual(find_anagrams("listen", candidates), ["inlets"])
def test_capital_word_is_not_own_anagram(self): self.assertEqual(find_anagrams("BANANA", ["Banana"]), [])
def test_does_not_detect_anagram_subsets(self): candidates = ["dog", "goody"] expected = [] self.assertCountEqual(find_anagrams("good", candidates), expected)
def test_no_matches(self): candidates = ["hello", "world", "zombies", "pants"] self.assertEqual(find_anagrams("diaper", candidates), [])
def test_detects_anagrams_using_case_insensitive_possible_matches(self): candidates = ["cashregister", "Carthorse", "radishes"] expected = ["Carthorse"] self.assertCountEqual(find_anagrams("orchestra", candidates), expected)
def test_find_anagrams(): ans = ['emit', 'time', 'mite'] valid_words = ['emit', 'time', 'mite', 'car'] assert ans == list(find_anagrams('time', valid_words))
def test_words_other_than_themselves_can_be_anagrams(self): candidates = ["Listen", "Silent", "LISTEN"] expected = ["Silent"] self.assertCountEqual(find_anagrams("LISTEN", candidates), expected)
def test_find_anagrams_empty_valid_words(): valid_words = [] assert [] == list(find_anagrams('time', valid_words))
def test_detects_two_anagrams(self): candidates = ["stream", "pigeon", "maters"] expected = ["stream", "maters"] assert find_anagrams("master", candidates) == expected
def test_find_anagrams_missing_word(): valid_words = ['emit', 'time', 'mite', 'car'] with pytest.raises(ValueError): list(find_anagrams(None, valid_words))
def test_detects_anagram(self): candidates = ["enlists", "google", "inlets", "banana"] expected = ["inlets"] assert find_anagrams("listen", candidates) == expected
def test_find_anagrams_missing_valid_words(): with pytest.raises(ValueError): list(find_anagrams('time', None))
def test_detects_multiple_anagrams_with_different_case(self): candidates = ["Eons", "ONES"] expected = ["Eons", "ONES"] assert find_anagrams("nose", candidates) == expected
def test_anagrams_must_use_all_letters_exactly_once(self): candidates = ["patter"] expected = [] assert find_anagrams("tapper", candidates) == expected
def test_detects_anagrams_using_case_insensitive_subject(self): candidates = ["cashregister", "carthorse", "radishes"] expected = ["carthorse"] assert find_anagrams("Orchestra", candidates) == expected
def test_base(self): anagrams = anagram.find_anagrams( 'abc', ('abc', 'bcd', 'test', 'cba', 'ab')) self.assertSetEqual(set(anagrams), {'abc', 'cba'})
def test_no_matches(self): candidates = ["hello", "world", "zombies", "pants"] expected = [] assert find_anagrams("diaper", candidates) == expected
def test_does_not_detect_anagram_subsets(self): self.assertEqual(find_anagrams("good", ["dog", "goody"]), [])
def test_words_other_than_themselves_can_be_anagrams(self): candidates = ["Listen", "Silent", "LISTEN"] expected = ["Silent"] assert find_anagrams("LISTEN", candidates) == expected
def main(word, filepath): valid_words = load_words_file(filepath) for anagram in find_anagrams(word, valid_words): print anagram
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"]), [])
def test_detects_anagram(self): candidates = ["enlists", "google", "inlets", "banana"] expected = ["inlets"] self.assertCountEqual(find_anagrams("listen", candidates), expected)
def test_does_not_detect_non_anagrams_with_identical_checksum(self): candidates = ["last"] expected = [] self.assertCountEqual(find_anagrams("mass", candidates), expected)
def test_detects_two_anagrams(self): candidates = ["lemons", "cherry", "melons"] expected = ["lemons", "melons"] self.assertCountEqual(find_anagrams("solemn", candidates), expected)
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)
def test_words_are_not_anagrams_of_themselves(self): candidates = ["BANANA"] expected = [] self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
def test_words_are_not_anagrams_of_themselves_case_insensitive(self): candidates = ["BANANA", "Banana", "banana"] expected = [] self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
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)
def test_no_matches(self): candidates = ["hello", "world", "zombies", "pants"] expected = [] self.assertCountEqual(find_anagrams("diaper", candidates), expected)
def test_words_are_not_anagrams_of_themselves_case_insensitive(self): candidates = ["BANANA", "Banana", "banana"] self.assertEqual(find_anagrams("BANANA", candidates), [])