You are given a string of numbers between 0-9. Find the average of these numbers and return it as a floored whole number (ie: no decimal places) written out as a string. Eg:

"zero nine five two" -> "four"

If the string is empty or includes a number greater than 9, return "n/a"

Source: codewars | Difficulty: EASY
Tags: String algorithms

Starter code: def average_string(s):
	

Solutions:
N = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

def average_string(s):
	try:
		return N[sum((N.index(w) for w in s.split())) // len(s.split())]
	except (ZeroDivisionError, ValueError):
		return 'n/a'


def average_string(s):
	if not s:
		return 'n/a'
	numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
	total = 0
	counter = 0
	for n in s.split():
		try:
			value = numbers.index(n)
			total += value
			counter += 1
		except:
			return 'n/a'
	return numbers[total // counter]


from statistics import mean
lst = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
d = {lst.index(i): i for i in lst}

def average_string(s):
	try:
		return d.get(int(mean([lst.index(i) for i in s.split()])), 'n/a')
	except:
		return 'n/a'


nbrs = 'zero one two three four five six seven eight nine'.split()

def average_string(s):
	try:
		return nbrs[sum(map(nbrs.index, s.split())) // len(s.split())]
	except:
		return 'n/a'


numbers = 'zero one two three four five six seven eight nine'.split()

def average_string(s):
	try:
		ns = list(map(numbers.index, s.split()))
		return numbers[sum(ns) // len(ns)]
	except (ValueError, ZeroDivisionError):
		return 'n/a'


from numpy import average
nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

def average_string(s):
	total = [nums.index(c) for c in s.split(' ') if c in nums]
	return nums[int(average(total))] if len(total) == len(s.split(' ')) else 'n/a'


def average_string(s):
	nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
	d = dict(zip(nums, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
	try:
		return nums[sum((d[w] for w in s.split())) // len(s.split())]
	except:
		return 'n/a'


s2d = 'zero one two three four five six seven eight nine'.split()

def average_string(stg):
	lst = stg.split()
	if not stg or set(lst) - set(s2d):
		return 'n/a'
	return s2d[int(sum((s2d.index(s) for s in lst)) / len(lst))]


def average_string(s):
	(d, c) = ('zero one two three four five six seven eight nine'.split(), 0)
	for i in s.split():
		if i not in d:
			return 'n/a'
		c += d.index(i)
	return d[int(c / len(s.split()))] if s else 'n/a'


def average_string(s):
	n = 0
	dic = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}
	s = s.split(' ')
	for i in range(len(s)):
		if s[i] in dic:
			n += dic[s[i]]
		else:
			return 'n/a'
	n //= len(s)
	for key in dic:
		if dic.get(key) == n:
			n = key
	return n


digit_map = {'zero': 0, 'nine': 9, 'five': 5, 'two': 2, 'three': 3, 'four': 4, 'one': 1, 'eight': 8, 'six': 6, 'seven': 7}
reverse_digit_map = dict(map(reversed, digit_map.items()))

class InValidNumException(Exception):
	pass

class StringNum(int):

	def __new__(cls, s):
		if s in digit_map:
			return int.__new__(cls, digit_map[s])
		raise InValidNumException()

def average_string(s):
	try:
		arr = [StringNum(t) for t in s.split() if t]
		if len(arr) == 0:
			return 'n/a'
		average = int(sum(arr) / len(arr))
	except InValidNumException:
		return 'n/a'
	if 0 <= average < 10:
		return reverse_digit_map[average]
	return 'n/a'


from statistics import mean
D = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}
L = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

def average_string(s):
	try:
		return L[int(mean(map(D.get, s.split())))]
	except:
		return 'n/a'


def average_string(s):
	try:
		l = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
		s = s.split(' ')
		c = 0
		for i in s:
			c += l.index(i)
		c = int(c / len(s))
		return l[c]
	except:
		return 'n/a'


def average_string(s):
	digits = list('zero,one,two,three,four,five,six,seven,eight,nine'.split(','))
	values = [digits.index(w) if w in digits else -1 for w in s.split()]
	return 'n/a' if not values or -1 in values else digits[sum(values) // len(values)]


average_string = lambda s, d='zero one two three four five six seven eight nine'.split(): (lambda l: set() < set(l) <= set(d) and d[sum(map(d.index, l)) // len(l)] or 'n/a')(s.split())


def average_string(s):
	sn = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}
	a = 0
	try:
		for i in s.split():
			a += sn[i]
		a /= len(s.split())
	except:
		return 'n/a'
	return list(sn.keys())[list(sn.values()).index(int(a))]


def average_string(s):
	res = 0
	num = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	str = ('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')
	for i in range(0, 10):
		num[i] = s.count(str[i])
	if sum(num) == 0 or sum(num) != s.count(' ') + 1:
		return 'n/a'
	for (i, val) in enumerate(num):
		res += val * i
	res /= sum(num)
	if res > 9:
		return 'n/a'
	return str[int(res)]