Python :あなたのコーディングインタビューの最後の分のメモ
17261 ワード
あなたがあなたのコーディング(データ構造とアルゴリズム)インタビューのためにPython言語を選ぶ誰かであるならば、あなたはあなたの最後の分改訂のためにこのポストをブックマークしたいかもしれません.
すばやく辞書でキーの値をインクリメントまたはデクリメントします.
1 .辞書キー値をタプルのリストに変換する
seen = {'i': 2, 'love': 2, 'leetcode': 1, 'coding': 1}
seen.items()
# [('i', 2), ('love', 2), ('coding', 1), ('leetcode', 1)]
+ 1 /- 1でDICOMを入力する
すばやく辞書でキーの値をインクリメントまたはデクリメントします.
seen[c] = 1 + seen.get(c,0)
seen[c] = seen.get(c,0) - 1
キーとリバースに基づくソート
# sorting interval at index i
# by 1st value as key in that interval
# [ [1,2], [3,4] ]
# and then reversing the result
# [ [3,4], [1,2] ]
items.sort(key=lambda x: x[1], reverse=True)
リストタイプならチェック
if isinstance(x, list)
# OR
if type(x) is list
ASCII値
ord('A') = 65
ord('Z') = 90
ord('a') = 97
ord('z') = 122
chr(65) = 'A'
6浮動小数点数を小数点以下2桁まで
round(x, 2)
ダブル終了キューの使用
from collections import deque
>>> word = 'hit'
>>> q = deque([word])
>>> q
deque(['hit'])
>>> q = deque(word)
>>> q
deque(['h', 'i', 't'])
# functions like these exist:
# q.appendleft
# q.popleft
# q.append
# q.pop
8 . DefaultDictによる隣接リストの作成
from collections import defaultdict
neighbours = defaultdict(list)
# creates a structure like this:
# neighbours = {
# key1: [item1, item2],
# key2: [item1, item2, item3],
# }
# can be populated as follows:
for key, val in somelist:
neighbours[key].append(val)
二つの集合の交点
>>> a = { 1, 2, 3 }
>>> b = { 3, 4, 5 }
>>> a.intersection(b)
{3}
>>> list(a.intersection(b))
[3]
組合せ対置換
a = "ABC"
>>> list(itertools.permutations(a))
[('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]
>>> list(itertools.combinations(a, 3))
[('A', 'B', 'C')]
>>> list(itertools.combinations(a, 2))
[('A', 'B'), ('A', 'C'), ('B', 'C')]
>>> list(itertools.permutations(a, 2))
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
行を入力する
if __name__ == '__main__':
n = int(input().strip())
words = []
for _ in range(n):
words_item = input()
words.append(words_item)
# call some function after reading..
noPrefix(words)
テストケースの作成
class Solution:
def twoSum(self, nums, target):
seen = {}
for i, num in enumerate(nums):
num2 = target-num
if num2 in seen:
return [seen[num2], i]
else:
seen[num] = i
import unittest
class BasicTests(unittest.TestCase):
testcases = [
([2,7,11,15], 9, [0,1]),
([3,2,4], 6, [1,2]),
([3,3], 6, [0,1])
]
def test1(self):
s = Solution()
for nums, target, expected in self.testcases:
actual = s.twoSum(nums, target)
# print(actual, expected)
assert actual == expected or list(reversed(actual)) == expected
if __name__ == "__main__":
unittest.main()
私は個人的にこれは非常に有用な発見し、私もあなたからいくつかの値を得ることを望む.グッドラック!Reference
この問題について(Python :あなたのコーディングインタビューの最後の分のメモ), 我々は、より多くの情報をここで見つけました https://dev.to/omkarscode/python-last-minute-notes-for-your-coding-interviews-4om0テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol