Given an array of numbers, return a string made up of four parts:

a) a four character 'word', made up of the characters derived from the first two and last two numbers in the array. order should be as read left to right (first, second, second last, last),

b) the same as above, post sorting the array into ascending order,

c) the same as above, post sorting the array into descending order,

d) the same as above, post converting the array into ASCII characters and sorting alphabetically.

The four parts should form a single string, each part separated by a hyphen: '-'

example format of solution: 'asdf-tyui-ujng-wedg'

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Sorting

Data structures

Starter code: def sort_transform(arr):
	

Solutions:
def extract(arr):
	return ''.join(arr[:2] + arr[-2:])

def sort_transform(arr):
	arr = list(map(chr, arr))
	w1 = extract(arr)
	arr.sort()
	w2 = extract(arr)
	return f'{w1}-{w2}-{w2[::-1]}-{w2}'


def as_str(xs):
	return ''.join(map(chr, xs[:2] + xs[-2:]))

def sort_transform(arr):
	return '-'.join([as_str(arr), as_str(sorted(arr)), as_str(sorted(arr, reverse=True)), as_str(sorted(arr))])


def sort_transform(arr):
	a = chr(arr[0]) + chr(arr[1]) + chr(arr[-2]) + chr(arr[-1])
	arr.sort()
	b = chr(arr[0]) + chr(arr[1]) + chr(arr[-2]) + chr(arr[-1])
	return '-'.join([a, b, b[::-1], b])


def sort_transform(a):
	return '-'.join([''.join(map(chr, x[:2] + x[-2:])) for x in [a, sorted(a), sorted(a, reverse=1), sorted(a, key=chr)]])


def sort_transform(arr):
	arr = list(map(chr, arr))
	srtd = sorted(arr)
	a = ''.join(arr[:2] + arr[-2:])
	b = ''.join(srtd[:2] + srtd[-2:])
	return f'{a}-{b}-{b[::-1]}-{b}'


from heapq import nlargest, nsmallest

def sort_transform(lst):
	first_last = ''.join((chr(n) for n in lst[:2] + lst[-2:]))
	small_big = ''.join((chr(n) for n in nsmallest(2, lst) + nlargest(2, lst)[::-1]))
	return f'{first_last}-{small_big}-{small_big[::-1]}-{small_big}'


def sort_transform(arr):
	d = lambda x: ''.join(map(chr, [x[0], x[1], x[-2], x[-1]]))
	b = d(sorted(arr))
	return '-'.join((d(arr), b, d(sorted(arr, reverse=True)), b))


def sort_transform(a):
	a = [chr(e) for e in a]
	r = sorted(a)
	x = ''.join(r[:2] + r[-2:])
	return '-'.join([''.join(a[:2] + a[-2:]), x, x[::-1], x])


def sort_transform(arr):
	arr_al = sorted([chr(x) for x in arr])
	return '-'.join([chr(arr[0]) + chr(arr[1]) + chr(arr[-2]) + chr(arr[-1]), chr(sorted(arr)[0]) + chr(sorted(arr)[1]) + chr(sorted(arr)[-2]) + chr(sorted(arr)[-1]), chr(sorted(arr, reverse=True)[0]) + chr(sorted(arr, reverse=True)[1]) + chr(sorted(arr, reverse=True)[-2]) + chr(sorted(arr, reverse=True)[-1]), arr_al[0] + arr_al[1] + arr_al[-2] + arr_al[-1]])


def fst2_lst2(arr):
	return chr(arr[0]) + chr(arr[1]) + chr(arr[-2]) + chr(arr[-1])

def sort_transform(arr):
	asc_arr = sorted([chr(n) for n in arr])
	return fst2_lst2(arr) + '-' + fst2_lst2(sorted(arr)) + '-' + fst2_lst2(sorted(arr, reverse=True)) + '-' + asc_arr[0] + asc_arr[1] + asc_arr[-2] + asc_arr[-1]