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

๐Ÿ“ ํŒŒ์ผ๋ช… ์ •๋ ฌ - [3์ฐจ] 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ / KAKAO BLIND RECRUITMENT

๋ฌธ์ œ ์„ค๋ช…

ํŒŒ์ผ๋ช… ์ •๋ ฌ

์„ธ ์ฐจ๋ก€์˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์™€ ๋‘ ์ฐจ๋ก€์˜ ๋ฉด์ ‘์ด๋ผ๋Š” ๊ธฐ๋‚˜๊ธด ๋ธ”๋ผ์ธ๋“œ ๊ณต์ฑ„๋ฅผ ๋ฌด์‚ฌํžˆ ํ†ต๊ณผํ•ด ์นด์นด์˜ค์— ์ž…์‚ฌํ•œ ๋ฌด์ง€๋Š” ํŒŒ์ผ ์ €์žฅ์†Œ ์„œ๋ฒ„ ๊ด€๋ฆฌ๋ฅผ ๋งก๊ฒŒ ๋˜์—ˆ๋‹ค.

์ €์žฅ์†Œ ์„œ๋ฒ„์—๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ๊ณผ๊ฑฐ ๋ฒ„์ „์„ ๋ชจ๋‘ ๋‹ด๊ณ  ์žˆ์–ด, ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ํŒŒ์ผ ๋ชฉ๋ก์€ ๋ณด๊ธฐ๊ฐ€ ๋ถˆํŽธํ–ˆ๋‹ค. ํŒŒ์ผ์„ ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด ๋‚˜์ค‘์— ๋งŒ๋“ค์–ด์ง„ ver-10.zip์ด ver-9.zip๋ณด๋‹ค ๋จผ์ € ํ‘œ์‹œ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ฒ„์ „ ๋ฒˆํ˜ธ ์™ธ์—๋„ ์ˆซ์ž๊ฐ€ ํฌํ•จ๋œ ํŒŒ์ผ ๋ชฉ๋ก์€ ์—ฌ๋Ÿฌ ๋ฉด์—์„œ ๊ด€๋ฆฌํ•˜๊ธฐ ๋ถˆํŽธํ–ˆ๋‹ค. ์˜ˆ์ปจ๋Œ€ ํŒŒ์ผ ๋ชฉ๋ก์ด [img12.png, img10.png, img2.png, img1.png]์ผ ๊ฒฝ์šฐ, ์ผ๋ฐ˜์ ์ธ ์ •๋ ฌ์€ [img1.png, img10.png, img12.png, img2.png] ์ˆœ์ด ๋˜์ง€๋งŒ, ์ˆซ์ž ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ [img1.png, img2.png, img10.png, img12.png"] ์ˆœ์ด ํ›จ์”ฌ ์ž์—ฐ์Šค๋Ÿฝ๋‹ค.

๋ฌด์ง€๋Š” ๋‹จ์ˆœํ•œ ๋ฌธ์ž ์ฝ”๋“œ ์ˆœ์ด ์•„๋‹Œ, ํŒŒ์ผ๋ช…์— ํฌํ•จ๋œ ์ˆซ์ž๋ฅผ ๋ฐ˜์˜ํ•œ ์ •๋ ฌ ๊ธฐ๋Šฅ์„ ์ €์žฅ์†Œ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์— ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

์†Œ์Šค ํŒŒ์ผ ์ €์žฅ์†Œ์— ์ €์žฅ๋œ ํŒŒ์ผ๋ช…์€ 100 ๊ธ€์ž ์ด๋‚ด๋กœ, ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž, ์ˆซ์ž, ๊ณต๋ฐฑ(" ), ๋งˆ์นจํ‘œ(.), ๋นผ๊ธฐ ๋ถ€ํ˜ธ(-")๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ํŒŒ์ผ๋ช…์€ ์˜๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ, ์ˆซ์ž๋ฅผ ํ•˜๋‚˜ ์ด์ƒ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.

ํŒŒ์ผ๋ช…์€ ํฌ๊ฒŒ HEAD, NUMBER, TAIL์˜ ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

HEAD๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์ตœ์†Œํ•œ ํ•œ ๊ธ€์ž ์ด์ƒ์ด๋‹ค.

NUMBER๋Š” ํ•œ ๊ธ€์ž์—์„œ ์ตœ๋Œ€ ๋‹ค์„ฏ ๊ธ€์ž ์‚ฌ์ด์˜ ์—ฐ์†๋œ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์•ž์ชฝ์— 0์ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 0๋ถ€ํ„ฐ 99999 ์‚ฌ์ด์˜ ์ˆซ์ž๋กœ, 00000์ด๋‚˜ 0101 ๋“ฑ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

TAIL์€ ๊ทธ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์œผ๋กœ, ์—ฌ๊ธฐ์—๋Š” ์ˆซ์ž๊ฐ€ ๋‹ค์‹œ ๋‚˜ํƒ€๋‚  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์•„๋ฌด ๊ธ€์ž๋„ ์—†์„ ์ˆ˜ ์žˆ๋‹ค.

ํŒŒ์ผ๋ช…์„ ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆˆ ํ›„, ๋‹ค์Œ ๊ธฐ์ค€์— ๋”ฐ๋ผ ํŒŒ์ผ๋ช…์„ ์ •๋ ฌํ•œ๋‹ค.

  • ํŒŒ์ผ๋ช…์€ ์šฐ์„  HEAD ๋ถ€๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ์ด๋•Œ, ๋ฌธ์ž์—ด ๋น„๊ต ์‹œ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. MUZI์™€ muzi, MuZi๋Š” ์ •๋ ฌ ์‹œ์— ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ทจ๊ธ‰๋œ๋‹ค.
  • ํŒŒ์ผ๋ช…์˜ HEAD ๋ถ€๋ถ„์ด ๋Œ€์†Œ๋ฌธ์ž ์ฐจ์ด ์™ธ์—๋Š” ๊ฐ™์„ ๊ฒฝ์šฐ, NUMBER์˜ ์ˆซ์ž ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. 9 < 10 < 0011 < 012 < 13 < 014 ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค. ์ˆซ์ž ์•ž์˜ 0์€ ๋ฌด์‹œ๋˜๋ฉฐ, 012์™€ 12๋Š” ์ •๋ ฌ ์‹œ์— ๊ฐ™์€ ๊ฐ™์€ ๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
  • ๋‘ ํŒŒ์ผ์˜ HEAD ๋ถ€๋ถ„๊ณผ, NUMBER์˜ ์ˆซ์ž๋„ ๊ฐ™์„ ๊ฒฝ์šฐ, ์›๋ž˜ ์ž…๋ ฅ์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•œ๋‹ค. MUZI01.zip๊ณผ muzi1.png๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด, ์ •๋ ฌ ํ›„์—๋„ ์ž…๋ ฅ ์‹œ ์ฃผ์–ด์ง„ ๋‘ ํŒŒ์ผ์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.

๋ฌด์ง€๋ฅผ ๋„์™€ ํŒŒ์ผ๋ช… ์ •๋ ฌ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜๋ผ.

์ž…๋ ฅ ํ˜•์‹

์ž…๋ ฅ์œผ๋กœ ๋ฐฐ์—ด files๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

  • files๋Š” 1000 ๊ฐœ ์ดํ•˜์˜ ํŒŒ์ผ๋ช…์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด๋‹ค.
  • ๊ฐ ํŒŒ์ผ๋ช…์€ 100 ๊ธ€์ž ์ดํ•˜ ๊ธธ์ด๋กœ, ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž, ์ˆซ์ž, ๊ณต๋ฐฑ(" ), ๋งˆ์นจํ‘œ(.), ๋นผ๊ธฐ ๋ถ€ํ˜ธ(-")๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ํŒŒ์ผ๋ช…์€ ์˜๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ, ์ˆซ์ž๋ฅผ ํ•˜๋‚˜ ์ด์ƒ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ค‘๋ณต๋œ ํŒŒ์ผ๋ช…์€ ์—†์œผ๋‚˜, ๋Œ€์†Œ๋ฌธ์ž๋‚˜ ์ˆซ์ž ์•ž๋ถ€๋ถ„์˜ 0 ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ํ•จ๊ป˜ ์ฃผ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. (muzi1.txt, MUZI1.txt, muzi001.txt, muzi1.TXT๋Š” ํ•จ๊ป˜ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค.)

์ถœ๋ ฅ ํ˜•์‹

์œ„ ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ˜ƒ ๋‚˜์˜ ํ’€์ด

import re
import functools

headParser = re.compile('^([a-zA-Z]|-|\s)+')
numberParser = re.compile('(\d+)')

def compare(file1, file2):
    lst1 = file1.split('.')
    name1 = lst1[0]
    head1Match = headParser.search(name1)
    head1 = head1Match.group().lower()
    number1Match = numberParser.search(name1)
    number1 = number1Match.group()
    tail1 = file1[number1Match.endpos:]

    lst2 = file2.split('.')
    name2 = lst2[0]
    head2Match = headParser.search(name2)
    head2 = head2Match.group().lower()
    number2Match = numberParser.search(name2)
    number2 = number2Match.group()
    tail2 = file2[number2Match.endpos:]

    print(head1, int(number1), head2, int(number2))
    for i in range(min(len(head1), len(head2))):
        if ord(head1[i])!=ord(head2[i]):
            return ord(head1[i]) - ord(head2[i])
        else:
            if i != min(len(head1), len(head2))-1:
                continue
            elif i == min(len(head1), len(head2))-1:
                if len(head1) != len(head2):
                    return len(head1) - len(head2)
                else:
                    if int(number1)!=int(number2):
                        return int(number1)-int(number2)
                    else:
                        return 0

def solution(files):
    return sorted(files, key=functools.cmp_to_key(compare))

์ •๊ทœํ‘œํ˜„์‹regular expression์„ ์ด์šฉํ•œ ์ ์ ˆํ•œ ํŒŒ์‹ฑ๊ณผ ๊ฐ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ์ปค์Šคํ…€ ๋น„๊ต ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๋‹ค.

headParse์˜ ์ •๊ทœํ‘œํ˜„์‹์€ '^([a-zA-Z]|-|\s)+' ๋กœ ์•ŒํŒŒ๋ฒณ ํ˜น์€ '-' ํ˜น์€ ๋นˆ์นธ์ด ํ•˜๋‚˜ ์ด์ƒ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฐพ๊ณ , numberParser์˜ ์ •๊ทœํ‘œํ˜„์‹์€ '\d+'๋กœ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ฐพ๋Š” ์ •๊ทœํ‘œํ˜„์‹์ด๋‹ค.

headParser์™€ numberParser๋ฅผ ์ด์šฉํ•ด ๊ฐ ํŒŒ์ผ๋“ค์„ head, number, tail๋กœ ์ ์ ˆํžˆ ๋‚˜๋ˆˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๋‹จ๊ณ„๋ณ„๋กœ ๋น„๊ต์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•œ๋‹ค.

  1. head ๋น„๊ต
  2. number ๋น„๊ต

๊ทธ๋ฆฌ๊ณ  number๊นŒ์ง€ ๊ฐ™๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ˆœ์„œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋†”๋‘”๋‹ค.

sorted์— ์‚ฌ์šฉ๋˜๋Š” compare ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ธฐ์œ„ํ•ด ํŒŒ์ด์ฌ ๋‚ด์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ functools.cmp_to_key๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์ด ์ฝ”๋“œ์˜ ํ•ต์‹ฌ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•