swea 4834 숫자카드

nocarrotrabbit·2022년 4월 28일
0
post-thumbnail

4834 숫자카드

T = int(input())
for tc in range(1, T+1):
    N = int(input())
    nums = list(map(int,list(input())))

        #여기에 문제로직이 들어감
    cards_count = [0] * 10  #카드 갯수 리스트 만들기
    for card in nums:
        cards_count[card] += 1

    max_count = max(cards_count)    # 갯수중 최대값구하기
    index = max([i for i, ele in enumerate(cards_count) if ele == max_count]) #최대값 인덱스중 가장큰값구하기

        #최종 출력
    print('#{} {} {}'.format(tc, index, max_count))

단순히 최대값의 인덱스찾는방법으로 cards_count.index(max_count)를 하게 되면 최대값이 여러개인 경우 가장 처음나오는 인덱스가 선택된다 이를 해결하기 위해 enumerate 함수를 씀 이외에도 filter를 쓸수도있다고한다

enumerate/ filter 함수

lis=[1,0,1]
 
#filter 사용
list(filter(lambda e:lis[e] == 1, range(len(lis))))
[0, 2]
 
#enumerate 사용
[i for i, ele in enumerate(lis) if ele == 1]
[0, 2]

0개의 댓글