https://programmers.co.kr/learn/courses/30/lessons/64064#
#불량사용자라는 이름으로 목록을 만들어서 당첨 처리 시 제외
# 이벤트 당첨자 아이디 중 일부 문자를 "*"문자로 가려서 전달
# 불량 사용자 목록에 매핑된 응모자 아이디를 제재 아이디
#숫자로 세면 안되는 이유, 중복이 있을 수 있음
#(a,b,c,d) ,(a,b,d,c)
#모든 banned_id 값이 모든 user_id 값에 일대일 대응이 되는지
from itertools import combinations
from itertools import permutations
def solution(user_id, banned_id):
answer = 0
items = list(combinations(user_id,len(banned_id)))
for item in items:
able = list(permutations(item,len(item)))
for it in able:
dictions = dict()
for b in banned_id:
for u in it:
flag = True
if u not in dictions.keys():
dictions[u] = 0
if dictions[u] != 0 or (len(u) != len(b)):
continue
for i in range(len(u)): #길이가 같다면
if b[i] == "*":
continue
elif u[i] != b[i]:
flag = False
break
if flag :
dictions[u] += 1
break
if sum(dictions.values()) == len(banned_id):
answer += 1
break
return answer
일단 우리가 고려해야 할 사항이 많았다.
처음에 생각했지만 틀린 알고리즘과 그 이유
from itertools import product
list1 = [[1,1],[2,2],[3,3]]
print(list(product(*list1)))
# [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]
from itertools import product
list1 = [[1,2],[3,4],[5,6]]
print(list(product(*list1)))
# [(1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6)]
중복 조합을 이러한 리스트 형태로 넣으면 각각 행에 해당하는 값들에서 하나씩 뽑아서 조합을 생성한다.