jtyoui.algorithm package

Submodules

jtyoui.algorithm.MatchingAlgorithm module

匹配算法

class jtyoui.algorithm.MatchingAlgorithm.FMMA(ls, sort=False)[源代码]

基类:object

FMMA(Forward Maximum Matching Algorithms)正向最大匹配算法

>>> r = FMMA(ls=['我们', '野生', '动物园', '在野'], sort=True)
>>> print(r.cut('我们在野生动物园玩', 3))
>>> # ['我们', '在野', '生', '动物园', '玩']
cut(line, max_length)[源代码]

输入一行字符串,最大按照max_length拆分

class jtyoui.algorithm.MatchingAlgorithm.RMMA(ls, sort=False)[源代码]

基类:object

RMMA(Reverse Maximum Matching Algorithms)逆向最大匹配算法

>>> r = RMMA(ls=['我们', '野生', '动物园', '在野'], sort=True)
>>> print(r.cut('我们在野生动物园玩', 3))
>>> # ['我们', '在', '野生', '动物园', '玩']
cut(line, max_length)[源代码]

输入一行字符串,最大按照max_length拆分

jtyoui.algorithm.MatchingAlgorithm.kmp(string, str_)[源代码]

KMP(The Knuth-Morris-Pratt Algorithm)无回溯串匹配算法

>>> print(kmp('我们在野生动物园玩', '动物园'))
jtyoui.algorithm.MatchingAlgorithm.max_sub_array(ls: list) → tuple[源代码]

求解最大子数组

>>> print(max_sub_array([5, 4, -12, 1, 3, -1, 4, 1, -6]))
参数:ls – 数字类列表
返回:(起始位置,结束位置,最大值)

jtyoui.algorithm.ReplaceAlgorithm module

jtyoui.algorithm.ReplaceAlgorithm.map_replace(str_: str, key: [<class 'list'>, <class 'str'>] = None, value: [<class 'list'>, <class 'str'>] = None, maps: dict = None) → str[源代码]

映射替换,最好使用maps字典映射

>>> print(map_replace('[中国]', '[]', '【】')) #【中国】
>>> print(map_replace('[中国]', maps={'[': '【', ']': '】'})) #【中国】
参数:
  • str – 字符串
  • key – 替换字符
  • value – 被替换的字符
  • maps – 字符映射
返回:

替换完毕的字符串

jtyoui.algorithm.SearchAlgorithm module

搜索算法

二分查找算法

>>> s = [1, 2, 3, 4, 5, 6, 10, 7]
>>> print(binary_search(s, 7, True))
参数:
  • ls – 列表。
  • x – 被查找的数。
  • sort – 是否要启动排序,False表示不启动排序,默认是不启动。
返回:

找到返回True,反之亦然

jtyoui.algorithm.SortAlgorithm module

排序算法

jtyoui.algorithm.SortAlgorithm.bubbled_sort(ls)[源代码]

冒泡算法

>>> import random
>>> import time
>>> s = []
>>> for _ in range(100):
        jr = random.randint(0, 1000)
        s.append(jr)
>>> start = time.time()
>>> bs = bubbled_sort(s)
>>> print(bs)
>>> print(time.time() - start)

jtyoui.algorithm.TreeAlgorithm module

class jtyoui.algorithm.TreeAlgorithm.Tree(value=None, parent=None)[源代码]

基类:object

创建一颗树

>>> tree = Tree(value='Root')
>>> for d in data:
        t = Tree(value=d, parent=tree)
        tree.add_child(t)
add_child(node)[源代码]

增加节点(孩子)

参数:node – 节点
node_parent_value()[源代码]

知道一个节点,打印该节点的所有值(路径)

返回:返回该节点路径上的所有值
search_tree(value: str, ls)[源代码]

多叉搜索树

>>> tree_object = []
>>> ts.search_tree('g', tree_object)
>>> print(tree_object)
参数:
  • value – 树上一个值
  • ls – 树集合
search_tree_value(value: str)[源代码]

搜索树的路径

参数:value – 树上的一个值
返回:树支的路径
jtyoui.algorithm.TreeAlgorithm.dict_create_tree(data: dict, tree: jtyoui.algorithm.TreeAlgorithm.Tree = <jtyoui.algorithm.TreeAlgorithm.Tree object>)[源代码]
创建下面的树结构
a

b c d

e f g |g h| k m

>>> ds = {'a': {'b': ['e', 'f', 'g'], 'c': ['g', 'h'], 'd': ['k', 'm']}}
>>> ts = dict_create_tree(ds)
>>> print(ts)
参数:
  • data – 创建树型结构,对照上面,例如:ds = {‘a’: {‘b’: [‘e’, ‘f’, ‘g’], ‘c’: [‘g’, ‘h’], ‘d’: [‘k’, ‘m’]}}
  • tree – 默认为上一层树结构,不需要传入
返回:

一颗自动带有root根目录的树结构

jtyoui.algorithm.TreeAlgorithm.tree()[源代码]

创建一颗简单树

>>> x = tree()
>>> x['a']['b'] = 1
返回:

Module contents