1. ๋ฌธ์ ์ค๋ช
- ์ ๋ ฅ : ํ์ผ์ด๋ฆ๋ค
- ์ถ๋ ฅ : ๋ฌธ์๋ถ๋ถ, ์ซ์๋ถ๋ถ, ๋จ์ ๋ถ๋ถ์ผ๋ก ๋๋์ด์ ์ ๋ ฌ๊ธฐ์ค์ ๋ง๊ฒ ์ ๋ ฌ๋ ํ์ผ๋ช ์ถ๋ ฅ
2. ์ฝ๋
solution1.py
from functools import cmp_to_key
def compare(s1,s2):
head1, number1, tail1 = s1[0], s1[1], s1[2]
head2, number2, tail2 = s2[0], s2[1], s2[2]
head1, head2 = head1.upper(), head2.upper() #์๋ฐ๋ฒณ ๊ตฌ๋ถ ์์ผ๋๊น
number1, number2 = int(number1), int(number2) #๋น๊ต๋ฅผ ์ํด intํ
if head1 != head2:
return -1 if head1 < head2 else 1 #๋ง์ฝ head1์ด head2๋ณด๋ค ์์ผ๋ฉด ๊ตํ(-1)
else: #head1 == head2
return number1 - number2 if number1 != number2 else 1 #์ซ์๊ฐ ์๋ก ๊ฐ์ง ์์ ๋๋ง ๋นผ๊ธฐ(๋์ด ๊ฐ์ ๋(๋์ ์๋ก ๋นผ๋ฉด 0์ด๋๊น) ๊ตํ์ด ๋์ด๋ฒ๋ฆผ์ ๋ฐฉ์ง)
def split_fname(filename):
HEAD = ""
NUMBER = ""
TAIL = ""
i = 0
while not filename[i].isnumeric():
i+=1
HEAD = filename[:i]
j = i
while filename[j].isnumeric():
j+=1
if j == len(filename): #TAIL์ด ์๋ ๊ฒฝ์ฐ
NUMBER = filename[i:j]
return [HEAD, NUMBER, '']
NUMBER = filename[i:j]
TAIL = filename[j:]
return [HEAD, NUMBER,TAIL]
def solution(files):
answer = []
filenames = []
for file in files: #ํ์ผ๋ช
ํ๋์ฉ ์กฐํ
filenames.append(split_fname(file))
filenames.sort(key=cmp_to_key(compare))
for i, filename in enumerate(filenames):
filenames[i] = ''.join(filename)
return filenames
- from functools import cmp_to_key๋ก ์ ์ธ
- functools.cmp_to_key(func) ํจ์๋ sorted์ ๊ฐ์ ์ ๋ ฌ ํจ์์ key ๋งค๊ฐ๋ณ์์ ํจ์(func)๋ฅผ ์ ๋ฌํ ๋ ์ฌ์ฉํ๋ ํจ์์ด๋ค.
- ๋จ, func ํจ์๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ์๋ค์ด๊ณ , ์ฒซ๋ฒ์งธ ์ธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ค์ ๋น๊ตํ์ฌ, ์์ผ๋ฉด ์์, ๊ฐ์ผ๋ฉด 0, ํฌ๋ฉด ์์๋ฅผ ๋ฐํํ๋ ๋น๊ต ํจ์์ด์ด์ผ ํ๋ค.
- ์ ๋ ฌ์ ํ ๋, cmp_to_key๋ฅผ ํ์ฉํด์ผํ๋ค.
- from functools import cmp_to_key๋ก ์ ์ธ
- cmp_to_key์์๋ compare ํจ์๋ฅผ ๋ฃ๋๋ค.
- compare ํจ์๋ ๋น๊ตํด์ ์ ์์๊ฐ ๋ ์์ผ๋ฉด -1์ ๋ฐํํด์ ์๋ก ๊ตํ(์์น๋ฅผ ๋ฐ๊ฟ)ํ๊ฒํ๊ณ ,์๋ก ๊ฐ์ผ๋ฉด 0์ ๋ฐํ(๊ตํ), ์ ์์๊ฐ ๋ ํฌ๋ฉด 1์ ๋ฐํ(๊ตํํ์ง์์)ํ๋ค.
- ์ฃผ์ 1) tail์ด ์๋ ๊ฒฝ์ฐ
- tail์ด ์๋ ๊ฒฝ์ฐ๋ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์ด์ผํ๋ค. ์ํ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋ ์ ์์
- ์ฃผ์ 2) ํ์ผ๋ช
์์๋ณต๊ท
- ''.join(filename) ๋ก ๋๋์ด๋์ ํ์ผ๋ช ๋ฆฌ์คํธ๋ฅผ stringํํ๋ก ๋ฐ๊ฟ์ฃผ์
solution2.py
def solution(files):
tmp = []
head, number, tail = '', '', ''
for file in files:
for i in range(len(file)):
if file[i].isdigit(): # ์ซ์๊ฐ ๋์ค๋ฉด ๊ทธ ์ด์ ์ ๋ฌด์กฐ๊ฑด HEAD, ์ดํ๋ NUMBER, TAIL๋ก ๋ค์ ๊ตฌ๋ถ
head = file[:i]
number = file[i:]
for j in range(len(number)): # NUMBER์ TAIL ๊ตฌ๋ถ (์ซ์ ์๋์ค๋ฉด TAIL)
if not number[j].isdigit():
tail = number[j:]
number = number[:j]
break
tmp.append([head, number, tail])
head, number, tail = '', '', ''
break
tmp = sorted(tmp, key=lambda x:(x[0].lower(), int(x[1])))
return [''.join(i) for i in tmp]
- for๋ฌธ๊ณผ break๋ฌธ ์ฌ์ฉ์ ํตํด ์ฝ๊ฒ ํผ ํ์ด์ด๋ค.
- ํจ์ฌ ๊ฐ๊ฒฐํ๋ค.
solution3.py
import re
import re
def solution(files):
temp = [re.split(r"([0-9]+)", file) for file in files]
sort = sorted(temp, key = lambda x: (x[0].lower(), int(x[1])))
return [''.join(s) for s in sort]
- ์ ๊ทํํ์์ ์ฌ์ฉํ์ฌ file์ ์ซ์๋ฅผ ๊ธฐ์ค์ผ๋ก split
- r'(\d+)' ๋ ์ฌ์ฉ๊ฐ๋ฅ
4. ์ฐธ๊ณ
https://www.youtube.com/watch?v=IcNPpsG8i2s
https://wikidocs.net/109303
https://velog.io/@sem/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-LEVEL2-%ED%8C%8C%EC%9D%BC%EB%AA%85-%EC%A0%95%EB%A0%AC-Python
'Algorithm > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (Python) (0) | 2022.02.13 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์คํ์ฑํ ๋ฐฉ (Python) (0) | 2022.02.12 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฐฉ๊ธ ๊ทธ๊ณก (Python) (0) | 2022.02.09 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ (Python) (0) | 2022.02.09 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์น ์ต๋ํ (Python) (0) | 2022.02.03 |