본문 바로가기

개발/algorithm

[프로그래머스][level3] 불량 사용자 -python

문제링크

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

 

코딩테스트 연습 - 불량 사용자

개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는 최근 진행된 카카오이모티콘 이벤트에 비정상적인 방법으로 당첨을 시도한 응모자들을 발견하였습니다. 이런 응모자들을 따로 모아 불량

programmers.co.kr

풀이 

  • 처음엔 조합을 사용해야하나 생각했지만, id 별로 구분이 안되므로 순열 사용 
  • 이 후 set으로 변환하여 가능한 조합 중복 제거해줌 

from itertools import permutations 

def solution(user_id, banned_id):
  # 가능한 banned_id 조합 모두. 어떤 banned_id에 해당되는지에 따라 결과 달라지므로 순열 사용 
  permute = list(permutations(user_id,len(banned_id)))
  answer = []
  for users in permute:
      # banned_id 에 해당하는지 체크 
    if not check(users,banned_id):
      continue
      # banned_id의 조합에 해당될 수 있으면 
    else :
        # set으로 변환하여 중복 제거 
      users = set(users)
      # answer에 없으면 추가 
      if users not in answer:
        answer.append(users)
    # 개수 반환 
  return len(answer)

def check(users, banned_id):
  
  for i in range(len(users)):
    # 길이 다르면 같지 않음 
    if len(users[i]) != len(banned_id[i]):
      return False

    for j in range(len(users[i])):
        # *이면 continue 
      if banned_id[i][j] == '*':
        continue
        # 한 글자라도 다르면 같지 않음 
      if users[i][j] != banned_id[i][j]:
        return False
  
  return True