2920. 음계

이윤설·2023년 3월 9일
0

https://www.acmicpc.net/problem/2920

내가 쓴 코드

list = list(map(int, input().split()))
# print(list)
if list[0] - list[1] == -1:
    print("ascending")
elif list[0] - list[1] == 1:
    print("descending")
else:
    print("mixed")

요소를 뺐을 때가 아니라 더 보편적으로 쓸 수 있도록 코드를 수정하자.
틀린 코드는 아니지만 좀 더 나은 답안이 있다.

정답

a = list(map(int, input().split()))
 
if a == sorted(a):
    print('ascending')
elif a == sorted(a, reverse=True):
    print('descending')
else:
    print('mixed')

오답노트

sorted()함수는 오름차순 또는 내림차순으로 정렬된 값을 반환한다.(리스트의 원본 값은 그대로 유지된다.)

오름차순의 경우:
sorted(리스트 명)

내림차순의 경우:
sorted(리스트 명, reverse = True)

역순으로 입력한 값은 내림차순으로 정렬해도 입력한 값과 똑같이 정렬된다.

profile
화려한 외면이 아닌 단단한 내면

0개의 댓글