diff --git a/README.md b/README.md index cb701c4..29f8f7b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # Algorithm >Notes and codes for learning algorithm and data structures :smiley: @@ -14,22 +15,24 @@ So,if you wannt to view the notes which contain latex math formulas and are in m # Index * [.](.) + * [README.md](./README.md) + * [backtracking](./backtracking) * [computationalMethod](./computationalMethod) + * [README.md](./computationalMethod/README.md) * [interplotion.py](./computationalMethod/interplotion.py) * [iteration.py](./computationalMethod/iteration.py) * [least_square.py](./computationalMethod/least_square.py) * [linear_equation.py](./computationalMethod/linear_equation.py) * [numerical_differential.py](./computationalMethod/numerical_differential.py) * [numerical_integration.py](./computationalMethod/numerical_integration.py) - * [README.md](./computationalMethod/README.md) * [solve-linear-by-iteration.py](./computationalMethod/solve-linear-by-iteration.py) * [tongyu_equation.py](./computationalMethod/tongyu_equation.py) * [vector_norm.py](./computationalMethod/vector_norm.py) * [dataStructure](./dataStructure) * [allOone](./dataStructure/allOone) + * [bTree.py](./dataStructure/bTree.py) * [binaryHeap.py](./dataStructure/binaryHeap.py) * [binaryTree.py](./dataStructure/binaryTree.py) - * [bTree.py](./dataStructure/bTree.py) * [graph](./dataStructure/graph) * [hashTable.py](./dataStructure/hashTable.py) * [huffman](./dataStructure/huffman) @@ -40,38 +43,44 @@ So,if you wannt to view the notes which contain latex math formulas and are in m * [polynomial.cpp](./dataStructure/polynomial.cpp) * [polynomial.py](./dataStructure/polynomial.py) * [redBlackTree.py](./dataStructure/redBlackTree.py) + * [redBlackTree0.py](./dataStructure/redBlackTree0.py) * [splayTree.py](./dataStructure/splayTree.py) * [trie.py](./dataStructure/trie.py) * [winnerTree.py](./dataStructure/winnerTree.py) * [divideAndConquer](./divideAndConquer) * [min_distance_of_n_points.py](./divideAndConquer/min_distance_of_n_points.py) * [docs](./docs) + * [README.md](./docs/README.md) * [algorithm-general.md](./docs/algorithm-general.md) * [b-tree.md](./docs/b-tree.md) * [fib-heap.md](./docs/fib-heap.md) * [graph.md](./docs/graph.md) * [hashTable.md](./docs/hashTable.md) - * [README.md](./docs/README.md) * [red-black-tree.md](./docs/red-black-tree.md) * [sort.md](./docs/sort.md) + * [src](./docs/src) * [tree.md](./docs/tree.md) * [dynamicProgramming](./dynamicProgramming) - * [lcs.hs](./dynamicProgramming/lcs.hs) + * [Vec2d.hs](./dynamicProgramming/Vec2d.hs) * [lcs.py](./dynamicProgramming/lcs.py) * [matrixChainMultiply.py](./dynamicProgramming/matrixChainMultiply.py) * [splitStripe.hs](./dynamicProgramming/splitStripe.hs) * [splitStripe.py](./dynamicProgramming/splitStripe.py) * [testVec2d.hs](./dynamicProgramming/testVec2d.hs) - * [Vec2d.hs](./dynamicProgramming/Vec2d.hs) * [math](./math) - * [arrangement.c](./math/arrangement.c) - * [cantor.c](./math/cantor.c) * [isPrime.py](./math/isPrime.py) * [num_weight.py](./math/num_weight.py) - * [README.md](./README.md) + * [permute_back_track.py](./math/permute_back_track.py) + * [permute_cantor.c](./math/permute_cantor.c) + * [permute_divide_and_conquer.py](./math/permute_divide_and_conquer.py) + * [permute_next_arrangement.c](./math/permute_next_arrangement.c) + * [primesLEn.hs](./math/primesLEn.hs) * [search](./search) * [8Astar.py](./search/8Astar.py) + * [BFS_knight.hs](./search/BFS_knight.hs) + * [binary_search.hs](./search/binary_search.hs) * [bloomFilter.py](./search/bloomFilter.py) + * [schedule.py](./search/schedule.py) * [sort](./sort) * [binaryTree.py](./sort/binaryTree.py) * [heapSort.py](./sort/heapSort.py) @@ -80,5 +89,15 @@ So,if you wannt to view the notes which contain latex math formulas and are in m * [select.py](./sort/select.py) * [shellSort.py](./sort/shellSort.py) * [string](./string) + * [KMP.py](./string/KMP.py) + * [README.md](./string/README.md) * [manacher.py](./string/manacher.py) * [markov.py](./string/markov.py) + * [rabin_karp.py](./string/rabin_karp.py) + * [src](./string/src) + * [sunday.py](./string/sunday.py) + * [utils](./utils) + * [config.py](./utils/config.py) + * [genReadme.py](./utils/genReadme.py) + * [headinfo.py](./utils/headinfo.py) + * [tree.py](./utils/tree.py) diff --git a/dataStructure/intervalTree.py b/dataStructure/intervalTree.py index 9ad88a9..2b975c0 100644 --- a/dataStructure/intervalTree.py +++ b/dataStructure/intervalTree.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : intervalTree.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-18 10:48 +# Description: +######################################################################### +''' from redBlackTree import redBlackTree from functools import total_ordering diff --git a/divideAndConquer/min_distance_of_n_points.py b/divideAndConquer/min_distance_of_n_points.py index 858c328..82620d4 100644 --- a/divideAndConquer/min_distance_of_n_points.py +++ b/divideAndConquer/min_distance_of_n_points.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : min_distance_of_n_points.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-24 22:03 +# Description: +######################################################################### +''' from random import randint from time import time from functools import total_ordering diff --git a/dynamicProgramming/matrixChainMultiply.py b/dynamicProgramming/matrixChainMultiply.py index ec3daa4..4b59bbc 100644 --- a/dynamicProgramming/matrixChainMultiply.py +++ b/dynamicProgramming/matrixChainMultiply.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : matrixChainMultiply.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-05 19:09 +# Description: +######################################################################### +''' def matrixChainMultiply(seq): '''matrix chain multiply, find the optimalest comb to multiply eg ABCD, (AB)(CD), A((BC)D) diff --git a/math/permute_back_track.py b/math/permute_back_track.py index 16547ba..4088bf2 100644 --- a/math/permute_back_track.py +++ b/math/permute_back_track.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : permute_back_track.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-25 12:32 +# Description: +######################################################################### +''' def permute(n): def _util(lst,i): if i==n:print(lst) diff --git a/math/permute_cantor.c b/math/permute_cantor.c index b0da38c..194f867 100644 --- a/math/permute_cantor.c +++ b/math/permute_cantor.c @@ -1,3 +1,14 @@ +/* mbinary +######################################################################### +# File : permute_cantor.c +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-17 11:25 +# Description: +######################################################################### +*/ #include //使用康托展开计算全排列, 下面存储的是0!,1!,2!...(n-1)! diff --git a/math/permute_divide_and_conquer.py b/math/permute_divide_and_conquer.py index f3f84c6..d71fa1b 100644 --- a/math/permute_divide_and_conquer.py +++ b/math/permute_divide_and_conquer.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : permute_divide_and_conquer.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-25 12:23 +# Description: +######################################################################### +''' def permute(lst,n): ''' O(n!), optimal''' if n==1:print(lst) diff --git a/math/permute_next_arrangement.c b/math/permute_next_arrangement.c index 9e70d42..5713a6c 100644 --- a/math/permute_next_arrangement.c +++ b/math/permute_next_arrangement.c @@ -1,3 +1,14 @@ +/* mbinary +######################################################################### +# File : permute_next_arrangement.c +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-17 14:31 +# Description: +######################################################################### +*/ #include void swap(int*arr,int i,int j) diff --git a/search/bloomFilter.py b/search/bloomFilter.py index 552ec30..0604b33 100644 --- a/search/bloomFilter.py +++ b/search/bloomFilter.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : bloomFilter.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-17 11:19 +# Description: +######################################################################### +''' from bitarray import bitarray import mmh3 diff --git a/search/schedule.py b/search/schedule.py index 27e7aef..f86f067 100644 --- a/search/schedule.py +++ b/search/schedule.py @@ -1,3 +1,14 @@ +''' mbinary +######################################################################### +# File : schedule.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-11-30 12:00 +# Description: +######################################################################### +''' ''' 回溯全空间搜索, 剪枝优化 diff --git a/utils/config.py b/utils/config.py new file mode 100644 index 0000000..a36226b --- /dev/null +++ b/utils/config.py @@ -0,0 +1,31 @@ +README = r''' +# Algorithm +>Notes and codes for learning algorithm and data structures :smiley: + +Some pictures and ideas are from `<> + +I use python 3.6+ and c++ to implements them. +Since I used f-Strings in python, you may use python 3.6+ to run the following python scripts. + +>>I am still learning new things and this repo is always updating. + +# Notice +Currently, Github can't render latex math formulas. +So,if you wannt to view the notes which contain latex math formulas and are in markdown format, you can visit [my blog](https://mbinary.coding.me) + +# Index +{index} +''' + +HEAD = '''{begin} mbinary +######################################################################### +# File : {name} +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: {ctime} +# Description: +######################################################################### +{end} +''' diff --git a/utils/genReadme.py b/utils/genReadme.py new file mode 100644 index 0000000..22731b9 --- /dev/null +++ b/utils/genReadme.py @@ -0,0 +1,32 @@ +''' mbinary +######################################################################### +# File : genReadme.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-12-11 15:53 +# Description: +######################################################################### +''' +# coding: utf-8 +from tree import tree +from argparse import ArgumentParser +from config import README + +parser = ArgumentParser() + +parser.add_argument('-p','--path',default='.',help='path to walk') +parser.add_argument('-f','--fileinclude',action='store_true',help='if has, list files and dirs, else only dirs') +parser.add_argument('-d','--depth', type = int, default = 2) +#获取参数 +args = parser.parse_args() +FILE = args.fileinclude +PATH = args.path +DEPTH = args.depth + + +idxs = tree(PATH,DEPTH,FILE) +s = README.format(index='\n'.join(idxs)) +with open('README.md','w') as f: + f.write(s) diff --git a/utils/headinfo.py b/utils/headinfo.py new file mode 100644 index 0000000..eb7ddc5 --- /dev/null +++ b/utils/headinfo.py @@ -0,0 +1,63 @@ +''' mbinary +######################################################################### +# File : headInfo.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-07-08 14:48 +# Description: +######################################################################### +''' + +import os +import sys +import time +from config import HEAD +count = 0 +def handleFile(path): + global count + head = getHead(path) + if head =='': return + with open(path,'r',encoding='utf8',errors='ignore') as f: + s = f.read() + if 'mbinary' in s: + return + count +=1 + name = os.path.basename(path) + print('[{count}]: Adding head info to {name}'.format(count=count,name=name)) + with open(path,'w') as f: + f.write(head+s) + +def getHead(path): + name = os.path.basename(path) + # skip self or hidden file + if name == os.path.basename(__file__) or name[0]=='.': return '' + suf = name[name.rfind('.')+1 :] + begin = end ='' + if suf == 'py': + begin = end = "'''" + elif suf in ['c','cc','cpp','java']: + begin,end = '/*','*/' + elif suf == 'sh': + begin = end = '#' + else:return '' + timeStamp = time.localtime(os.stat(path).st_ctime) + ctime = time.strftime('%Y-%m-%d %H:%M',timeStamp) + return HEAD.format(begin=begin,end=end,ctime=ctime,name = name) + +def handleDir(dirPath): + gen = os.walk(dirPath) + for path,dirs,files in gen: + for f in files: handleFile(os.path.join(path,f)) +if __name__ == '__main__': + works = sys.argv[1:] + if works==[] : works = ['.'] + for one in works: + if not os.path.exists(one): + print('[PathError]: {one} not exists'.format(one=one)) + continue + if os.path.isdir(one): + handleDir(one) + else: + handleFile(one) diff --git a/utils/tree.py b/utils/tree.py new file mode 100644 index 0000000..8c7b9fc --- /dev/null +++ b/utils/tree.py @@ -0,0 +1,54 @@ +''' mbinary +######################################################################### +# File : tree.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-12-11 15:56 +# Description: +######################################################################### +''' +# coding: utf-8 +import os +from argparse import ArgumentParser + +#命令行输入参数处理 +parser = ArgumentParser() + +parser.add_argument('-p','--path',default='.',help='path to walk') +parser.add_argument('-f','--fileinclude',action='store_true',help='if has, list files and dirs, else only dirs') +parser.add_argument('-d','--depth', type = int, default = 2) +#获取参数 +args = parser.parse_args() +FILE = args.fileinclude +PATH = args.path +DEPTH = args.depth + +def mklink(path): + return '* [{name}]({path})'.format(name=os.path.basename(path),path=path) +def clean(paths): + ret = [] + for path in paths: + name = os.path.basename(path) + if not ( name.startswith('.') or name.startswith('__')): + ret.append(path) + return ret + +def tree(path='.',depth=2,showfile=False): + li = [] + if os.path.isdir(path):li = os.listdir(path) + else:li=[path] + items = [os.path.join(path,i) for i in li if not i.startswith('.')] + items = clean(items) + items = sorted(items) + if not showfile: items = [i for i in items if os.path.isdir(i)] + if depth==1: + return [mklink(path)] + [' '*4 + mklink(i) for i in items] + else: + uls = [tree(i,depth-1,showfile) for i in items] + ret = [' '*4 + li for ul in uls for li in ul] + return [mklink(path)] + ret + +if __name__ =='__main__': + print('\n'.join(tree(PATH,DEPTH,FILE)))