Python/BAEKJOON

[백준] 1431번 시리얼번호(python)

Magin 2022. 2. 15. 18:47
728x90

문제)

 

 

 

알고리즘)

- 정렬 람다(lambda) 함수 활용 조건 우선순위

1. 길이  2. 숫자총합  3. 알파벳 사전순

 

- def f(a) 함수  i.isdigit() 함수 사용 

isdigit => 문자열내에 숫자가 있다면 T/F 반환

각 문자열의 숫자가 있다면 result에 숫자변환 후 더한 후 result 출력

 

람다 정렬 후 최종출력  

코드)

#시리얼 번호
import sys
input = sys.stdin.readline
n = int(input())

data = [input().rstrip() for _ in range(n)]

def f(a):
    result = 0
    for i in a:
        if i.isdigit(): #숫자가 있다면?
            result += int(i)

    return result

data.sort(key=lambda x:(len(x), f(x), x)) #조건(길이, 숫자총합, 알파벳)
for x in data:
    print(x)
728x90