-
[PS] 단어 정렬하기Problem Solving 2020. 12. 2. 19:51
무작위로 입력된 소문자 영단어를 길이 순으로 정렬하되 같은 길이면 사전순으로 나열해야하는 문제이다.
단, 길이가 같으면 한 번만 출력해야 한다.
import time
import math
def mergesort(entries):
if isinstance(entries,list) and len(entries) > 1 :
pivot = math.floor(len(entries)/2)-1
first = mergesort(entries[:pivot+1])
second = mergesort(entries[pivot+1:])
return merge(first,second)
else:
return entries
def merge(first,second):
unit=[]
if not isinstance(first,list):
first = [first]
if not isinstance(second,list):
second = [second]
first_p = second_p = 0
while first_p < len(first) and second_p < len(second):
if len(first[first_p]) < len(second[second_p]):
unit.extend([first[first_p]])
first_p+=1
elif len(first[first_p]) == len(second[second_p]):
firstr = first[first_p]
secstr = second[second_p]
if firstr == secstr:
unit.extend([first[first_p]])
first_p+=1
second_p+=1
else:
for x in range(0,len(firstr)):
if ord(firstr[x]) < ord(secstr[x]):
unit.extend([first[first_p]])
first_p+=1
break
elif ord(firstr[x])>ord(secstr[x]) or x==(len(firstr)-1):
unit.extend([second[second_p]])
second_p +=1
break
else:
unit.extend([second[second_p]])
second_p+=1
if first_p >= len(first) and second_p < len(second):
unit.extend(second[second_p:])
else:
unit.extend(first[first_p:])
return unit
num = int(input())
entries=[]
for n in range(0,num):
entries.extend([input()])
entries = mergesort(entries)
for x in entries:
print(x)
길이에 따른 오름차순 정렬은 앞 문제에서 사용한 병합 정렬을 응용하였다.무작위로 입력된 소문자 영단어를 길이 순으로 정렬하되 같은 길이면 사전순으로 나열해야하는 문제이다.
단, 길이가 같으면 한 번만 출력해야 한다.
import time
import math
def mergesort(entries):
if isinstance(entries,list) and len(entries) > 1 :
pivot = math.floor(len(entries)/2)-1
first = mergesort(entries[:pivot+1])
second = mergesort(entries[pivot+1:])
return merge(first,second)
else:
return entries
def merge(first,second):
unit=[]
if not isinstance(first,list):
first = [first]
if not isinstance(second,list):
second = [second]
first_p = second_p = 0
while first_p < len(first) and second_p < len(second):
if len(first[first_p]) < len(second[second_p]):
unit.extend([first[first_p]])
first_p+=1
elif len(first[first_p]) == len(second[second_p]):
firstr = first[first_p]
secstr = second[second_p]
if firstr == secstr:
unit.extend([first[first_p]])
first_p+=1
second_p+=1
else:
for x in range(0,len(firstr)):
if ord(firstr[x]) < ord(secstr[x]):
unit.extend([first[first_p]])
first_p+=1
break
elif ord(firstr[x])>ord(secstr[x]) or x==(len(firstr)-1):
unit.extend([second[second_p]])
second_p +=1
break
else:
unit.extend([second[second_p]])
second_p+=1
if first_p >= len(first) and second_p < len(second):
unit.extend(second[second_p:])
else:
unit.extend(first[first_p:])
return unit
num = int(input())
entries=[]
for n in range(0,num):
entries.extend([input()])
entries = mergesort(entries)
for x in entries:
print(x)
길이에 따른 오름차순 정렬은 앞 문제에서 사용한 병합 정렬을 응용하였다.결과는 성공!
이번에도 pypy를 사용하였다.'Problem Solving' 카테고리의 다른 글
[PS]좌표 정렬하기 (0) 2020.12.10 [PS] 수 찾기 (0) 2020.12.05 [PS]수 정렬하기 3 (0) 2020.12.03 [PS] 백준 - 수 정렬하기 2 (0) 2020.11.19