반응형
Complete the method which accepts an array of integers, and returns one of the following:
"yes, ascending"
- if the numbers in the array are sorted in an ascending order
"yes, descending"
- if the numbers in the array are sorted in a descending order
"no"
- otherwise
You can assume the array will always be valid, and there will always be one correct answer.
Solution:
def is_sorted_and_how(arr):
if sorted(arr) == arr:
return "yes, ascending"
elif sorted(arr, reverse=True) == arr:
return "yes, descending"
return "no"
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0910. Quarter of the year (0) | 2022.09.10 |
---|---|
0909. Will there be enough space? (0) | 2022.09.09 |
0907. Count by X (0) | 2022.09.07 |
0906. The Coupon Code (0) | 2022.09.06 |
0905. Training JS #7: if..else and ternary operator (0) | 2022.09.05 |