๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[Programmers] [KAKAO BLIND RECRUITMENT] [Python] Level2_[1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

Dbswnstjd 2022. 10. 26. 02:54

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/17677

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ’€์ด

Counter ๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด

# [Programmers] [KAKAO BLIND RECRUITMENT] [Python] Level2_[1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง
from collections import Counter
def solution(str1, str2):
    str1 = str1.lower()
    str2 = str2.lower()
    new_str1 = []
    new_str2 = []

    for i in range(1, len(str1)):
        if str1[i].isalpha() and str1[i-1].isalpha():
            new_str1.append(str1[i-1] + str1[i])
    for j in range(1, len(str2)):
        if str2[j].isalpha() and str2[j-1].isalpha():
            new_str2.append(str2[j-1] + str2[j])
    Counter1 = Counter(new_str1)
    Counter2 = Counter(new_str2)

    intersection = list((Counter1 & Counter2).elements())
    union = list((Counter1 | Counter2).elements())

    if len(union) == 0 and len(intersection) == 0:
        return 65536
    return int(len(intersection) / len(union) * 65536)
print(solution('aa1+aa2', 'AAAA12'))

 

 

Counter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ํ’€์ด

# [Programmers] [KAKAO BLIND RECRUITMENT] [Python] Level2_[1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง
def solution2(str1, str2):
    answer = 0
    str1 = str1.lower()
    str2 = str2.lower()
    new_str1 = []
    new_str2 = []
    intersection = [] # ๊ต์ง‘ํ•ฉ
    union = [] # ํ•ฉ์ง‘ํ•ฉ

    for i in range(1, len(str1)):
        if str1[i].isalpha() and str1[i-1].isalpha():
            new_str1.append(str1[i-1] + str1[i])
    for j in range(1, len(str2)):
        if str2[j].isalpha() and str2[j-1].isalpha():
            new_str2.append(str2[j-1] + str2[j])
    
    for string in new_str1:
        if string in new_str2:
            intersection.append(string)
            union.append(string)
            new_str2.remove(string)
        else:
            union.append(string)
    for k in range(len(new_str2)):
        union.append(new_str2[k])
    
    if len(union) == 0 and len(intersection) == 0:
        return 65536
    return int((len(intersection) / len(union)) * 65536)
print(solution2('aa1+aa2', 'AAAA12'))

 

 

ํ•ฉ์ง‘ํ•ฉ๊ณผ ๊ต์ง‘ํ•ฉ์ด 0 ์ผ๋•Œ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์•„ ์‹œ๊ฐ„์„ ๋งŽ์ด ๋‚ญ๋น„ํ•˜์˜€๋‹ค. ์ฒ˜์Œ์—๋Š” ํ•ฉ์ง‘ํ•ฉ์ด 0์ผ๊ฒฝ์šฐ๋งŒ ์ƒ๊ฐํ•˜์—ฌ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ์—ˆ๋‹ค.