Skip to content

Commit

Permalink
feat(algorithms): programmers solutions for brute force
Browse files Browse the repository at this point in the history
  • Loading branch information
bugoverdose committed Nov 28, 2024
1 parent c32f2da commit 67b0230
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
16 changes: 16 additions & 0 deletions algorithms/programmers/bruteforce/carpet/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/42842?language=python3

def solution(brown, yellow):
total = brown + yellow
row = 2
while True:
row += 1
if total % row > 0: continue
col = total // row
if 2 * (row + col) - 4 == brown:
return [col, row]

print(solution(10, 2)) # [4, 3]
print(solution(8, 1)) # [3, 3]
print(solution(24, 24)) # [8, 6]

37 changes: 37 additions & 0 deletions algorithms/programmers/bruteforce/electricity/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/86971?language=python3

def find(parents, a):
if parents[a] == a:
return a
parents[a] = find(parents, parents[a])
return parents[a]

def union(parents, a, b):
pa = find(parents, a)
pb = find(parents, b)
if pa > pb:
pa, pb = pb, pa
parents[pb] = pa

def solution(n, wires):
answer = 999
for disconnected in range(len(wires)):
parents = [i for i in range(n+1)]
for i in range(len(wires)):
if i == disconnected: continue
[a, b] = wires[i]
union(parents, a, b)
for i in range(1, n+1):
find(parents, i)
diff = 0
a = parents[1]
for i in parents[1:]:
if i == a:
diff += 1
else:
diff -= 1
answer = min(answer, abs(diff))
return answer

print(solution(9, [[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]])) # 3
print(solution(4, [[1,2],[2,3],[3,4]])) # 0
14 changes: 14 additions & 0 deletions algorithms/programmers/bruteforce/minimumrectangle/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/86491?language=python3

def solution(sizes):
max_num, min_num = 0, 0
for size in sizes:
[a, b] = size
if a < b:
a, b = b, a
max_num = max(a, max_num)
min_num = max(b, min_num)
return max_num * min_num

print(solution([[60, 50], [30, 70], [60, 30], [80, 40]])) # 4000
print(solution([[10, 7], [12, 3], [8, 15], [14, 7], [5, 15]])) # 120
22 changes: 22 additions & 0 deletions algorithms/programmers/bruteforce/mockexam/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/42840?language=python3

def solution(answers):
A = [1, 2, 3, 4, 5]
B = [2, 1, 2, 3, 2, 4, 2, 5]
C = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
scores = [0, 0, 0]
for i in range(len(answers)):
answer = answers[i]
scores[0] += A[i % len(A)] == answer
scores[1] += B[i % len(B)] == answer
scores[2] += C[i % len(C)] == answer

max_score = max(scores)
answer = []
for i in range(3):
if scores[i] == max_score:
answer.append(i+1)
return answer

print(solution([1,2,3,4,5])) # [1]
print(solution([1,3,2,4,2])) # [1,2,3]
27 changes: 27 additions & 0 deletions algorithms/programmers/bruteforce/prime/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/42839?language=python3

from itertools import permutations

def is_prime(num):
if num == 0: return False
if num == 1: return False
for i in range(2, int(num ** (1/2)) + 1):
if num % i == 0: return False
return True

def solution(numbers):
nums = []
for num in numbers:
nums.append(num)
all_nums = set()
for i in range(1, len(numbers)+1):
for perms in permutations(nums, i):
all_nums.add(int("".join(perms)))
answer = 0
for num in all_nums:
if is_prime(num):
answer += 1
return answer

print(solution("17")) # 3
print(solution("011")) # 2
18 changes: 18 additions & 0 deletions algorithms/programmers/bruteforce/tiredness/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/87946?language=python3

from itertools import permutations

def solution(k, dungeons):
answer = 0
for perm in permutations(dungeons, len(dungeons)):
count = 0
cur = k
for d in perm:
[needed, cost] = d
if needed > cur: continue
cur -= cost # NOTE: "최소 필요 피로도"는 항상 "소모 피로도"보다 크거나 같습니다.
count += 1
answer = max(answer, count)
return answer

print(solution(80, [[80,20],[50,40],[30,10]])) # 3
27 changes: 27 additions & 0 deletions algorithms/programmers/bruteforce/vowels/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# https://school.programmers.co.kr/learn/courses/30/lessons/84512?language=python3

def solution(word):
vowels = ["A", "E", "I", "O", "U"]
target = []
for w in word:
target.append(w)

answer = 1
cur = ["A"]
while target != cur:
answer += 1
if len(cur) < 5:
cur.append("A")
continue
while len(cur) >= 2 and cur[-1] == "U":
cur.pop()
for i in range(4):
if cur[-1] == vowels[i]:
cur[-1] = vowels[i+1]
break
return answer

print(solution("AAAAE")) # 6
print(solution("AAAE")) # 10
print(solution("I")) # 1563
print(solution("EIO")) # 1189

0 comments on commit 67b0230

Please sign in to comment.