# Sorted? yes? no? how? ..in Python

## The challenge

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.

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.

## The solution in Python code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```def is_sorted_and_how(arr):
asc = sorted(arr)
desc = sorted(arr, reverse=True)
if arr==asc:
return "yes, ascending"
elif arr==desc:
return "yes, descending"
else:
return "no"
```Code language: Python (python)```

Option 2:

``````def is_descending(arr):
for i in range(len(arr) - 1):
if arr[i + 1] > arr[i]: return False
return True

def is_ascending(arr):
for i in range(len(arr) - 1):
if arr[i + 1] < arr[i]: return False
return True

def is_sorted_and_how(arr):
if is_ascending(arr): return 'yes, ascending'
if is_descending(arr): return 'yes, descending'
return 'no'
```Code language: Python (python)```

Option 3:

``````is_sorted_and_how = lambda a: ['no','yes, ascending','yes, descending'][(sorted(a)==a)+(sorted(a)[::-1]==a)*2]
```Code language: Python (python)```

## Test cases to validate our solution

``````test.it("[1, 2]")
test.assert_equals(is_sorted_and_how([1, 2]), 'yes, ascending')

test.it("[15, 7, 3, -8]")
test.assert_equals(is_sorted_and_how([15, 7, 3, -8]), 'yes, descending')

test.it("[4, 2, 30]")
test.assert_equals(is_sorted_and_how([4, 2, 30]), 'no')
```Code language: Python (python)```
Tags:
Subscribe
Notify of