본문 바로가기
Computer Engineering/백준

[백준/파이썬3/2920] 음계

by UC우공 2019. 12. 24.

 

풀이

notes = list(map(int, input().split()))
des = True
asc = True
for i in range(len(notes)-1):
    if notes[i]< notes[i+1]:
        des=False
    if notes[i]> notes[i+1]:
        asc=False

if des == False and asc == False:
    print("mixed")
elif des == True:
    print("descending")
elif asc == True:
    print("ascending")

1. 배열로 입력받고 notes 라는 리스트에 데이터를 저장

2. for문을 사용하여 배열의 길이에서 1개 뺀것 만큼 모든 배열 탐색

3. if문을 사용하여 이전보다 작거나 큰거를 판단하여 틀릴경우 Boolean 타입으로 저장

4. Boolean타입(참/거짓) 기준으로 조건문을 사용하여 결과 출력.

 

추가풀이

notes = list(map(int, input().split()))
 
if notes == sorted(notes):			#이경우는 오름차순으로 sort (1,2,3~)
    print("ascending")
elif notes == sorted(notes, reverse=True):      #이경우는 내림차순으로 sort (10,9,8~)
    print("descending")
else:
    print("mixed")

sorted() 라는 기본 패키지 함수인데, 리스트를 오름차순으로 정렬하고 리스트를 리턴한다.
내림차순으로 정렬하고 싶으면 "sorted(list,reverse=True)" 사용하면 된다.

댓글