Skip to content

Commit

Permalink
[프로그래머스] 주사위 고르기
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdgua01 committed Sep 2, 2024
1 parent 3c7628d commit 2593420
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions coding_test/programmers/select_dice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import bisect
from itertools import combinations, product


def solution(dice):
data = {
idx_list: sorted(
sum(dice[v][k] for k, v in zip(x, idx_list))
for x in product(range(6), repeat=len(idx_list))
)
for idx_list in combinations(range(len(dice)), len(dice) // 2)
}
result = {}
for k, v in data.items():
if k in result:
continue
other_key = tuple(i for i in range(len(dice)) if i not in k)
win = lose = 0
for x_value in v:
win += bisect.bisect_left(data[other_key], x_value)
lose += len(v) - bisect.bisect_right(data[other_key], x_value)
total = len(v)
result[k] = win / total * 100
result[other_key] = lose / total * 100
return [
i + 1 for i in sorted(result.items(), key=lambda x: x[1], reverse=True)[0][0]
]


def test_cases():
assert solution(
[[1, 2, 3, 4, 5, 6], [3, 3, 3, 3, 4, 4], [1, 3, 3, 4, 4, 4], [1, 1, 4, 4, 5, 5]]
) == [1, 4]
assert solution([[1, 2, 3, 4, 5, 6], [2, 2, 4, 4, 6, 6]]) == [2]
assert solution(
[
[40, 41, 42, 43, 44, 45],
[43, 43, 42, 42, 41, 41],
[1, 1, 80, 80, 80, 80],
[70, 70, 1, 1, 70, 70],
]
) == [1, 3]

0 comments on commit 2593420

Please sign in to comment.