Python/알고리즘 - 백준
[백준] 2920 : 음계 (파이썬)
Jong_seoung
2022. 10. 8. 11:10
반응형
문제 링크 : https://www.acmicpc.net/problem/2920
문제 요약
입력받은 값이 1부터 8까지 차례대로면 ascending, 8부터 1까지 차례대로면 descending, 둘 다 아니라면 mixed를 출력하는 프로그램을 만드는 문제이다.
TRY CODE
a = list(map(int,input().split()))
b = [1,2,3,4,5,6,7,8]
if a == b :
print("ascending")
elif a == reversed(b):
print("descending")
else:
print("mixed")
문제점
- 숫자를 거꾸로 입력하면 descending가 출력되는 것이 아니라 mixed가 출력되었다
해결법
- reversed(b)를 출력해 보았더니 <list_reverseiterator object at 0x000001ED0C106340>라는 주소 값이 나와서 list(reversed(b))로 바꿔주었다.
CODE
a = list(map(int,input().split()))
b = [1,2,3,4,5,6,7,8]
if a == b :
print("ascending")
elif a == list(reversed(b)):
print("descending")
else:
print("mixed")
풀이
1. a에 int형 list로 입력 받았다. / b에 리스트형으로 1,2,3,4,5,6,7,8을 넣어주었다.
a = list(map(int,input().split()))
b = [1,2,3,4,5,6,7,8]
2. a와 b가 같으면 ascedding를 출력
a와 reversed(b)가 같으면 descending 출력
그 외에는 mixed 출력
if a == b :
print("ascending")
elif a == list(reversed(b)):
print("descending")
else:
print("mixed")
반응형