HackerRank Sales by Match

양말 짝을 찾는 문제다
dict에 양말 key를 넣고 개수를 누적한다
dict를 for문으로 돌면서 값을 2로 나눈 몫을 누적하고 응답한다

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the sockMerchant function below.
def sockMerchant(n, ar):
    socks_dict = {}
    socks = ar
    for sock in socks:
        if sock in socks_dict:
            socks_dict[sock] += 1
        else:
            socks_dict[sock] = 1
        
    s = 0
    for socks_count in socks_dict.values():
        s += socks_count // 2
        
    return s
            
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = sockMerchant(n, ar)

    fptr.write(str(result) + '\n')

    fptr.close()

https://www.hackerrank.com/challenges/sock-merchant/submissions/code/210576188

좋은 웹페이지 즐겨찾기