본문 바로가기
Computer Science/Algorithm

[백준] 2920번 음계, python

by 9루트 2023. 1. 6.

 

https://www.acmicpc.net/status?user_id=rndpqls999&problem_id=2920&from_mine=1 

 

채점 현황

 

www.acmicpc.net

 

와 list의 슬라이싱에서 잘못해서 계속 틀렸던 거였다.

 

 

# BOJ_2920

melodys = list(map(int, input().split()))

pre_melody = melodys[0]
answer = 'dontknow'
# for melody in melodys[1:]:
#     print(melody)
for melody in melodys[1:]:
    if melody > pre_melody:
        if answer == 'descending' or answer == 'mixed':
            answer = 'mixed'
            break
        else:
            answer = 'ascending'
    elif melody < pre_melody:
        if answer == 'ascending' or answer == 'mixed':
            answer = 'mixed'
            break
        else:
            answer = 'descending'
    else:
        answer = 'mixed'
        break
    pre_melody = melody

print(answer)

 

for melody in melodys[1:-1]:

라고 해서 틀린 건데, 헷갈렸던 부분을 짚어보자.

 

list = [1, 2, 3, 4, 5, 6, 7, 8] 일 때

list[1:-1] 와 list[1:] 의 차이

 

주의하자, -1은 뒤에서 두번째 수인 7이라는 것을.

 

 

 

sort를 이용한 간단한 풀이들이 있었다.

다음에는 노가다식으로 말고 도구를 사용해서 풀도록 해보자.

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