Blog Archive

Wednesday, November 20, 2019

[学Python/Perl] 一些刷题常用的 python 技巧

Ref: https://www.1point3acres.com/bbs/thread-543794-1-1.html

Python 越来越多地成为大家刷题的主流语言,主要原因是它的语法非常简洁明了。因此我们能节省更多的时间,来关注算法和数据结构本身。

而用好 Python 自身独有的一些语法特性,不仅能更节省时间,也能让代码看起来更加优雅。这里我总结了一些我自己刷题过程中用到的一些常用的功能。以下以 python3 为例, python2 略有差异。

List
Python 的列表 List 基本就是其它语言的 Array.

Initialization 初始化
List 的初始化一般用 List comprehension,往往能一行解决问题

[Python] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
# 1d array
l = [0 for _ in range(len(array)]
# or
l = [0] * len(array)
 
# 2d
l = [[0] for i in range(cols) for j in range(rows)]


# or
l = [0] * len(array)[/mw_shl_code]
l = [0 for _ in range(len(array)]
# or
l = [0] * len(array)[/mw_shl_code]


# 2d
l = [[0] for i in range(cols) for j in range(rows)]
Start from the behind
你可以轻松从后往前访问:

lastElement = l[-1]

lastTwo = l[-2:]

for i in range(0, -10, -1)
# 0, -1, -2, -3, -4, -5, -6, -7, -8, -9

copy 复制

shallow copy 浅拷贝

l2 = l1[:]
# or
l2 = l1.copy()
浅复制的问题在于,如果 l1 内部还有 list,那么这种嵌套的索引不能被复制,比如:

[Python] 纯文本查看 复制代码
?
01
02
03
04
05
a = [1, 2, [3, 4]]
b = a[:]
a[2].append(5)
print(b)
# [1, 2, [3, 4, 5]]


deep copy 深拷贝

所以如果要做深拷贝,要节制自带库 copy

import copy

copy.deepcopy()

enumerate 枚举

当我们需要枚举一个数组并同时获得值与 index 的时候可以使用:

l = ["a", "b", "c"]

for i, v in enumerate(l):
    print(i, v)
# 0 a
# 1 b
# 2 c

zip

zip 本意就是拉链,可以想象成将两个数组像拉链一样挨个聚合:

[Python] 纯文本查看 复制代码
?
01
02
03
04
05
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]


reduce

reduce 可以分别对相邻元素使用同一种计算规则,同时每一步结果作为下一步的参数,很典型的函数式编程用法。
[Bash shell] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
# importing functools for reduce()
import functools
# initializing list
lis = [ 1, 3, 5, 6, 2, ]
 
# using reduce to compute sum of list
print ("The sum of the list elements is : ",end="")
print (functools.reduce(lambda a,b : a+b,lis))
 
# The sum of the list elements is : 17


map

可以将参数一一映射来计算, 比如

date = "2019-8-15"
Y, M, D = map(int, date.split('-'))
# Y = 2019, M = 8, D = 15

deque

list 删除末尾的操作是O(1)的,但是删除头操作就是O(n),这时候我们就需要一个双端队列 deque。首尾的常规操作为:

append,添加到末尾
appendleft, 添加到开头
pop, 剔除末尾
popleft,移除开头

sorted

list 自身有自带的 sort(), 但是它不返回新的 list. sorted 能返回一个新的 list, 并且支持传入参数reverse。

比如我们有一个 tuple 的数组,我们想按照 tuple 的第一个元素进行排序:

l1 = [(1,2), (0,1), (3,10) ]

l2 = sorted(l1, key=lambda x: x[0])

# l2 = [(0, 1), (1, 2), (3, 10)]
这里的 key 允许传入一个自定义参数,也可以用自带函数进行比较,比如在一个 string 数组里只想比较小写,可以传入key=str.lower

l1 = ["banana","APPLE", "Watermelon"]
l2 = sorted(l1, key=str.lower)
print(l2)

# ['APPLE', 'banana', 'Watermelon']
lambda
你注意到我们在上面使用了 lambda 来定义一个匿名函数,十分方便。如果你熟悉其它语言类似 JS 的话,可以把它理解成一个 callback 函数,参数名一一对应就行。

cmp_to_key

在 python3 中,sorted 函数取消了自带的cmp函数,需要借助functools 库中的 cmp_to_key来做比较。
比如如果要按照数组元素的绝对值来排序:

[Bash shell] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
from functools import cmp_to_key
def absSort(arr):
    newarr = sorted(arr, key = cmp_to_key(sortfunc))
    return newarr
def sortfunc(a, b):
    if abs(a) < abs(b):
      return -1
    elif abs(a) > abs(b):
      return 1
    else:
      return a - b


set

set 的查找操作复杂度为O(1),有时候可以替代dict 来存储中间过程。

add : set 的添加是 add 不是append
remove vs discard: 都是删除操作,区别在于remove不存在的元素会报错,discard不会。
union, intersection: 快速获得并集和交集,方便一些去重操作。

dict

字典,相当于其它语言中的map, hashtable, hashmap之类的,读取操作也是O(1) 复杂度

keys(), values(), items()
这三个方法可以分别获得key, value, {key: value}的数组。

setdefault

这个函数经常在初始化字典时候使用,如果某个key在字典中存在,返回它的value, 否则返回你给的 default 值。比如在建一个 trie 树的时候

[Python] 纯文本查看 复制代码
?
01
02
03
node = self.root
for char in word:
     node = node.setdefault(char, {})


OrderedDict

OrderedDict 能记录你 key 和 value 插入的顺序,底层其实是一个双向链表加哈希表的实现。我们甚至可以使用move_to_end这样的函数:

>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys())
'acdeb'
# 放开头
>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
'bacde'

defaultdict

defaultdict可以很好地来解决一些初始化的问题,比如 value 是一个 list,每次需要判断 key 是否存在的情况。这时我们可以直接定义

d = defaultdict(list)

s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
for k, v in s:
     d[k].append(v)
sorted(d.items())
# [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

heapq

heapq 就是 python 的 priority queue,heapq[0]即为堆顶元素。

heapq 的实现是小顶堆,如果需要一个大顶堆,常规的一个做法是把值取负存入,取出时再反转。
以下是借助 heapq 来实现 heapsort 的例子:

>>> def heapsort(iterable):
...     h = []
...     for value in iterable:
...         heappush(h, value)
...     return [heappop(h) for i in range(len(h))]
...
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

bisect

python 自带二分查找的库,在一些不要求实现 binary search,但是借助它能加速的场景下可以直接使用。

bisect.bisect(a, x, lo=0, hi=len(a))
这里的参数分别为 数组,要查找的数,范围起始点,范围结束点
相似函数还有

bisect.bisect_left
bisect.bisect_right
分别返回可以插入 x 的最左和最右 index

Counter

Counter 接受的参数可以是一个 string, 或者一个 list, mapping

>>> c = Counter()                           # a new, empty counter
>>> c = Counter('gallahad')                 # a new counter from an iterable
>>> c = Counter({'red': 4, 'blue': 2})      # a new counter from a mapping
>>> c = Counter(cats=4, dogs=8)             # a new counter from keyword args
most_common(n)
可以得到出现次数最多的 n 个数:
>>> Counter('abracadabra').most_common(3)  # doctest: +SKIP
[('a', 5), ('r', 2), ('b', 2)]

strings

ord, char

ord 返回单个字符的 unicode:

>>> ord('a')
97
char 则是反向操作:

>>> chr(100)
'd'

strip

移除 string 前后的字符串,默认来移除空格,但是也可以给一个字符串,然后会移除含有这个字符串的部分:

>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'

split

按照某个字符串来切分,返回一个 list, 可以传入一个参数maxsplit来限定分离数。

>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']

int/ float

最大, 最小 number
有时候初始化我们需要设定 Math.max() 和 Math.min(), 在 python 中分别以 float('inf') 和 float('-inf')表示

我们也可以这么做:

[Python] 纯文本查看 复制代码
?
01
02
03
04
import sys
 
#maxint
Max = sys.maxint


除法

在 python3 中, / 会保留浮点,相当于 float 相除,如果需要做到像 pyhton2 中的 int 相除,需要 //:

>>> 3 / 2
1.5
>>> 3 // 2
1
次方
在 python 中为 **:

>>> 2 ** 10
1024

conditions

在 python 的三项表达式(ternary operation) 与其它语言不太一样:

res = a if condition else b
它表示如果 condition 满足,那么 res = a, 不然 res = b,在类 c 的语言里即为:

res = condition ? a : b;

any, all

any(), all()很好理解,就是字面意思,即参数中任何一个为 true 或者全部为 true 则返回 true。经常可以秀一些骚操作:
比如 36. Valid Sudoku 这题:

[Python] 纯文本查看 复制代码
?
01
02
03
04
05
06
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        row = [[x for x in y if x != '.'] for y in board]
        col = [[x for x in y if x != '.'] for y in zip(*board)]
        pal = [[board[i+m][j+n] for m in range(3) for n in range(3) if board[i+m][j+n] != '.'] for i in (0, 3, 6) for j in (0, 3, 6)]
        return all(len(set(x)) == len(x) for x in (*row, *col, *pal))


itertools
这是 python 自带的迭代器库,有很多实用的、与遍历、迭代相关的函数。

permutations 排列

permutations('ABCD', 2)
# AB AC AD BA BC BD CA CB CD DA DB DC

combinations 组合

combinations('ABCD', 2)
# AB AC AD BC BD CD

groupby 合并

https://leetcode.com/problems/swap-for-longest-repeated-character-substring/discuss/355852/Python-Groupby/322898

[k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
[list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D

functools

这个库里有很多高阶函数,包括前面介绍到的cmp_to_key 以及 reduce,但是比较逆天的有 lru_cache,即 least recently used cache. 这个 LRU Cache是一个常见的面试题,通常用 hashmap 和双向链表来实现,python 居然直接内置了。

用法即直接作为 decorator 装饰在要 cache 的函数上,以变量值为 key 存储,当反复调用时直接返回计算过的值,例子如下:

lru_cache
https://leetcode.com/problems/stone-game-ii/discuss/345230/Python-DP-Solution

[Python] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
def stoneGameII(self, A: List[int]) -> int:
    N = len(A)
      for i in range(N - 2, -1, -1):
    A += A[i + 1]
      from functools import lru_cache
    @lru_cache(None)
    def dp(i, m):
        if i + 2 * m >= N: return A
        return A - min(dp(i + x, max(m, x)) for x in range(1, 2 * m + 1))
    return dp(0, 1)


resource


这是一个大神用各种 Python trick 解题的 repo,可供娱乐:

https://github.com/cy69855522/Shortest-LeetCode-Python-Solutions

当然 Leetcode 讨论区还会经常见到 StefanPochmann 或者 lee215这样的大神 Po 一些很秀技的 python 代码,都是学习范本

Tuesday, November 12, 2019

小白码农辛苦上岸的一些经验

全局:👍   97% (297)
 
 
2% (9)    👎

2019(7-9月)-EE硕士+1-3年 | Other| 码农类General全职@
背景:本来是一行代码不会写的,读了几天HTML/CSS的教程,踩了狗屎运找了个小公司实习,写HTML/CSS,然后return offer,到现在一眨眼就是2年零10个月。突发奇想想去做后端,业余时间开始刷题,努力了一阵刚拿到offer。有个同事问我是怎么学的,给他讲了讲,顺便给大家分享下我的小白上岸之路。
1.  https://www.freecodecamp.org/new ... mmies-5e048933b82b/ -baidu 1point3acres
    这是一篇很入门的文章,介绍算法面试的流程/ 题型分类 / 典型题。 建议仔细读一遍,然后把里面的题大概都看一遍,有个概念。

2. cc150/cc189(google Cracking-the-Coding-Intervie pdf 下载), 基本一样,是几乎所有数据结构的讲解,例题和答案。例题可以读一读,不用写,leetcode里都有。

3. 刷Leetcode,每道题点discussion,都有别人上传的代码,有的题有最优解法,没读过答案根本想不出来,这种题不用较真,多读几次答案就行。google 直接搜题号和题目,有很多同胞给的讲解,挑自己喜欢的风格就行,这三个人我用的比较多
    https://www.cnblogs.com/grandyang/p/4606334.html  这个人讲的非常细
    https://zxi.mytechroad.com/blog/ ... -number-complement/  这个人是视频讲解
    https://wdxtub.com/interview/14520594642530.html 这个人分享的覆盖面比较全

4. 然后就是 刷,刷,刷,刷题,刷200-300道就足够应付中型固定题目的公司了。

5. 1point3acres 同胞分享的面试经验/题目,刷题心得及包裹等等,受益颇多
    hired.com 是hr主动上门的网站
    triplebyte  免费模拟面试(可重复),然后帮忙联系公司
-baidu 1point3acres
6. 什么时候拿到面试了, 开始准备ood,system design 和 SQL 就来得及,题型非常固定,2-3周足够了。
    https://www.educative.io/,付费的,别心疼钱,省去了很多搜索资料的时间
    http://blog.gainlo.co/index.php/2016/10/22/design-youtube-part/ , system design 补充资料,免费的,也不错
    SQL 就做leetcode上的就行,不多,主要是熟练,要多写,面试是设计table(随便找一遍文章读一下,难度基本===人有几个鼻子几个眼睛),加白板写query,99%就是join,group by,count,order by, limit 这几个,换汤不换药。

7. 算法读懂了就可以投投简历,很多公司第一步是算法在线测试,就算不想去,就当作模拟题练手。一亩三分地上,只要有人分享面经的公司,就说明在招人,如果有朋友在公司里就找内推, 不然就直接到公司官网投了,就有概率得到测试。

8. 我的timeline,两个半月刷题260道,1.5遍。一个月从零学习的OOD, SYSTEM DESIGN, SQL. 海投了30-50家公司拿到面试3-5个, 内推10家公司拿到面试5个, onsite interview 2次, 一个offer,一个待定, 赶巧拿offer的公司比较想去,package也几乎是现在double,打算签了。


应该是第一次在求职栏发帖,很多规矩不太懂,如果有违规或冒犯之处,请见谅。

人生路很长,祝人人可以先上岸,然后找到自己梦想的靶心。

最不想提,但是还是提了,求米

https://www.1point3acres.com/bbs/thread-563736-1-1.html

Tuesday, November 5, 2019

DFS + Memoization (Good) vs DFS + Memorization (wrong)


DFS + Memorization


memorization 美 [,mɛmərɪ'zeʃən]  n. 记住;暗记
Memoization Pattern 使用备忘录模式
Memoization is an approach to avoid work repetition by caching previous calculations for later reuse, which makes memoization a useful technique for recursive algorithms. 
制表,通过缓存先前计算结果为后续计算所重复使用,避免了重复工作。这使得制表成为递归算法中有用的技术。

If the terminal condition is correct, then the algorithm contains too much recursion to safely be run in the browser and should be changed to use iteration, memoization, or both. 

如果终止条件是正确的,那么算法包含了太多层递归,为了能够安全地在浏览器中运行,应当改用迭代,制表,或两者兼而有之。




! "bang", "exclamation point"
@ "at", and rarely, "strudel"
# "crunch", "hash", "pound", and rarely, "octothorpe"
^ "circumflex", "hat", "chapeau"
& "ampersand", "and"
* "splat", "star", "asterisk", "times" (as in multiplication)
_ "underscore"
- "hyphen", "dash", "minus sign"
. "dot", "period"
, "comma"
: "colon"
; "semi-colon"
/ "slash"
\ "backslash"
~ "twiddle", also "squiggle", or more correctly, "tilde"
' "tick", "quote", "apostrophe"
" "double-quote"
` "backtick", "backquote"
< "less-than", "left angle bracket"
> "greater-than", "right angle bracket"

https://www.1point3acres.com/bbs/thread-308790-1-1.html

Saturday, October 12, 2019

My favorite quotes from movie Aladdin

source:

https://www.moviequotesandmore.com/aladdin-best-movie-quotes/


You got things to do. Places to go. People to see. Futures to make.

A diamond in the rough.  像未经雕刻的原钻

The boss looks ill-tempered, but he is in fact a diamond in the rough.


The candidate pools were so large it's hard to find a diamond in the rough.

Aladdin: We get by. Every day, I just think things will be different, but it never seems to change. Just sometimes, I feel like I’m…
Jasmine: Trapped. Like you can’t escape what you were born into?
Aladdin: Yes.

Jasmine: You remember my mother used to say, “We would only ever be as happy as our least happy subject.” If she saw what I saw today, she would be heart broken.
Dalia: She would also want you to be safe. And clean. I’ll draw the bath.

Steal an apple, and you’re a thief. Steal a kingdom, and you’re a statesman.

Only weak men stop there. You’re either the most powerful man in the room, or you’re nothing. You, you stumbled upon an opportunity. I can make you rich. Rich enough to impress a princess. But nothing comes for free.

Aladdin: What would I have to do?
Jafar: There’s a cave nearby, and in it, a simple oil lamp. Retrieve it for me, and I will make you wealthy enough to impress a princess. You’re nothing to her. But you could be. Your life begins now, Aladdin.


Jafar: [to Aladdin] The Cave of Wonders. When you enter, you will see more riches than you ever dreamed of. Gold, diamonds, and the lamp. Bring it to me and I will make you rich and free. But take no other treasure, no matter how sorely you are tempted. And you will be tempted.
[as Aladdin approaches the cave, it opens up]
Cave of Wonders: Only one may enter here. One whose worth lies far within. A diamond in the rough.
Jafar: Remember, take nothing but the lamp.

Genie: Where’s your boss?
Aladdin: Um, my boss?
Genie: Look, kid, I’ve been doing this a long time, alright? There’s always a guy, you know. He’s cheated somebody, or buried somebody, or, I mean, you get my point. Where’s that guy?
Aladdin: I know that guy. He’s outside.

Genie: So here’s the basics. Step one, rub the lamp. Step two, say what you want. Step three, there is no step three. See? It’s that easy. You get three wishes. They must begin with rubbing the lamp and saying, “I wish.” Got it?

 I promise you, there’s not enough money and power on earth for you to be satisfied. Good?


Aladdin: Hey, can you make me a prince?
Genie: There’s a lot of gray area in “make me a prince”. I could just make you a prince.
[he uses his magic to create a prince]
Aladdin: Oh, no.
Genie: But you’d be snuggled up with that dude for the rest of your life.
[from the distance]
Prince: Yoh! Y’all seen my palace?


Genie: Stop, stop, stop. I made you look like a prince on the outside, but I didn’t change anything on the inside. Prince Ali got you to the door, but Aladdin has to open it.

Genie: It’s showtime.
Aladdin: No, I’m waiting for the right moment.
Genie: No, no. No waiting. We’re done waiting.
Aladdin: No. I’m in charge, okay? I say when it’s the right moment.

Genie: Wow. I mean, genie magic is really just a facade. At some point, the real character’s always going to shine through. But that’s a good thing, right? Now, she knows.

Jafar: As the old man said, “You should have left Agrabah when you had the chance.” I told you before to think bigger. You could have been the most powerful man in the room. But now, I hold the lamp. I hold the power.
Aladdin: You can’t find what you’re looking for in that lamp, Jafar. I tried and failed, and so will you.
Jafar: You think so? But I am Sultan! I am the greatest sorcerer the world has ever seen. I will create an empire that history cannot ignore. I can destroy cities. I can destroy kingdoms. And I can destroy you.
Aladdin: True. But who made you a sultan? Who made you a sorcerer? There will always be some thing, some man, some being more powerful than you.
Genie: What are you doing?
Aladdin: Genie gave you your power, and he can take it away.
Jafar: He serves me!
Aladdin: For now. But you’ll never have more power than the genie. You said it yourself, you’re either the most powerful in the room, or you’re nothing. You will always be second.
Iago: Second. Second.
Jafar: Second?! Only second? He serves me! I will make sure no one will ever say these words again!


Jafar: I will not forget you, boy! Mark my words. I will not forget what you have done to me.
Iago: Goodbye, Jafar.


Genie: Alright, last wish. Let’s get it.
Aladdin: Okay. Last wish. Genie…
Genie: I’m ready. Hold on. Here we go.
Aladdin: I wish…
Genie: The third and final wish.
Aladdin: I wish to set you free.
Genie: What? Woh. Oh! Wait.
[his genie shackels starts to disappear and becomes human and free]
Genie: Am I… Wait, wait, wait. Um, tell me to do something.
Aladdin: Um, give me some jams.
Genie: Get it yourself? Get your own jams!
[Aladdin and Genie embrace each other]
Genie: Thank you. Thank you.
Aladdin: No. Thank you, Genie. I owe you everything.

Sultan: Sit with me, my child. I’m sorry.
Jasmine: Baba, why are you…?
Sultan: Please, let me finish. I feared losing you, like I lost your mother. All I saw was my little girl, not the woman you have become. You have shown me courage and strength. You are the future of Agrabah. You shall be the next sultan.
[he kisses her hand]
Jasmine: Thank you, Baba.
Sultan: As sultan, you may change the law.
[referring to Aladdin]
Sultan: He is a good man.
[Jamsmine kisses his forehead]