[PG] 튜플

nerry·2022년 5월 13일
0

알고리즘

목록 보기
84/86

문제

me

def solution(strings):
    strings = strings.split('}')
    answer =[]
    for i,string in enumerate(strings):
        strings[i]= string.strip('{').strip(',').strip('{').split(',')
    strings.sort(key=lambda x:len(x))
    for string in strings:
        if string == ['']: continue
        for s in string:
            if s in answer:
                if string.count(s)>answer.count(s): # 여러개라면 넣기
                    answer.append((s))
            else:
                answer.append((s))

    return [ int(a) for a in answer]

others

def solution(s):

    s = Counter(re.findall('\d+', s))
    return list(map(int, [k for k, v in sorted(s.items(), key=lambda x: x[1], reverse=True)]))

import re
from collections import Counter
  • 개수를 세어서 많은 순서대로,, 글쿤

def solution(s):
    answer = []

    s1 = s.lstrip('{').rstrip('}').split('},{')

    new_s = []
    for i in s1:
        new_s.append(i.split(','))

    new_s.sort(key = len) ####

    for i in new_s:
        for j in range(len(i)):
            if int(i[j]) not in answer:
                answer.append(int(i[j]))

    return answer
  • key = len 하면 알아서 길이대로 정렬해줌
  • 근데 중복된 원소가 있을 수 있습니다. ex : (2, 3, 1, 2) 이 조건에 반하는 것이 아닌가??
profile
터벅터벅 개발(은좋은)자 로그

0개의 댓글