본문 바로가기

프로그래머스 - 야구게임[42841] 본문

Algorithms/BF (Brute-Force)

프로그래머스 - 야구게임[42841]

Louisus 2020. 5. 23. 23:30
728x90

https://programmers.co.kr/learn/courses/30/lessons/42841

 

코딩테스트 연습 - 숫자 야구

[[123, 1, 1], [356, 1, 0], [327, 2, 0], [489, 0, 1]] 2

programmers.co.kr


def check_score(question, candidate, s, b):
    strike =
0

   
for i in range(len(question)):
       
if question[i] == candidate[i]:
            strike +=
1
   
if s != strike:
       
return False
   
ball = len(set(question) & set(candidate)) - strike
   
if b != ball:
       
return False
    return True

def
solution(baseball):
    lst =
list(permutations(range(1,10), 3))
   
for i in baseball:
       
for item in lst[:]:
           
if not check_score([int(i) for i in list(str(i[0]))], item, i[1], i[2]):
                lst.remove(item)

   
return len(lst)


baseball = [[
123, 1, 1], [356, 1, 0], [327, 2, 0], [489, 0, 1]]
print(solution(baseball))

Comments