๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2018 KAKAO BLIND RECRUITMENT] [1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง(Lv.2) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 8.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

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

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

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

1. makeJ ํ•จ์ˆ˜

def makeJ(str):
    alphabet = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']
    
    strArr = []
    for i in range(1, len(str)):
        if str[i-1] in alphabet and str[i] in alphabet:
            strArr.append(str[i-1]+str[i])
    
    return strArr
  • makeJ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์—์„œ ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด ๋‹ค์ค‘์ง‘ํ•ฉ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋‹จ, ๋‘ ๊ธ€์ž๊ฐ€ ๋ชจ๋‘ ์˜๋ฌธ์ž์ผ ๊ฒฝ์šฐ์—๋งŒ ๋‹ค์ค‘์ง‘ํ•ฉ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.
  • alphabet ๋ฆฌ์ŠคํŠธ๋Š” ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์„ ์ €์žฅํ•œ ๋ฆฌ์ŠคํŠธ๋กœ, ๋‘ ๊ธ€์ž๊ฐ€ ๋ชจ๋‘ ์ด ๋ฆฌ์ŠคํŠธ์— ์†ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด์„ ๋‘ ๊ธ€์ž์”ฉ ์ž˜๋ผ์„œ ๋‹ค์ค‘์ง‘ํ•ฉ strArr์— ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” str[i-1] ๊ณผ str[i]๋กœ ๋‘ ๊ธ€์ž๋ฅผ ์กฐํ•ฉํ•œ ํ›„ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

2. ๋Œ€๋ฌธ์ž ๋ณ€ํ™˜ ๋ฐ ๋‹ค์ค‘์ง‘ํ•ฉ ์ƒ์„ฑ

def solution(str1, str2):
    answer = 0
    
    # ๋Œ€๋ฌธ์ž๋กœ ๋งž์ถ”๊ณ  ๋‹ค์ค‘์ง‘ํ•ฉ ์ƒ์„ฑ
    str1Arr = makeJ(str1.upper()) 
    str2Arr = makeJ(str2.upper())
  • ์ž…๋ ฅ๋œ ๋‘ ๋ฌธ์ž์—ด str1 ๊ณผ str2๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ makeJ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ๋‹ค์ค‘์ง‘ํ•ฉ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • str1Arr์™€ str2Arr๋Š” ๊ฐ๊ฐ์˜ ๋‹ค์ค‘์ง‘ํ•ฉ์„ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.

3. ๊ต์ง‘ํ•ฉ ๋ฐ ํ•ฉ์ง‘ํ•ฉ ๊ณ„์‚ฐ

    a = []      # ๊ต์ง‘ํ•ฉ
    b = []      # ํ•ฉ์ง‘ํ•ฉ
    
    # ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
    for i in str1Arr:
        if i in str2Arr and i not in a:
            minStrLen = min(str1Arr.count(i), str2Arr.count(i))
            for j in range(minStrLen):
                a.append(i)
            # ํ•ฉ์ง‘ํ•ฉ์—์„œ ์“ธ max ๋ฒ„์ „
            maxStrLen = max(str1Arr.count(i), str2Arr.count(i))
            for j in range(maxStrLen):
                b.append(i)
  • a: ๊ต์ง‘ํ•ฉ์„ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ
  • b: ํ•ฉ์ง‘ํ•ฉ์„ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ
  1. ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ:
    • str1Arr์— ์žˆ๋Š” ์›์†Œ ์ค‘ str2Arr์—๋„ ์กด์žฌํ•˜๋Š” ์›์†Œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
    • ํ•ด๋‹น ์›์†Œ๊ฐ€ ์ด๋ฏธ a(๊ต์ง‘ํ•ฉ) ๋ฆฌ์ŠคํŠธ์— ์—†๋‹ค๋ฉด, ๊ทธ ์›์†Œ์˜ ์ตœ์†Œ ๋“ฑ์žฅ ํšŸ์ˆ˜๋งŒํผ a๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
    • ํ•ฉ์ง‘ํ•ฉ์—์„œ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ๊ทธ ์›์†Œ์˜ ์ตœ๋Œ€ ๋“ฑ์žฅ ํšŸ์ˆ˜ ๋งŒํผ b๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋‚˜๋จธ์ง€ ํ•ฉ์ง‘ํ•ฉ ์›์†Œ ์ถ”๊ฐ€
    # ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
    for i in str1Arr:
        if i not in a:
            b.append(i)
    for i in str2Arr:
        if i not in a:
            b.append(i)
  • ๊ต์ง‘ํ•ฉ์— ์—†๋Š” ์›์†Œ๋ฅผ str1Arr๊ณผ str2Arr์—์„œ ์ฐพ์•„์„œ b(ํ•ฉ์ง‘ํ•ฉ)์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

4. ์ž์นด๋“œ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ ๋ฐ ๋ณ€ํ™˜

    if len(a) == 0 and len(b) == 0:
        answer = 1
    else:
        answer = len(a)/len(b)
    
    answer = int(answer*65536)
    
    return answer

 

  • ๊ต์ง‘ํ•ฉ๊ณผ ํ•ฉ์ง‘ํ•ฉ์ด ๋ชจ๋‘ ๋น„์–ด ์žˆ๋Š” ๊ฒฝ์šฐ(๋‘ ๋‹ค์ค‘์ง‘ํ•ฉ์ด ๊ณต์ง‘ํ•ฉ), ์œ ์‚ฌ๋„๋Š” 1๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด, ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ / ํ•ฉ์ง‘ํ•ฉ ํฌ๊ธฐ๋กœ ์ž์นด๋“œ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ์œ ์‚ฌ๋„์— 65536์„ ๊ณฑํ•œ ํ›„, ์†Œ์ˆ˜์  ์•„๋ž˜๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ตœ์ข… ์ •๋‹ต ์ฝ”๋“œ

 

def makeJ(str):
    alphabet = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']
    
    strArr = []
    for i in range(1, len(str)):
        if str[i-1] in alphabet and str[i] in alphabet:
            strArr.append(str[i-1]+str[i])
    
    return strArr

def solution(str1, str2):
    answer = 0
    
    # ๋Œ€๋ฌธ์ž๋กœ ๋งž์ถ”๊ณ  ๋‹ค์ค‘์ง‘ํ•ฉ ์ƒ์„ฑ
    str1Arr = makeJ(str1.upper()) 
    str2Arr = makeJ(str2.upper())
    
    a = []      # ๊ต์ง‘ํ•ฉ
    b = []      # ํ•ฉ์ง‘ํ•ฉ
    
    # ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
    for i in str1Arr:
        if i in str2Arr and i not in a:
            minStrLen = min(str1Arr.count(i), str2Arr.count(i))
            for j in range(minStrLen):
                a.append(i)
            # ํ•ฉ์ง‘ํ•ฉ์—์„œ ์“ธ max ๋ฒ„์ „
            maxStrLen = max(str1Arr.count(i), str2Arr.count(i))
            for j in range(maxStrLen):
                b.append(i)
    
    
    # ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
    for i in str1Arr:
        if i not in a:
            b.append(i)
    for i in str2Arr:
        if i not in a:
            b.append(i)
    
    if len(a) == 0 and len(b) == 0:
        answer = 1
    else:
        answer = len(a)/len(b)
    
    answer = int(answer*65536)
    
        
    return answer
728x90
๋ฐ˜์‘ํ˜•