예제 #1
0
def generate_word_square(n: int, letters: str) -> list:
    assert n > 0, "Invalid square"
    words = get_anagrams(n, letters)
    # Trie - https://en.wikipedia.org/wiki/Trie
    t = Trie(words)
    result = recurse_generate([], t, n, 0)
    print(result)
    return result
예제 #2
0
    def post(self):
        time = datetime.strptime(session['time'], "%Y-%m-%dT%H:%M:%S.%f")
        if time < datetime.now() - timedelta(minutes=1):
            return {'status': 'expired'}

        args = submit_parser.parse_args()
        answer = args['answer']
        if session['round'] == 'letters':
            anagrams = get_anagrams(session['letters'])
            opponents_answer = choice(anagrams) if anagrams else ''
            response = check_letters(answer, opponents_answer)
        else:
            numbers = session['numbers']
            target = session['target']
            target_away = randint(0, 9)
            solutions = Solver(numbers, target, target_away).solve()
            response = check_numbers(answer, solutions[0])
        session.clear()
        return response
예제 #3
0
def test_get_anagrams():
    word = 'lumber'
    expected = 6

    assert len(anagram.get_anagrams(word)) == expected
예제 #4
0
 def test_invalid_number_input(self):
     with self.assertRaises(AssertionError):
         get_anagrams(-1, "foo")
예제 #5
0
 def test_get_anagrams(self):
     n = 3
     res = get_anagrams(n, "bar")
     self.assertGreater(len(res), 0)
     for r in res:
         self.assertEqual(len(r), n)
예제 #6
0
 def test_invalid_square_length(self):
     with self.assertRaises(AssertionError):
         get_anagrams(100, "bar")
def store_anagrams(filename):
    shelf = shelve.open(filename, 'c')
    anagram_map = anagram.get_anagrams('words.txt')
    for words, anagrams in anagram_map.iteritems():
        shelf[words] = anagrams
    shelf.close()
예제 #8
0
 def test_anagrams_return_words_different_len(self):
     result = anagram.get_anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])
     assert.equals(result, ['carer', 'racer'])
예제 #9
0
 def test_anagrams_(self):
     result = anagram.get_anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada'])
     assert.equals(result, ['aabb', 'bbaa'])
예제 #10
0
 def test_anagrams_return_two_empty_result(self):
     result = anagram.get_anagrams('laser', ['lazing', 'lazy', 'lacer'])
     assert.equals(result, [])