当前位置: 首页>>代码示例>>Python>>正文


Python Trie.suggestions方法代码示例

本文整理汇总了Python中trie.Trie.suggestions方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.suggestions方法的具体用法?Python Trie.suggestions怎么用?Python Trie.suggestions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.suggestions方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TrieTestCase

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import suggestions [as 别名]
class TrieTestCase(unittest.TestCase):
    """ Define test cases for Trie class """
    def setUp(self):
        """ Setup env before each test """
        self.trie = Trie()

    def tearDown(self):
        """ Tear down env after each test """
        pass

    def test_insert(self):
        """
        Test insert of the trie and assert if the
        word was added successfully
        """
        word = "bar"
        self.trie.insert(word)
        expected_result = [word]
        actual_result = self.trie.suggestions(word)
        self.assertEqual(expected_result, actual_result)

    def test_suggestion_when_prefix_valid(self):
        """
        Test suggestions of the trie and assert if all the word
        mathcing the prefix are returned. There should be 2 matching
        words for a prefix 'bar'
        """
        words = ["bar", "barcelona", "foo", "foobar"]
        valid_key = "bar"
        for word in words:
            self.trie.insert(word)

        expected_result = ["bar", "barcelona"]
        actual_result = self.trie.suggestions(valid_key)
        self.assertEqual(expected_result, actual_result)

    def test_suggestion_when_prefix_invalid(self):
        """
        Test suggestions of the trie and assert if all the word
        mathcing the prefix are returned. There should be no matching
        words for a prefix 'foo'
        """
        words = ["bar", "barcelona"]
        invalid_key = "foo"
        for word in words:
            self.trie.insert(word)

        expected_result = []
        actual_result = [invalid_key] \
            if invalid_key in self.trie.suggestions(invalid_key) else []
        self.assertEqual(expected_result, actual_result)
开发者ID:sshukla31,项目名称:misc_algos,代码行数:53,代码来源:test_trie.py


注:本文中的trie.Trie.suggestions方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。