Given a string of words (x), you need to return an array of the words, sorted alphabetically by the final character in each.

If two words have the same last letter, they returned array should show them in the order they appeared in the given string.

All inputs will be valid.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Sorting

Data structures

Starter code: def last(s):
	

Solutions:
def last(s):
	return sorted(s.split(), key=lambda x: x[-1])


def last(s):
	return sorted(s.split(), key=lambda word: word[-1])


from operator import itemgetter

def last(s):
	return sorted(s.split(), key=itemgetter(-1))


def last(s):
	return sorted(s.split(' '), key=lambda w: w[-1])


last = lambda x: sorted(x.split(), key=lambda i: i[-1])


last = lambda x: sorted(x.split(), key=lambda e: e[-1])


def last(x):
	a = x.split()
	last_char = []
	i = 0
	for word in a:
		last_char.append((word[-1], i, word))
		i = i + 1
	last_char = sorted(last_char)
	output = [word for (char, i, word) in last_char]
	return output


def last(x):
	from collections import defaultdict
	d = defaultdict(list)
	res = []
	for w in x.split():
		d[w[-1]] += [w]
	for i in range(97, 123):
		res += d[chr(i)]
	return res


def last(x):
	return sorted(x.split(' '), key=lambda word: word[-1])