python代码模板-算法竞赛用
1 2 3 4 5 6 7
| def getBin(n: int) -> str: """获取一个数字的二进制""" b = str(bin(n)) if n < 0: return b[3:] else: return b[2:]
|
全排列
1 2 3
| from itertools import permutations
print(list(permutations([1, 2, 3])))
|
回文数
1 2 3
| def isPalindrome(x: int) -> bool: """判断是否是回文数""" return x > -1 and str(x)[::-1] == str(x)
|
图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from typing import List class Graph: def __init__(self, number: int): self.dicChild = {} self.dicFather = {} for i in range(number): self.dicChild[i] = set() self.dicFather[i] = set() self.childCountDic = {} self.zeroChildNode = set() self.nextZero = set() def initSet(self, childList: List[List[int]]): for i, childArr in enumerate(childList): for child in childArr: self.dicFather[child].add(i) self.dicChild[i].add(child) self.childCountDic[i] = len(childArr) if len(childArr) == 0: self.zeroChildNode.add(i) def delZeroChild(self): for node in self.zeroChildNode: self.delNode(node) self.zeroChildNode = self.nextZero self.nextZero = set() def delNode(self, node: int): for n in self.dicFather[node]: self.dicChild[n].remove(node) if len(self.dicChild[n]) == 0: self.nextZero.add(n) for n in self.dicChild[node]: self.dicFather[n].remove(node) self.dicChild.pop(node) self.dicFather.pop(node)
|
给字典排序
1 2 3 4 5
| import collections
d = dict(collections.Counter('Hello World'))
print(sorted(d.items(), key=lambda x: x[1], reverse=True))
|
字典计数器
1 2 3 4 5 6 7 8 9 10
| import collections
print(collections.Counter(['a', 'b', 'c', 'a', 'b', 'b']))
print(collections.Counter({'a': 2, 'b': 3, 'c': 1}))
print(collections.Counter(a=2, b=3, c=1))
|
日期
1 2 3 4 5
| from datetime import datetime, timedelta
d = datetime(2001, 5, 15, 13, 59, 59) dt = timedelta(days=1, seconds=1, microseconds=1, milliseconds=1, minutes=1, hours=1, weeks=1)
|
最大公约数(其实math里面有)
1 2
| def gcd(n1, n2): return gcd(n2, n1 % n2) if n2 > 0 else n1
|
最小公倍数
1 2 3 4
| def gcd(n1, n2): return gcd(n2, n1 % n2) if n2 > 0 else n1 def lcm(n1, n2): return n1 * n2 // gcd(n1, n2)
|
序列组合
1 2 3
| from itertools import combinations
print(list(combinations([1, 2, 3], 2)))
|
组合数
1 2 3
| def c(m, n): import math return math.factorial(n) // (math.factorial(m) * math.factorial(n - m))
|
自定义排序
1 2 3
| arr = [23, 131, 5, 124, 35] arr = sorted(arr, key=lambda item: item, reverse=True) print(arr)
|
质数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| def isPrim(n: int) -> bool: """判断是否是质数 O(✓n)""" if n < 2: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True
def countPrim(n: int) -> int: """[0, n) 内有多少个质数 厄拉多塞筛法 O(n*(?n))""" count = 0 signs = [True] * n for i in range(2, n): if signs[i]: count += 1 for j in range(i + i, n, i): signs[j] = False return count
|
队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from collections import deque
d = deque([1,2,3], maxlen=3)
d.append(4)
d = deque()
d.append(1) d.append(2) d.append(3)
print(d.popleft())
print(d.pop())
d.appendleft(0)
|
阶乘
1 2
| import math math.factorial(5)
|
Author:
Littlefean
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
仅个人观点,buddy up!