Skip to content

Commit ca7bd3a

Browse files
committed
Update misc codes.
1 parent 772ae03 commit ca7bd3a

8 files changed

+213
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,12 @@
108108
| 93(문제 5) | 뉴스 클러스터링 | ★★ | 부록 B. 카카오 공채 문제 풀이 | [5.py](appendix-B/5.py) |
109109
| 94(문제 6) | 프렌즈4블록 | ★★★ | 부록 B. 카카오 공채 문제 풀이 | [6.py](appendix-B/6.py) |
110110
| 95(문제 7) | 추석 트래픽 | ★★★ | 부록 B. 카카오 공채 문제 풀이 | [7.py](appendix-B/7.py) |
111+
112+
## 기타 코드
113+
- 11장 [생일 문제](miscellaneous/11-birthday.py)
114+
- 12장 [그래프 순회](miscellaneous/12-graph-traversals.py)
115+
- 14장 [트리 순회](miscellaneous/14-tree-traversals.py)
116+
- 17장 [버블 정렬](miscellaneous/17-bubble-sort.py)
117+
- 17장 [퀵 정렬](miscellaneous/17-quick-sort.py)
118+
- 21장 [분할 가능 배낭 문제](miscellaneous/21-fractional-knapsack.py)
119+
- 23장 [0-1 배낭 문제](miscellaneous/23-zero-one-knapsack.py)

miscellaneous/11-birthday.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import random
2+
3+
TRIALS = 100000 # 10만 번 실험
4+
same_birthdays = 0 # 생일이 같은 실험의 수
5+
6+
# 10만 번 실험 진행
7+
for _ in range(TRIALS):
8+
birthdays = []
9+
# 23명이 모였을 때, 생일이 같을 경우 same_birthdays +=1
10+
for i in range(23):
11+
birthday = random.randint(1, 365)
12+
if birthday in birthdays:
13+
same_birthdays += 1
14+
break
15+
birthdays.append(birthday)
16+
17+
# 전체 10만 번 실험 중 생일이 같은 실험의 확률
18+
print(f'{same_birthdays / TRIALS * 100}%')

miscellaneous/12-graph-traversals.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
graph = {
2+
1: [2, 3, 4],
3+
2: [5],
4+
3: [5],
5+
4: [],
6+
5: [6, 7],
7+
6: [],
8+
7: [3],
9+
}
10+
11+
12+
def recursive_dfs(v, discovered=[]):
13+
discovered.append(v)
14+
for w in graph[v]:
15+
if w not in discovered:
16+
discovered = recursive_dfs(w, discovered)
17+
return discovered
18+
19+
20+
def iterative_dfs(start_v):
21+
discovered = []
22+
stack = [start_v]
23+
while stack:
24+
v = stack.pop()
25+
if v not in discovered:
26+
discovered.append(v)
27+
for w in graph[v]:
28+
stack.append(w)
29+
return discovered
30+
31+
32+
def iterative_bfs(start_v):
33+
discovered = [start_v]
34+
queue = [start_v]
35+
while queue:
36+
v = queue.pop(0)
37+
for w in graph[v]:
38+
if w not in discovered:
39+
discovered.append(w)
40+
queue.append(w)
41+
return discovered
42+
43+
44+
print(f'recursive dfs: {recursive_dfs(1)}')
45+
print(f'iterative dfs: {iterative_dfs(1)}')
46+
print(f'iterative bfs: {iterative_bfs(1)}')

miscellaneous/14-tree-traversals.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Node:
2+
def __init__(self, val, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
7+
8+
root = Node('F',
9+
Node('B',
10+
Node('A'),
11+
Node('D',
12+
Node('C'), Node('E'))),
13+
Node('G',
14+
None,
15+
Node('I', Node('H'))
16+
)
17+
)
18+
19+
20+
def preorder(node):
21+
if node is None:
22+
return
23+
print(node.val)
24+
preorder(node.left)
25+
preorder(node.right)
26+
27+
28+
def inorder(node):
29+
if node is None:
30+
return
31+
inorder(node.left)
32+
print(node.val)
33+
inorder(node.right)
34+
35+
36+
def postorder(node):
37+
if node is None:
38+
return
39+
postorder(node.left)
40+
postorder(node.right)
41+
print(node.val)
42+
43+
44+
preorder(root)
45+
inorder(root)
46+
postorder(root)

miscellaneous/17-bubble-sort.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def bubblesort(A):
2+
for i in range(1, len(A)):
3+
for j in range(0, len(A) - 1):
4+
if A[j] > A[j + 1]:
5+
A[j], A[j + 1] = A[j + 1], A[j]
6+
7+
8+
A = [38, 27, 43, 3, 9, 82, 10]
9+
bubblesort(A)
10+
print(A)

miscellaneous/17-quick-sort.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def quicksort(A, lo, hi):
2+
def partition(lo, hi):
3+
pivot = A[hi]
4+
left = lo
5+
for right in range(lo, hi):
6+
if A[right] < pivot:
7+
A[left], A[right] = A[right], A[left]
8+
left += 1
9+
A[left], A[hi] = A[hi], A[left]
10+
return left
11+
12+
if lo < hi:
13+
pivot = partition(lo, hi)
14+
quicksort(A, lo, pivot - 1)
15+
quicksort(A, pivot + 1, hi)
16+
17+
18+
A = [38, 27, 43, 3, 9, 82, 10]
19+
quicksort(A, 0, len(A) - 1)
20+
print(A)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cargo = [
2+
(4, 12),
3+
(2, 1),
4+
(10, 4),
5+
(1, 1),
6+
(2, 2),
7+
]
8+
9+
10+
def fractional_knapsack(cargo):
11+
capacity = 15
12+
pack = []
13+
# 단가 계산 역순 정렬
14+
for c in cargo:
15+
pack.append((c[0] / c[1], c[0], c[1]))
16+
pack.sort(reverse=True)
17+
18+
# 단가 순 그리디계산
19+
total_value: float = 0
20+
for p in pack:
21+
if capacity - p[2] >= 0:
22+
capacity -= p[2]
23+
total_value += p[1]
24+
else:
25+
fraction = capacity / p[2]
26+
total_value += p[1] * fraction
27+
break
28+
29+
return total_value
30+
31+
32+
r = fractional_knapsack(cargo)
33+
print(r)

miscellaneous/23-zero-one-knapsack.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cargo = [
2+
(4, 12),
3+
(2, 1),
4+
(10, 4),
5+
(1, 1),
6+
(2, 2),
7+
]
8+
9+
10+
def zero_one_knapsack(cargo):
11+
capacity = 15
12+
pack = []
13+
14+
for i in range(len(cargo) + 1):
15+
pack.append([])
16+
for c in range(capacity + 1):
17+
if i == 0 or c == 0:
18+
pack[i].append(0)
19+
elif cargo[i - 1][1] <= c:
20+
pack[i].append(
21+
max(
22+
cargo[i - 1][0] + pack[i - 1][c - cargo[i - 1][1]],
23+
pack[i - 1][c]
24+
))
25+
else:
26+
pack[i].append(pack[i - 1][c])
27+
return pack[-1][-1]
28+
29+
30+
r = zero_one_knapsack(cargo)
31+
print(r)

0 commit comments

Comments
 (0)