Write a function that takes a single array as an argument (containing multiple strings and/or positive numbers and/or arrays), and returns one of four possible string values, depending on the ordering of the lengths of the elements in the input array:

Your function should return...

- “Increasing” - if the lengths of the elements increase from left to right (although it is possible that some neighbouring elements may also be equal in length)
- “Decreasing” - if the lengths of the elements decrease from left to right (although it is possible that some neighbouring elements may also be equal in length)
- “Unsorted” - if the lengths of the elements fluctuate from left to right
- “Constant” - if all element's lengths are the same.



Numbers and Strings should be evaluated based on the number of characters or digits used to write them.

Arrays should be evaluated based on the number of elements counted directly in the parent array (but not the number of elements contained in any sub-arrays).

Happy coding! :)

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Starter code: def order_type(arr):
	

Solutions:
def order_type(arr):
	if not arr:
		return 'Constant'
	arr = list(map(len, [str(elt) if type(elt) == int else elt for elt in arr]))
	cmp = sorted(arr)
	if arr == [arr[0]] * len(arr):
		s = 'Constant'
	elif arr == cmp:
		s = 'Increasing'
	elif arr == cmp[::-1]:
		s = 'Decreasing'
	else:
		s = 'Unsorted'
	return s


def order_type(arr):
	xs = [len(x) if hasattr(x, '__len__') else len(str(x)) for x in arr]
	ys = sorted(xs)
	return 'Constant' if not xs or ys[0] == ys[-1] else 'Increasing' if xs == ys else 'Decreasing' if xs[::-1] == ys else 'Unsorted'


def order_type(arr):
	lengths = [len(e if isinstance(e, list) else str(e)) for e in arr]
	if len(set(lengths)) < 2:
		return 'Constant'
	inc = sorted(lengths)
	return 'Increasing' if inc == lengths else 'Decreasing' if inc[::-1] == lengths else 'Unsorted'


from operator import le, ge
size = lambda x: len(str(x)) if type(x) == int else len(x)

def order_type(arr):
	L = list(map(size, arr))
	if len(set(L)) <= 1:
		return 'Constant'
	if all(map(le, L, L[1:])):
		return 'Increasing'
	if all(map(ge, L, L[1:])):
		return 'Decreasing'
	return 'Unsorted'


from itertools import tee

def pairwise(iterable):
	(a, b) = tee(iterable)
	next(b, None)
	return zip(a, b)

def length(value):
	try:
		return len(value)
	except TypeError:
		return len(str(value))

def order_type(values):
	if len(values) <= 1:
		return 'Constant'
	deltas = [length(b) - length(a) for (a, b) in pairwise(values)]
	min_delta = min(deltas)
	max_delta = max(deltas)
	if min_delta == max_delta == 0:
		return 'Constant'
	if min_delta >= 0 and max_delta >= 0:
		return 'Increasing'
	if min_delta <= 0 and max_delta <= 0:
		return 'Decreasing'
	return 'Unsorted'


def order_type(arr):
	len_stats = [len(str(x)) if type(x) == int else len(x) for x in arr]
	sorted_stats = sorted(len_stats)
	if not arr or sorted_stats[0] == sorted_stats[-1]:
		return 'Constant'
	if len_stats == sorted_stats:
		return 'Increasing'
	return 'Decreasing' if len_stats == sorted_stats[::-1] else 'Unsorted'


def order_type(arr):
	l = [len(str(e)) if type(e) != list else len(e) for e in arr]
	return 'Constant' if len(set(l)) <= 1 else 'Increasing' if l == sorted(l) else 'Decreasing' if l == sorted(l)[::-1] else 'Unsorted'


def order_type(arr):
	l = [len(str(i)) if type(i) == int else len(i) for i in arr]
	return [['Decreasing', 'Unsorted'][l != sorted(l)[::-1]], ['Increasing', 'Constant'][len(set(l)) < 2]][sorted(l) == l]


from collections import OrderedDict

def order_type(arr):
	res = OrderedDict([('Constant', True), ('Increasing', True), ('Decreasing', True), ('Unsorted', True)])

	def get_length(element):
		try:
			return len(element)
		except:
			return len(str(element))
	for (first, second) in zip(arr, arr[1:]):
		length_of_first = get_length(first)
		length_of_second = get_length(second)
		if length_of_first > length_of_second:
			res['Increasing'] = False
			res['Constant'] = False
		if length_of_first < length_of_second:
			res['Decreasing'] = False
			res['Constant'] = False
	for k in res:
		if res[k]:
			return k