![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
文章平均质量分 93
hope_worker
这个作者很懒,什么都没留下…
展开
-
unittest source code learning
Structure#mermaid-svg-g3PbzysQX9n4xNTU .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-g3PbzysQX9n4xNTU .label text{fill:#333}#mermaid-svg-g3PbzysQX9n4xNTU .node rect,#mermaid-svg-原创 2021-06-09 10:20:03 · 324 阅读 · 4 评论 -
leetcode 122. Best Time to Buy and Sell Stock II
QuestionSay you have an array prices for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multip原创 2020-10-10 09:02:45 · 134 阅读 · 0 评论 -
leetcode 463. Island Perimeter
class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: """ grid = [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] """ ...原创 2020-09-02 09:01:42 · 93 阅读 · 0 评论 -
binary search - leetcode 33. Search in Rotated Sorted Array
1. questionSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target value to search. If found in the array return its index, otherwise ret原创 2020-08-12 09:43:38 · 126 阅读 · 0 评论 -
quick sort
"""step 1 : maintain the following structurea[l] ...... ,,,,,, < a[l] > a[l] j istep 2: in the end, swap(l, j), return j. .....a[l] ,,,,,, < a[l] > a[l] j iFor examp原创 2020-07-15 08:42:13 · 122 阅读 · 0 评论 -
Python slice notation [:] - leetcode 189. Rotate Array
1. a[start:stop:step]q = "12345"w = q[0:2:1] # [0, 2)print(f"w: {w}")# w: 12w = q[::-1] # if step is negative, reverse traverseprint(f"w's type: {type(w)} w: {w}")# w's type: <class 'str'> w: 54321w = q[::-2]print(f"w: {w}") # w: 5312.原创 2020-06-29 15:11:39 · 129 阅读 · 0 评论 -
python 模块导入注意事项
入口文件不能使用相对导入。import .module1if __name__ == '__main__': print("hello")ImportError: attempted relative import with no known parent package相对导入不能超过顶层包。app||----module1| |---f1.py||----module2| |---f2.py||----main.py# f2.p.原创 2020-06-19 13:38:07 · 190 阅读 · 0 评论 -
slide window & +Counter() : leetcode 438. Find All Anagrams in a String
def findAnagrams_leetcode(self, s: str, p: str) -> List[int]: if len(s) < len(p): return [] p_chars = Counter(p) i = 0 j = len(p) window = Counter(s[i:j]) results = [] while j &...原创 2020-06-12 15:21:29 · 168 阅读 · 0 评论 -
subsets : zero left padding : leetcode 78
leetcode 78求解序列的所有子集。例如: [1, 2, 3].所有子集为:[ [ ], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3] ]其中一种解题思路为:使用2进制来表示,组合中是否包含某个元素。例如 001–> [3], 101–>[1, 3], 111–>[1, 2, 3]因此只有列出 [0, 2n) 的2进制表示,就可以求出所有的子集。求解2进制表示时,有一个“zero left padd原创 2020-06-02 07:37:11 · 105 阅读 · 0 评论 -
regular expression examples
import rea = "java|Python|c0123tttyyyuu9"res = re.search("java\|Python\|c([0-9]+)tttyyyuu9", a)print(res.group(0)) # whole stringprint(res.groups()) # matched string in ()print(res.group(1)) # matched string in ()print(res.span(1)) # span of group 1原创 2020-05-29 10:00:55 · 113 阅读 · 0 评论 -
backtrace&usage of slice notation[:] - leetcode 77. Combinations
def combine(self, n: int, k: int) -> List[List[int]]: """ Input: n = 4, k = 2 Output: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] :param n:...原创 2020-05-26 09:07:52 · 162 阅读 · 1 评论 -
build tree & sum path (usage of list[0]) - 404. Sum of Left Leaves
from collections import dequeclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = rightclass Solution: def printTreeCore(self, root, preorder=True): if not原创 2020-05-22 06:37:11 · 140 阅读 · 2 评论 -
算法-计算1的个数及python sort函数的多层排序
leetcode: 1356.Sort Integers by The Number of 1 Bits1. 计算数字中bit1的个数在这里,计算1 bit 的个数使用的是 减1再 bit and的方式。例如 9的二进制表示为 1001:(1)减1 后为 1000, 1001&1000 = 1000.(2)1000 减1 后为 0111, 1000&...原创 2020-04-07 07:37:11 · 265 阅读 · 0 评论 -
design pattern
1 adapterclass Cat: def __init__(self): self.name = "cat" def mom(self): print("mom!")class Dog: def __init__(self): self.name = "dog" def bark(self): ...原创 2020-02-23 09:00:11 · 112 阅读 · 0 评论 -
sort & search algorithm
def quick_sort(array): if len(array) < 2: return array else: pivot_index = 0 pivot = array[pivot_index] less_part = [i for i in array[pivot_index+1:] if i &...原创 2020-02-17 17:49:16 · 115 阅读 · 0 评论 -
parameter passing
print("""There is only one type of parameter passing in Python, which is object reference passing.In the function body, mutable and immutable objects have different behaviours.""")def fl(l): ...原创 2020-02-10 12:36:43 · 212 阅读 · 0 评论 -
GIL & coroutine & performance
GILone byte code instruction is thread safe.The following code has 3 byte code instructions, so it’s not thread safe.import disdef incr_list(l): l[0] += 1if __name__ == '__main__': ...原创 2020-02-09 18:51:42 · 78 阅读 · 0 评论 -
exception
‘docs.python.org’ about ‘exception’def exception_example(): a = 0 try: a = 1/0 except Exception as e: print(e) else: a = 1 finally: print(a)cl...原创 2020-02-09 08:01:26 · 262 阅读 · 0 评论 -
unit test & mock
external_module.pydef multiply(x, y): return x*y+1main.pyimport unittestfrom unittest import mockfrom unittest.mock import patch#### pay attention to the way we import 'multiply'### in t...原创 2020-02-08 13:11:16 · 117 阅读 · 0 评论 -
shallow copy & deep copy
import copydef shallow_n_deep_copy(): a = {'a': [1, 2, 3]} b = a.copy() print('a:', a, 'b:', b) a['a'].append(4) print('a:', a, 'b:', b) c = copy.deepcopy(a) a['a'].appe...原创 2020-02-04 13:41:22 · 96 阅读 · 0 评论 -
collections
from collections import namedtuplefrom collections import dequefrom collections import OrderedDictdef nametuple_example(): Person = namedtuple("hahaha", "name, age, gender") alice = Perso...原创 2020-02-04 06:34:56 · 104 阅读 · 0 评论