์ƒˆ์†Œ์‹

๐Ÿงฎ PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Python) - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

  • -

https://programmers.co.kr/learn/courses/30/lessons/92334

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๋ฌธ์ œ ์„ค๋ช… ์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์ง€๊ฐ€ ๊ฐœ๋ฐœํ•˜๋ ค๋Š” ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜

programmers.co.kr

ํ’€์ด

๋ณ„๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ• ์—†์ด ๋ฌธ์ œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

# ํ•œ๋ฒˆ์— ํ•œ ์œ ์ € ์‹ ๊ณ  ๊ฐ€๋Šฅ, ํ•œ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์‹ ๊ณ ์‹œ 1๋ฒˆ์œผ๋กœ ๊ฐ„์ฃผ
# K๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋˜๋ฉด ์ •์ง€
# ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์—๊ฒŒ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐ›๋Š”๋‹ค.
 
 
 
3๊ฐœ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ํ’€์—ˆ๋‹ค. ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ๋ณผ๋•Œ ํ•จ์ˆ˜๋ฅผ ๋‚˜๋ˆ„๋Š”๊ฒŒ ์ข‹์„์ง€ ์•„๋‹์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค....
 

# 0. (์‚ฌ์ „์ฒ˜๋ฆฌ) ๊ฐ™์€ ์‹ ๊ณ ๋ฅผ ํ•œ ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋ฏ€๋กœ ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ๋จผ์ € ํ•ด์ค€๋‹ค.

# 1๋ฒˆ ๋ฉ”์„œ๋“œ. ์‹ ๊ณ ์ž : [ํ”ผ ์‹ ๊ณ ์ž๋“ค] ํ˜•ํƒœ๋กœ ๋งŒ๋“ ๋‹ค.
# 2๋ฒˆ ๋ฉ”์„œ๋“œ. ํ”ผ ์‹ ๊ณ ์ž๋“ค๋งŒ ์นด์šดํŒ…ํ•ด์„œ ์ •์ง€๋‹นํ•œ ํ”ผ ์‹ ๊ณ ์ž์ฒดํฌ( K ๋ฒˆ ์ด์ƒ )
# 3๋ฒˆ ๋ฉ”์„œ๋“œ. ์ •์ง€๋ฆฌ์ŠคํŠธ์™€ ํ”ผ ์‹ ๊ณ ์ž๋“ค ๋ฆฌ์ŠคํŠธ ๋Œ€์กฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•  result๋ฐฐ์—ด ์ƒ์„ฑ
 
 
# ๋ฉ”์„œ๋“œ 3
def send_email(id_list, block_list, report_dict):
    result = [0 for _ in range(len(id_list))]
    idx = 0
    for user in id_list:
        for block_user in block_list:
            if None != report_dict.get(user) and report_dict[user].count(block_user):
                    result[idx] += 1
        idx += 1
    return result
# ๋ฉ”์„œ๋“œ 2
def block_user_check(id_list, reported_list ,k):
    block_list = set()
    for user in id_list:
        if reported_list.count(user) >= k :
            block_list.add(user)
    return list(block_list)
#๋ฉ”์„œ๋“œ 1
def transform_dict(report):
    report_dict = dict()
    reported_list = []
    for rep in report:
        report_user , reported_user = rep.split()
        reported_list.append(reported_user)
        if report_dict.get(report_user) == None  :
            report_dict[report_user] = [reported_user]
        else :
            report_dict[report_user].append(reported_user)
    return report_dict, reported_list

def solution(id_list, report, k):
    report = list(set(report)) # 0. ์ค‘๋ณต์ œ๊ฑฐ
    report_dict, reported_list = transform_dict(report)
    block_list = block_user_check(id_list, reported_list, k)
    return send_email(id_list,block_list, report_dict)
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.