본문 바로가기
Python

[백준] 2920번: 음계

by Leunco 2021. 12. 6.

 

a = list(map(int, input().split(' ')))

ascending = True
descending = True

for i in range(1,8):
    if a[i] > a[i-1]:
        descending = False
    elif a[i] < a[i-1]:
        ascending = False

if ascending:
    print('ascending')
elif descending:
    print('descending')
else:
    print('mixed')

 

1. 다시 생각해봐야 할 문제

a = list(map(int, input().split(' ')))
  • input()을 통해 그대로 받아들이게 되면 string 형태가 되기 때문에, 숫자를 하나씩 비교하기 까다로워진다.
  • 그래서 우선 split(' ')으로 공백 기준으로 input을 받아들여 배열에 넣는다.
    ex. 35...9 → ['3', '5', ... '9']
  • map()을 이용해 배열의 원소를 int형으로 바꿔준다.
  • 또한 전체를 list()로 감싸줘야 숫자 하나씩 비교할 수 있다.

 

** map()

  • 리스트의 요소를 지정된 함수로 처리해주는 함수
  • 원본 리스트를 변경하지 않고 새 리스트를 생성
  • 출처 : https://dojang.io/mod/page/view.php?id=2286

 

 

출처 : 패스트캠퍼스 - 알고리즘 / 기술면접 완전 정복 올인원 패키지 Online

반응형

댓글