PS/This
성적이 낮은 순서로 학생 출력하기
RUBY_루비
2020. 8. 4. 20:34
출처:: D기업 프로그래밍 콘테스트 예선
분류:: 정렬
1. 문제 이해 및 해결과정
- 점수, 이름으로 묶은 뒤에 점수를 기준으로 정렬을 수행해야한다.
2. 풀이방법
#성적이 낮은 순서로 학생 출력하기
#
import sys
sys.stdin = open("input.txt", 'rt', encoding='UTF8')
n=int(input())
print(n)
arr=[]
for i in range(n):
inputdata = input().split()
arr.append((inputdata[0],int(inputdata[1])))
#키를 이용하여 점수를 기준으로 정렬
arr = sorted(arr,key=lambda student: student[1])
for student in arr:
print(student[0],end=' ')
3. 오답원인
4. 알게된 점
UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6987: illegal multibyte sequence
: 다음과 같은 오류는 cp949 코덱으로 인코딩 된 파일을 읽어들일때 문제가 생긴다고 한다. 한글을 써서 오류가 생긴것 같은데 아래와 같이 인코딩 형식도 지정하여 해결할 수 있다.
sys.stdin = open("input.txt", 'rt', encoding='UTF8')
정렬 라이브러리에서 key를 활용한 소스코드
#정렬 라이브러리에서 key를 활용한 소스코드
#
import sys
sys.stdin = open("input.txt","r")
arr=[('마카롱',10),('피자',3),('치즈볼',8)]
def setting(data):
return data[1]
result = sorted(arr,key=setting)
print(result)