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.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Sorting

Data structures

Starter code: def is_sorted_and_how(arr):
	

Solutions:
def is_sorted_and_how(arr):
	if arr == sorted(arr):
		return 'yes, ascending'
	elif arr == sorted(arr)[::-1]:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	if arr == sorted(arr):
		return 'yes, ascending'
	elif arr == sorted(arr, reverse=True):
		return 'yes, descending'
	return 'no'


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'


from itertools import islice
import operator

def is_sorted_and_how(arr):
	return 'yes, ascending' if is_sorted_with(arr, operator.le) else 'yes, descending' if is_sorted_with(arr, operator.ge) else 'no'

def is_sorted_with(arr, pred):
	return all((pred(x, y) for (x, y) in zip(arr, islice(arr, 1, None))))


def sign(x):
	return (x > 0) - (x < 0)

def is_sorted_and_how(arr):
	s = {sign(y - x) for (x, y) in zip(arr, arr[1:])}
	if len(s) == 1 or (len(s) == 2 and 0 in s):
		return 'yes, %s' % ['descending', 'ascending'][1 in s]
	return 'no'


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


def is_sorted_and_how(arr):
	(op, txt) = ((int.__le__, 'yes, ascending'), (int.__ge__, 'yes, descending'))[arr[0] > arr[-1]]
	return all(map(op, arr, arr[1:])) and txt or 'no'


def is_sorted_and_how(nums):
	a_or_d = {'a': 'ascending', 'd': 'descending'}
	diffs = {'d' if b - a < 0 else 'a' for (a, b) in zip(nums, nums[1:])}
	return 'yes, {}'.format(a_or_d[diffs.pop()]) if len(diffs) == 1 else 'no'


def is_sorted_and_how(arr):
	return next(('yes, {}cending'.format(k) for (k, v) in {'as': sorted(arr), 'des': sorted(arr, reverse=True)}.items() if v == arr), 'no')


from typing import List

def is_sorted_and_how(arr: List[int]) -> str:
	order = (a > b for (a, b) in zip(arr, arr[1:]) if a != b)
	if next(order):
		return 'yes, descending' if all(order) else 'no'
	return 'yes, ascending' if not any(order) else 'no'


def is_sorted_and_how(arr):
	if all((x > y for (x, y) in zip(arr, arr[1:]))):
		return 'yes, descending'
	elif all((x < y for (x, y) in zip(arr, arr[1:]))):
		return 'yes, ascending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	decend_Counter = 0
	accend_Counter = 0
	for i in range(len(arr) - 1):
		if arr[i] > arr[i + 1]:
			decend_Counter += 1
		if arr[i] < arr[i + 1]:
			accend_Counter += 1
	if decend_Counter == len(arr) - 1:
		return 'yes, descending'
	elif accend_Counter == len(arr) - 1:
		return 'yes, ascending'
	else:
		return 'no'
	return 1


def is_sorted_and_how(arr):
	arrS = sorted(arr)
	revArrS = sorted(arr, reverse=True)
	if arr == arrS:
		return 'yes, ascending'
	elif arr == revArrS:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	a = sorted(arr)
	d = sorted(arr, reverse=True)
	return 'yes, ascending' if arr == a else 'yes, descending' if arr == d else 'no'


def is_sorted_and_how(a):
	if a == sorted(a):
		return 'yes, ascending'
	elif a == sorted(a, reverse=True):
		return 'yes, descending'
	else:
		return 'no'


from operator import le, ge

def is_sorted_and_how(arr):
	if all(map(le, arr, arr[1:])):
		return 'yes, ascending'
	elif all(map(ge, arr, arr[1:])):
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	sorted_asc = sorted(arr)
	sorted_desc = sorted_asc[::-1]
	return 'yes, ascending' if sorted_asc == arr else 'yes, descending' if sorted_desc == arr else 'no'


def is_sorted_and_how(arr):
	return all((arr[i] < arr[i + 1] for i in range(len(arr) - 1))) and 'yes, ascending' or (all((arr[i] > arr[i + 1] for i in range(len(arr) - 1))) and 'yes, descending') or 'no'


def is_sorted_and_how(arr):
	return {('LT',): 'yes, ascending', ('GT',): 'yes, descending'}.get(tuple(set(map(lambda x, y: 'LT' if x <= y else 'GT', arr, arr[1:]))), 'no')


def is_sorted_and_how(arr):
	counts = 0
	for index in range(len(arr) - 1):
		if arr[index] > arr[index + 1]:
			counts += 1
		elif arr[index] < arr[index + 1]:
			counts -= 1
	if counts == len(arr) - 1:
		return 'yes, descending'
	elif counts == -len(arr) + 1:
		return 'yes, ascending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	return ('yes, ascending' if sorted(arr) == arr else 'yes, descending') if sorted(arr) == arr or sorted(arr, reverse=True) == arr else 'no'


def is_sorted_and_how(arr):
	check = [arr[i - 1] < arr[i] for i in range(1, len(arr))]
	true = check.count(True)
	false = check.count(False)
	if true == len(arr) - 1 and false == 0:
		return 'yes, ascending'
	elif false == len(arr) - 1 and true == 0:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	s = 0
	t = 0
	for i in range(0, len(arr) - 1):
		if arr[i] < arr[i + 1]:
			s += 1
		elif arr[i] > arr[i + 1]:
			t += 1
	if t == len(arr) - 1:
		return 'yes, descending'
	elif s == len(arr) - 1:
		return 'yes, ascending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	for i in range(len(arr)):
		if sorted(arr) == arr:
			return 'yes, ascending'
		elif sorted(arr, reverse=True) == arr:
			return 'yes, descending'
		else:
			return 'no'


def is_sorted_and_how(arr):
	monity = arr[1] <= arr[0]
	for index in range(len(arr))[2:]:
		if (arr[index] <= arr[index - 1]) != monity:
			return 'no'
	return 'yes, descending' if monity else 'yes, ascending'


def is_sorted_and_how(arr):
	if arr == list(sorted(arr)) or arr == list(sorted(arr))[::-1]:
		return 'yes, ascending' if arr[0] < arr[1] else 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	if arr[0] < arr[1]:
		for i in range(len(arr) - 1):
			if arr[i] > arr[i + 1]:
				return 'no'
		return 'yes, ascending'
	if arr[0] > arr[1]:
		for i in range(len(arr) - 1):
			if arr[i] < arr[i + 1]:
				return 'no'
		return 'yes, descending'


def is_sorted_and_how(arr):
	asc = False
	desc = False
	for index in range(len(arr)):
		if index == 0:
			if arr[index + 1] > arr[index]:
				asc = True
				desc = False
			if arr[index + 1] < arr[index]:
				asc = False
				desc = True
		if index != 0 and index != len(arr) - 1:
			if arr[index - 1] < arr[index] < arr[index + 1] and asc:
				asc = True
				desc = False
			elif arr[index - 1] > arr[index] > arr[index + 1] and desc:
				asc = False
				desc = True
			else:
				asc = False
				desc = False
		if index == len(arr) - 1:
			if arr[index - 1] < arr[index] and asc:
				asc = True
				desc = False
			if arr[index - 1] < arr[index] and desc:
				asc = False
				desc = True
	if asc:
		return 'yes, ascending'
	if desc:
		return 'yes, descending'
	if not asc and (not desc):
		return 'no'


def is_sorted_and_how(arr):
	asc = sorted(arr)
	des = sorted(arr, reverse=True)
	if arr == asc:
		return 'yes, ascending'
	elif arr == des:
		return 'yes, descending'
	elif arr != (asc or des):
		return 'no'


def is_sorted_and_how(arr):
	delta_value = []
	positive_num = 0
	negtive_num = 0
	for i in range(len(arr) - 1):
		delta_value.append(arr[i + 1] - arr[i])
	for i in range(len(delta_value)):
		if delta_value[i] >= 0:
			positive_num += 1
		else:
			negtive_num += 1
	if positive_num == len(delta_value):
		return 'yes, ascending'
	elif negtive_num == len(delta_value):
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	sort = 'descending' if arr[0] > arr[-1] else 'ascending'
	return 'yes, {}'.format(sort) if sort == 'ascending' and sorted(arr) == arr or (sort == 'descending' and sorted(arr, reverse=True) == arr) else 'no'


def is_sorted_and_how(arr):
	if arr == sorted(arr):
		return 'yes, ascending'
	elif arr == sorted(arr, key=None, reverse=True):
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	list_a = []
	list_d = []
	list_a = sorted(arr)
	for i in list_a[::-1]:
		list_d.append(i)
	if arr == list_a:
		return 'yes, ascending'
	if arr == list_d:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	type_arr = set()
	prev = arr[0]
	for item in arr[1:]:
		if item > prev:
			type_arr.add('asc')
		elif item < prev:
			type_arr.add('desc')
		prev = item
	if len(type_arr) != 1:
		return 'no'
	result = type_arr.pop()
	return 'yes, ascending' if result == 'asc' else 'yes, descending'


def is_sorted_and_how(arr):
	sort_arr = sorted(arr)
	sort_desc = sorted(arr, reverse=True)
	if sort_arr == arr:
		return 'yes, ascending'
	if sort_desc == arr:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	c = 0
	a = 0
	for i in range(len(arr) - 1):
		if arr[i] <= arr[i + 1]:
			c += 1
			if c == len(arr) - 1:
				return 'yes, ascending'
		elif arr[i] >= arr[i + 1]:
			a += 1
			if a == len(arr) - 1:
				return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	if arr[0] < arr[1]:
		return 'yes, ascending'
	elif arr[0] > arr[1] and arr[0] > arr[2]:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	asc = True
	desc = True
	for i in range(1, len(arr)):
		if arr[i - 1] == arr[i]:
			return 'no'
		if arr[i - 1] < arr[i]:
			desc = False
		else:
			asc = False
		if not asc and (not desc):
			return 'no'
	return 'yes, ascending' if asc else 'yes, descending'


def is_sorted_and_how(arr):
	isAllNeg = True
	isAllPos = True
	for i in range(len(arr) - 1):
		isAllPos = isAllPos and arr[i + 1] - arr[i] > 0
		isAllNeg = isAllNeg and arr[i + 1] - arr[i] < 0
	if isAllPos:
		return 'yes, ascending'
	elif isAllNeg:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	(k, c) = (0, 0)
	for i in range(1, len(arr)):
		if arr[i] > arr[i - 1]:
			k += 1
		else:
			c += 1
	if k > 0 or c > 0:
		if k > 0 and c == 0:
			return 'yes, ascending'
		elif c > 0 and k == 0:
			return 'yes, descending'
	return 'no'


def is_sorted_and_how(list):
	if list == sorted(list):
		return 'yes, ascending'
	if list == sorted(list, reverse=True):
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	order = 0
	i = 0
	for num in arr:
		hold = arr[i]
		if i + 1 < len(arr):
			if hold <= arr[i + 1]:
				order += 1
			if hold >= arr[i + 1]:
				order -= 1
		i += 1
	if order == len(arr) - 1:
		return 'yes, ascending'
	if order * -1 == len(arr) - 1:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	print(arr)
	d = True
	a = True
	for i in range(len(arr) - 1):
		if arr[i] < arr[i + 1]:
			d = False
	for i in range(len(arr) - 1):
		if arr[i] > arr[i + 1]:
			a = False
	if d:
		return 'yes, descending'
	elif a:
		return 'yes, ascending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	if arr[0] == min(arr) and arr[-1] == max(arr):
		return 'yes, ascending'
	elif arr[0] == max(arr) and arr[-1] == min(arr):
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(A):
	S = sorted(A)
	if S == A:
		return 'yes, ascending'
	elif S == A[::-1]:
		return 'yes, descending'
	return 'no'


def is_sorted_and_how(arr):
	if all((arr[i] >= arr[i - 1] for i in range(1, len(arr)))):
		return 'yes, ascending'
	elif all((arr[i] <= arr[i - 1] for i in range(1, len(arr)))):
		return 'yes, descending'
	return 'no'


def is_sorted_and_how(arr):
	x = list(arr)
	x.sort()
	if arr == x:
		return 'yes, ascending'
	elif arr[::-1] == x:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	lst = arr[:]
	lst.sort()
	if lst == arr:
		return 'yes, ascending'
	elif lst[::-1] == arr:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	if arr[0] > arr[1]:
		try:
			if arr[1] < arr[2]:
				return 'no'
		except:
			pass
		return 'yes, descending'
	elif arr[0] < arr[1]:
		try:
			if arr[1] > arr[2]:
				return 'no'
		except:
			pass
		return 'yes, ascending'
	return 'no'


def is_sorted_and_how(arr):
	if arr == sorted(arr):
		return 'yes, ascending'
	for i in range(len(arr) - 1):
		for j in range(1):
			if arr[i] > arr[i + 1]:
				continue
			else:
				return 'no'
	return 'yes, descending'


def is_sorted_and_how(arr):
	new_sorted_list = sorted(arr)
	new_sorted_list_d = sorted(arr, reverse=True)
	if arr == new_sorted_list:
		return 'yes, ascending'
	elif arr == new_sorted_list_d:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	return 'yes, %sscending' % ['a', 'de'][max(sorted(arr)) == arr[0]] if sorted(arr)[0] in [arr[0], arr[-1]] else 'no'


def is_sorted_and_how(arr):
	sort_arr = sorted(arr)
	des_arr = sorted(arr)[::-1]
	if arr == sort_arr:
		return 'yes, ascending'
	if arr == des_arr:
		return 'yes, descending'
	return 'no'


def is_sorted_and_how(arr):
	arrasc = arr[:]
	arrasc.sort()
	arrdsc = arr[:]
	arrdsc.sort(reverse=True)
	if arrasc == arr:
		return 'yes, ascending'
	elif arrdsc == arr:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	sort = 'yes, ascending'
	sort2 = 'yes, descending'
	if arr[0] > arr[len(arr) - 1]:
		for i in range(len(arr) - 1):
			if arr[i] < arr[i + 1]:
				sort = 'no'
	if arr[0] < arr[len(arr) - 1]:
		for i in range(len(arr) - 1):
			if arr[i] > arr[i + 1]:
				sort = 'no'
	if sort != 'no':
		if arr[0] > arr[len(arr) - 1]:
			return sort2
		elif arr[0] < arr[len(arr) - 1]:
			return sort
	else:
		return sort


def is_sorted_and_how(arr):
	if arr[::-1] == sorted(arr):
		return 'yes, descending'
	if arr == sorted(arr):
		return 'yes, ascending'
	if arr != sorted(arr):
		return 'no'


def is_sorted_and_how(arr):
	brr = arr.copy()
	crr = arr.copy()
	brr.sort()
	crr.sort(reverse=True)
	if arr == brr:
		return 'yes, ascending'
	if arr == crr:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	h = arr.copy()
	h.sort(reverse=True)
	p = arr.copy()
	p.sort()
	if arr == p:
		return 'yes, ascending'
	if arr == h:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	a = sorted(arr)
	b = sorted(arr, reverse=True)
	if arr[1] == a[1]:
		return 'yes, ascending'
	elif arr[1] == b[1]:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	k = 0
	i = 0
	while i != len(arr) - 1:
		if arr[i] < arr[i + 1]:
			k += 1
		elif arr[i] > arr[i + 1]:
			k -= 1
		i += 1
	if k == len(arr) - 1:
		return 'yes, ascending'
	elif k == -len(arr) + 1:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	b = 0
	for x in range(0, len(arr) - 1):
		if arr[x] < arr[x + 1]:
			b += 1
			if b == len(arr) - 1:
				return 'yes, ascending'
	b = 0
	for x in range(0, len(arr) - 1):
		if arr[x] > arr[x + 1]:
			b += 1
			if b == len(arr) - 1:
				return 'yes, descending'
		else:
			return 'no'


def is_sorted_and_how(arr):
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			print(str(arr[i]) + ' ' + str(arr[i + 1]))
	as_start = 0
	de_start = 0

	def compare(first, second):
		if second > first:
			return 'as'
		elif first > second:
			return 'de'
		else:
			return 'eq'
	state = compare(arr[0], arr[1])
	for i in range(1, len(arr) - 1):
		new_state = compare(arr[i], arr[i + 1])
		print(f'{arr[i]}, {arr[i + 1]}, {new_state}, {state}')
		if new_state != 'eq':
			if state == 'eq':
				state = new_state
			if new_state != state:
				state = 'unsorted'
				break
	if state == 'unsorted' or state == 'eq':
		return 'no'
	if state == 'as':
		return 'yes, ascending'
	if state == 'de':
		return 'yes, descending'


def is_sorted_and_how(arr):
	arr_up = arr[0]
	arr_down = arr[0]
	check = 1
	for x in range(len(arr)):
		if arr[x] > arr_up:
			arr_up = arr[x]
			check += 1
	if check == len(arr):
		return 'yes, ascending'
	check = 1
	for x in range(len(arr)):
		if arr[x] < arr_down:
			arr_down = arr[x]
			check += 1
	if check == len(arr):
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(arr):
	if arr[-1] > arr[0]:
		last = arr[0]
		for n in arr[1:]:
			if n < last:
				return 'no'
			last = n
		return 'yes, ascending'
	else:
		last = arr[0]
		for n in arr[1:]:
			if n > last:
				return 'no'
			last = n
		return 'yes, descending'


def is_sorted_and_how(list):
	ascendList = list[:]
	ascendList.sort()
	descendList = list[:]
	descendList.sort(reverse=True)
	return 'yes, ascending' if ascendList == list else 'yes, descending' if descendList == list else 'no'


def is_sorted_and_how(arr):
	my_list = list(arr)
	if sorted(my_list) == arr:
		return 'yes, ascending'
	elif sorted(my_list, reverse=True) == arr:
		return 'yes, descending'
	else:
		return 'no'


def is_sorted_and_how(BLM):
	if BLM == sorted(BLM):
		return 'yes, ascending'
	if BLM == sorted(BLM)[::-1]:
		return 'yes, descending'
	return 'no'


def is_sorted_and_how(arr):
	s = 'no'
	for i in range(0, len(arr) - 1):
		if arr[i] < arr[i + 1]:
			s = 'yes'
			z = 'ascending'
		else:
			s = 'no'
			z = ''
			break
	if s == 'no':
		for i in range(len(arr) - 1):
			if arr[i] > arr[i + 1]:
				s = 'yes'
				z = 'descending'
			else:
				s = 'no'
				z = ''
				break
	if s == 'no':
		return s
	else:
		return s + ', ' + z