1 minute read

1. 개요

2. 신규 아이디 추천 풀이

import re

def solution(new_id):
    # 1-2단계
    new_id = "".join(re.findall("[a-z0-9-_.]", new_id.lower()))
    
    # 3-4단계
    new_id = ".".join(re.split("[.]{2,}", new_id)).strip(".")
    
    # 5단계
    if not new_id:
        new_id = "a"
        
    # 6단계
    new_id = new_id[:15].strip(".")
    
    # 7단계
    if len(new_id) <= 2:
        new_id = "".join((new_id + new_id[-1]*3)[:3])
    
    return new_id

3. 문자열 나누기 풀이

def solution(s):
    answer = i = 0
    
    while i < len(s):
        start, same, diff = s[i], 0, 0

        while i < len(s):
            if s[i] == start:
                same += 1
            else:
                diff += 1
            i += 1
            
            if same==diff:
                answer += 1
                break
    
    # same이랑 diff가 다르다면 맨마지막에 짤린거므로 1을 추가해준다.
    return answer + (same!=diff)

4. 성격 유형 검사하기 풀이

def solution(surveys, choices):
    compared = {"R":"T", "C":"F", "J":"M", "A":"N"} #성격의 쌍을 관리한다. key가 value보다 앞선다.
    scores = {"R":0, "C":0, "J":0, "A":0, "T":0, "F":0, "M":0, "N":0} #성격마다 점수를 관리
    choicer = [0,3,2,1,0,1,2,3] # 선택지에 따라 부여되는 점수를 관리한다.
    answer = [] # 답을 저장할 리스트를 관리한다.
    
    # 성격별로 점수를 증가시킨다.
    for survey, choice in zip(surveys, choices):
        left, right = survey[0], survey[1]
        if choice < 4:
            scores[left] += choicer[choice]
        else:
            scores[right] += choicer[choice]

    for left in compared:
        right = compared[left]

        if scores[left] >= scores[right]:
            answer.append(left)
        else:
            answer.append(right)

    return ''.join(answer)

5. 햄버거 만들기 풀이

def solution(ingredient):
    stack = []
    answer = 0
    
    for c in ingredient:
        # stack에 주어진 재료를 쌓는다.
        stack.append(c)
        
        # stack에 쌓인 재료로 햄버거를 만들수 있으면 즉시 제조한다.
        while len(stack)>=4 and stack[-4:]==[1,2,3,1]:
            answer += 1
            for _ in range(4):
                stack.pop()
    return answer

Leave a comment