Implement Union Find in Python
UnionFind 是在 Find 的時候才把 set 合併在一起,並且在 union 的時候會 call find ```py class UnionFind: def __init__(self): self.father = {} ...
Python use for loop to generate nested list to avoid weird behavior
如果使用 `[[0] *3] *2` 這樣的方式建立 nested array ` [[0, 0, 0], [0, 0, 0]]`,那第一個 array 跟第二個 array 都是指向同一個記憶體位置,所以當改變 element 的值時,例如: `list[0][0] =...
Python class method, instance method, and static method
* instance method => 傳 instance 進去的 function,以 self 作為第一個 argument * class method => 傳 class 進去的 function,以 cls (就是 class 的縮寫) 作為第一個 argu...
Python3 sorting
https://docs.python.org/3/howto/sorting.html#sortinghowto 這個 doc 講得很清楚。 ## Basic: `list.sort()` => 改變原本的 list,只能用在 list `sorted()`=> 回...
Python decimal
Float ```py In [10]: num = 3577 In [11]: while num >= 10: ...: num /= 10 ...: In [12]: num Out[12]: 3.5769999999999995 ```...
Python collections.defaultdict
> Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writab...
Redefine comparator in Python and Ruby
## Python In python, there are several magic functions for comparing: * `__eq__(self, other)` 定義了等號的行為` ==` 。 * `__ne__(self, other)` ...
How to sort a list in Python
## 1. Object oriented way: ```python list = [4,3,2,1] list.sort() # => return nothing list # => [1,2,3,4] ``` - called from object ...
複習一下各種排序法,和 Python 實現
搭配題目和講解影片服用: * Lintcode: https://www.lintcode.com/problem/sort-integers * Leetcode: https://leetcode.com/problems/sort-an-array/ * https...
Python deque 用法
1. deque 類似於 list (ruby 的 array),只不過 deque 可以操作兩端 2. deque 是 tread safe,所以可以同時操作 何謂操作兩端?這個 example 看一下即了解: ```py import collections d1...
Some Python tools
List few tools that I find helpful for Python's beginner learner - Console: https://ipython.org/ - IDE: https://www.jetbrains.com/pycha...
Writing idiomatic python 3
Recomended http://downloads.niceware.com/TECH-pdf/PythonStyle-Writing_idiomatic_python_3.pdf?fbclid=IwAR1uhD-_8WRmWy1Sb2Ncz9Afwk248-o5JA...