순열과 조합
N사 등 여러 코딩테스트에서 유용하게 쓴
from itertools import permutations, combinations
permutations(arr, 2) # 들어갈 배열과 몇개 뽑을건지. 순서를 고려해 (A,B)와 (B,A)는 다른 것
combinations(arr, 2) # 순서를 고려하지 않는다. (A, B)와 (B, A)는 같은 것
이외에 zip(), chain(), all(), any() 등 iterable 관련 함수들
filter
filter 함수를 쓰면 type이 <class filter> 이니 list로 바꿔줘야함
def func(num):
return num > 100
arr = [1,2,3,101,102]
list(filter(func, arr) # 100 이상의 수만 filter
list(filter(lambda n : n > 100, arr) # 람다식 활용
binary, oct, hex
bin(9) #0b1001
oct(0b101010) #0o52
hex(0b101010) #0x2a
int('0b1001', 2) #9
int('0o2', 8) #2
int('0x9', 16) #9
math
math.sqrt(100) # 루트값 -> 10
math.pow(10, 2) # 제곱값 -> 100
set
my_set = set()
# 길이
len(my_set) # size 아님
# 추가
my_set.add(1)
my_set.update([1,2,3])
# 제거
my_set.remove(1)
my_set.discard(2) # remove는 없는거 제거하면 에러남. discard는 에러 안남
my_set.clear()
# check
if 1 in my_set:
print(True)
{'A', 'B'} == {'B', 'A'}
# operation
union = set1 | set2 # set1.union(set2)
intersection = set1 & set2 # set1.intersection(set2)
difference = set1 - set2 # set1.difference(set2)
symmetric_difference = set1 ^ set2 # 교집합 제외 대칭차집합
To be continue...
'알고리즘' 카테고리의 다른 글
leetcode 88 - Merge sorted Array (2) | 2023.05.28 |
---|---|
leetcode 35 - Search Insert Position (0) | 2023.05.28 |
백준 2839 - 설탕배달 (python) (1) | 2023.05.21 |
백준 2178 - 미로탐색 (python) (0) | 2023.05.21 |
백준 10814 - 나이순 정렬 (class2) (python) (0) | 2023.05.14 |