문제 제목 응계
문제 난이도 하
문제 유형: 배열, 구현
추천 풀이 시간: 15분
문제를 사이트에서 처음 풀어서 input으로 받아야 하는지 모르고 기존에 파이썬 코드에 받던 식으로(?) 작성했다
음계를 문자열에 맞춰 1,2,3,...,8로 변환까지 해줘야 하는 줄 알았다
arr = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'C']
arr2 = ['C', 'b', 'a', 'g', 'f', 'e', 'd', 'c']
arr3 = ['C', 'd', 'a', 'g', 'f', 'e', 'b', 'c']
def solution(arr):
result = list()
answer = ''
for i in range(len(arr)):
if(arr[i] == 'c'):
result.append(1)
elif(arr[i] == 'd'):
result.append(2)
elif(arr[i] == 'e'):
result.append(3)
elif(arr[i] == 'f'):
result.append(4)
elif(arr[i] == 'g'):
result.append(5)
elif(arr[i] == 'a'):
result.append(6)
elif(arr[i] == 'b'):
result.append(7)
elif(arr[i] == 'C'):
result.append(8)
else:
return None
if result == sorted(result):
answer = 'ascending'
elif result == sorted(result, reverse=True):
answer = 'descending'
else:
answer = 'mixed'
return answer
arr = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'C']
arr2 = ['C', 'b', 'a', 'g', 'f', 'e', 'd', 'c']
arr3 = ['C', 'd', 'a', 'g', 'f', 'e', 'b', 'c']
print(solution(arr))
print(solution(arr2))
print(solution(arr3))
입 출력 예시를 잘 보고 컴퓨터가 알아서 입력을 넣어볼 수 있도록 input 태그를 잘 활용해야 한다!
a[2] > a[1]
일 경우 오름차순이므로 descending을 False로 바꾼다 a[2] < a[1]
일 경우 내림차순이므로 ascending을 False로 바꾼다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 2 3 4 5 6 7 8 ascending
# >>> 8 7 6 5 4 3 2 1 descending
# >>> 1 3 4 5 6 7 8 2 mixed