๊ธ€ ์ž‘์„ฑ์ž: ํƒ์‹œ ์šด์ „์‚ฌ
๋ฐ˜์‘ํ˜•

 

KAKAO BLIND RECRUITMENT

๐Ÿ‘ฉ‍๐Ÿ’ผ ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ

 

๐Ÿ˜ƒ ๋‚˜์˜ ์ฝ”๋“œ

import re
from collections import Counter as mset

p = re.compile("[a-z]{2}")

def multiSet(str):
    lst = []
    for idx in range(len(str)-1):
        if p.match(str[idx:idx+2]):
            lst.append(str[idx:idx+2])
    return lst

def solution(str1, str2):
    lst1 = multiSet(str1.lower())
    lst2 = multiSet(str2.lower())
    len_lst1 = len(lst1)
    len_lst2 = len(lst2)
    if len_lst1 == 0 and len_lst2 == 0:
        return 65536
    mset1 = mset(lst1)
    mset2 = mset(lst2)
    inter_lst = list((mset1 & mset2).elements())
    len_inter_lst = len(inter_lst)
    len_union_lst = len_lst1 + len_lst2 - len_inter_lst
    return int(len_inter_lst/len_union_lst *65536)

์ž์นด๋“œ ์œ ์‚ฌ๋„๋ผ๋Š” ๊ธฐ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ๋‘ ์ง‘ํ•ฉ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์œ ์‚ฌ๋„๋ฅผ ๊ฒ€์‚ฌํ•  ๋•Œ ๊ฐ ๋ฌธ์ž์—ด์„ ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ์›์†Œ๋กœ ๋งŒ๋“ ๋‹ค.ํ•˜๊ณ  ์˜๋ฌธ์ž๋กœ ๋œ ๊ธ€์ž ์Œ๋งŒ ์œ ํšจํ•˜๋‹ค ํ–ˆ์œผ๋‹ˆ, ์ฒ˜์Œ์œผ๋กœ ํ•ด์•ผํ•  ๊ฑด ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋‹ค์ค‘ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

def multiSet(str):
    lst = []
    for idx in range(len(str)-1):
        if p.match(str[idx:idx+2]):
            lst.append(str[idx:idx+2])
    return lst

๋‘ ๋ฌธ์ž์—ด์— ๊ฐ™์€ ์‹œํ–‰์„ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— multiSet์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. multiSet์€ ๋Œ€์†Œ๋ฌธ์ž ์ฐจ์ด๋Š” ๋ฌด์‹œํ•œ๋‹ค๋ผ๋Š” ์กฐ๊ฑด์— ์˜ํ•ด ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋‹ค์ค‘์ง‘ํ•ฉ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ ๋’ค ์•ž์—์„œ ๋ถ€ํ„ฐ ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด์„œ match๋ฅผ ์ด์šฉํ•ด ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋†“์€ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

p = re.compile("[a-z]{2}")

์ •๊ทœ ํ‘œํ˜„์‹์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๋‘ ์ž๋ฆฌ๋ฉด ํ•ด๋‹นํ•˜๋Š” match ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์•„๋‹ˆ๋ฉด None์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ๋‘ ๊ธ€์ž ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฐ๊ณผ ๊ฐ’์—์„œ ๋‘ ๋ฐฐ์—ด ๋ชจ๋‘ ํฌ๊ธฐ๊ฐ€ 0์ผ ์‹œ ๋ฐ”๋กœ 65536์„ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต์„ ํฌํ•จํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— set ์ž๋ฃŒํ˜•์œผ๋กœ ์ง„ํ–‰ํ•  ์‹œ ์ค‘๋ณต์ด ์‚ฌ๋ผ์ ธ์„œ ๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” ์—ฌ๊ธฐ์„œ Python ๋‚ด์žฅ ๋ชจ๋“ˆ์ธ collections์˜ Counter๋ฅผ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. Counter๋Š” dict ์ž๋ฃŒํ˜•์˜ ์„œ๋ธŒ ํด๋ž˜์Šค๋กœ list๋ฅผ Counter๋กœ ๋ณ€ํ™˜ํ•˜๊ฒŒ ๋˜๋ฉด list์˜ ์š”์†Œ๋ฅผ key๋กœ ๋“ฑ์žฅํ•œ ๋ฐฐ์—ด ๋‚ด์—์„œ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ value๋กœ ํ•˜๋Š” Counter ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Counter ๊ฐ์ฒด๋Š” set๊ณผ ๋น„์Šทํ•˜๊ฒŒ & ์—ฐ์‚ฐ๊ณผ |์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. &์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ Counter์˜ key๋ณ„๋กœ value์˜ ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ง„ Counter ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , |์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€๊ฐ’์„ ๊ฐ€์ง„ Counter ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

mset1 = mset(lst1)
mset2 = mset(lst2)
inter_lst = list((mset1 & mset2).elements())

์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ๊ฑด &์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. multiSet์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋‘ ๋‹ค์ค‘ ์ง‘ํ•ฉ์„ Counter ๊ฐ์ฒด๋กœ ๋งŒ๋“  ๋’ค, ๋‘ Counter ๊ฐ์ฒด์— &์—ฐ์‚ฐ์„ ํ•˜์—ฌ ๊ต์ง‘ํ•ฉ์˜ Counter๊ฐ์ฒด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋’ค ๋‹ค์‹œ list์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

len_inter_lst = len(inter_lst)
len_union_lst = len_lst1 + len_lst2 - len_inter_lst
return int(len_inter_lst/len_union_lst *65536)

์ด์ œ ๋ชจ๋“  ๊ณผ์ •์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ต์ง‘ํ•ฉ์— ํ•ด๋‹นํ•˜๋Š” list์˜ ๊ธธ์ด๋ฅผ ์ธก์ •ํ•˜์—ฌ ์ด๋ฅผ ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ๋‘ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๋”ํ•˜๊ณ  ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๋นผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ๋งˆ๋ฌด๋ฆฌ ๋ฉ๋‹ˆ๋‹ค.

 

Written with StackEdit.

 

๋ฐ˜์‘ํ˜•