[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] 2021 KAKAO BLIND RECRUITMENT : ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

2021. 6. 29. 15:26

๋ฌธ์ œ ์„ค๋ช…

์นด์นด์˜ค์— ์ž…์‚ฌํ•œ ์‹ ์ž… ๊ฐœ๋ฐœ์ž ๋„ค์˜ค๋Š” "์นด์นด์˜ค๊ณ„์ •๊ฐœ๋ฐœํŒ€"์— ๋ฐฐ์น˜๋˜์–ด, ์นด์นด์˜ค ์„œ๋น„์Šค์— ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์˜ ์•„์ด๋””๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—…๋ฌด๋ฅผ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. "๋„ค์˜ค"์—๊ฒŒ ์ฃผ์–ด์ง„ ์ฒซ ์—…๋ฌด๋Š” ์ƒˆ๋กœ ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์ด ์นด์นด์˜ค ์•„์ด๋”” ๊ทœ์น™์— ๋งž์ง€ ์•Š๋Š” ์•„์ด๋””๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ์ž…๋ ฅ๋œ ์•„์ด๋””์™€ ์œ ์‚ฌํ•˜๋ฉด์„œ ๊ทœ์น™์— ๋งž๋Š” ์•„์ด๋””๋ฅผ ์ถ”์ฒœํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋‹ค์Œ์€ ์นด์นด์˜ค ์•„์ด๋””์˜ ๊ทœ์น™์ž…๋‹ˆ๋‹ค.

  • ์•„์ด๋””์˜ ๊ธธ์ด๋Š” 3์ž ์ด์ƒ 15์ž ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์•„์ด๋””๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‹จ, ๋งˆ์นจํ‘œ(.)๋Š” ์ฒ˜์Œ๊ณผ ๋์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋˜ํ•œ ์—ฐ์†์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

"๋„ค์˜ค"๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด 7๋‹จ๊ณ„์˜ ์ˆœ์ฐจ์ ์ธ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ํ†ตํ•ด ์‹ ๊ทœ ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ์•„์ด๋””๊ฐ€ ์นด์นด์˜ค ์•„์ด๋”” ๊ทœ์น™์— ๋งž๋Š” ์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  ๊ทœ์น™์— ๋งž์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ทœ์น™์— ๋งž๋Š” ์ƒˆ๋กœ์šด ์•„์ด๋””๋ฅผ ์ถ”์ฒœํ•ด ์ฃผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์‹ ๊ทœ ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ์•„์ด๋””๊ฐ€ new_id ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด,

  • 1๋‹จ๊ณ„ new_id์˜ ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ๋Œ€์‘๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • 2๋‹จ๊ณ„ new_id์—์„œ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • 3๋‹จ๊ณ„ new_id์—์„œ ๋งˆ์นจํ‘œ(.)๊ฐ€ 2๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ ๋ถ€๋ถ„์„ ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ(.)๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • 4๋‹จ๊ณ„ new_id์—์„œ ๋งˆ์นจํ‘œ(.)๊ฐ€ ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • 5๋‹จ๊ณ„ new_id๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, new_id์— "a"๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
  • 6๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด, new_id์˜ ์ฒซ 15๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ œ๊ฑฐ ํ›„ ๋งˆ์นจํ‘œ(.)๊ฐ€ new_id์˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ๋์— ์œ„์น˜ํ•œ ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • 7๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ผ๋ฉด, new_id์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ new_id์˜ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ๋์— ๋ถ™์ž…๋‹ˆ๋‹ค.

 

๋ฌธ์ œ

์‹ ๊ทœ ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ์•„์ด๋””๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” new_id๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, "๋„ค์˜ค"๊ฐ€ ์„ค๊ณ„ํ•œ 7๋‹จ๊ณ„์˜ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„์˜ ์ถ”์ฒœ ์•„์ด๋””๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

new_id๋Š” ๊ธธ์ด 1 ์ด์ƒ 1,000 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
new_id๋Š” ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
new_id์— ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋Š” ํŠน์ˆ˜๋ฌธ์ž๋Š” -_.~!@#$%^&*()=+[{]}:?,<>/ ๋กœ ํ•œ์ •๋ฉ๋‹ˆ๋‹ค.

 

์ •๋‹ต

def solution(new_id):
    answer = ''

    # 1๋‹จ๊ณ„ : ๋ชจ๋“  ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜
    new_id = new_id.lower()

    # 2๋‹จ๊ณ„ : ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๊ฐ€๋Šฅ ๋ฌธ์ž ์ œ์™ธ ์ œ๊ฑฐ ํ›„ answer์— ์ €์žฅ
    sp = ["-", "_", "."]
    for i in new_id:
        if i.isalnum() or i in sp:
            answer += i

    # 3๋‹จ๊ณ„ : .๊ฐ€ ๋‘๋ฒˆ ์ด์ƒ ๋‚˜์˜ฌ ๋•Œ . ํ•˜๋‚˜๋กœ ์น˜ํ™˜
    while ".." in answer:
        answer = answer.replace("..", ".")

    # 4๋‹จ๊ณ„ : ์ฒ˜์Œ์ด๋‚˜ ๋์— . ์œ„์น˜ํ•˜๋ฉด ์ œ๊ฑฐ
    if answer and answer[0] == ".":
        answer = answer[1:]
    if answer and answer[-1] == ".":
        answer = answer[:-1]

    # 5๋‹จ๊ณ„ : ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด a ๋Œ€์ž…
    if answer == "":
        return "aaa"

    # 6๋‹จ๊ณ„ : 16์ž ์ด์ƒ์ด๋ฉด 16์ž๋ถ€ํ„ฐ ์‚ญ์ œ, ๋งˆ์ง€๋ง‰ ๋ฌธ์ž .์ด๋ฉด ์‚ญ์ œ
    if len(answer) >= 16:
        answer = answer[:15]
        if answer[-1] == ".":
            answer = answer[:-1]

    # 7๋‹จ๊ณ„ : 2์ž ์ดํ•˜๋ผ๋ฉด ๋งˆ์ง€๋ง‰ ๋ฌธ์ž 3์ž ๋ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    elif len(answer) <= 2:
        answer += answer[-1]*(3-len(answer))

    return answer

3๋‹จ๊ณ„์—์„œ while๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ".."์ด answer์— ํฌํ•จ๋  ๋™์•ˆ ".."์„ "."์œผ๋กœ ์น˜ํ™˜ํ•œ๋‹ค.

"...."์ผ ๊ฒฝ์šฐ๋„ "..." -> ".." -> "." ์œผ๋กœ ๋ฐ”๋€Œ๋ฏ€๋กœ .์ด ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜ค๋”๋ผ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

4๋‹จ๊ณ„์—์„œ answer์˜ ์ฒซ๋ฌธ์ž์™€ ๋๋ฌธ์ž๋ฅผ ํ™•์ธํ•  ๋•Œ answer๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ ์ œ๊ฑฐํ•ด์•ผํ•œ๋‹ค.

answer๋ฅผ ํ™•์ธํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ answer[0]์™€ answer[-1]์„ ํ™•์ธํ–ˆ์„ ๋•Œ

answer๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด ์ธ๋ฑ์Šค ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

 

5๋‹จ๊ณ„์—์„œ ๋นˆ ๋ฌธ์ž์—ด์ผ ๋•Œ a๋ฅผ ๋Œ€์ž…ํ•œ ํ›„ 7๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์•ผํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ aaa๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—

๋ฐ”๋กœ aaa๋ฅผ returnํ•œ๋‹ค.

BELATED ARTICLES

more