[프로그래머스] 튜플

O2o2✨·2020년 11월 30일
0

알고리즘

목록 보기
9/43

링크: 프로그래머스 - 2019 카카오 개발자 겨울 인턴십 > 튜플

풀이

def solution(s):
    s = (s[1:-1] + ',').split('},')[:-1]
    groups = []
    
    for i in s:
        numbers = i[1:].split(',')
        groups.append(set(int(n) for n in numbers))
    
    groups.sort()
    answer = [list(groups[0])[0]]
    
    for i in range(len(groups)-1):
        num = [groups[i+1] - groups[i]]
        answer.append(list(groups[i+1] - groups[i])[0])
    
    return answer

길이 순 정렬 후 set해서 차집합해서 추가된 원소를 result에 추가
s = (s[1:-1] + ',').split('},')[:-1] # ['{20,111', '{111']


옛날 풀이(1년 전)

def solution(s):
    s = s[1:-1] 
    a = []
    st = ""
    result = []
    length = 1
    answer=[]
    
    for text in s:
        if text == '}': 
            a.append(st)
            st = ""
        elif text == ',' and st != "":
            st+= text
            
        elif text != '{' and text != ",":
            st += text
    
    a = tuple(a)
    
    while len(a) != len(result): 
        for i in range(len(a)):
            if len(a[i]) == length:     
                result.append(a[i])
        length += 1

    if (len(result) == 1):
        result[0] = int(result[0])
        return result
    
    for i in range(len(a)):
        result[i] = result[i].split(',')
        result[i] = set(result[i])

    for i in range(len(a)-1):
        if i == 0:
            answer += list(result[i+1] & result[i])
                
        answer += list(result[i+1] - result[i])
    
    for i in range(len(answer)):
        answer[i] = int(answer[i])
        
    return answer
profile
프론트엔드 & 퍼블리셔

0개의 댓글