Challenge:

Given a two-dimensional array, return a new array which carries over only those arrays from the original, which were not empty and whose items are all of the same type (i.e. homogenous). For simplicity, the arrays inside the array will only contain characters and integers.

Example:

Given [[1, 5, 4], ['a', 3, 5], ['b'], [], ['1', 2, 3]], your function should return [[1, 5, 4], ['b']].

Addendum:

Please keep in mind that for this kata, we assume that empty arrays are not homogenous.

The resultant arrays should be in the order they were originally in and should not have its values changed.

No implicit type casting is allowed. A subarray [1, '2'] would be considered illegal and should be filtered out.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Starter code: def filter_homogenous(arrays):
	

Solutions:
def filter_homogenous(arrays):
	return [a for a in arrays if len(set(map(type, a))) == 1]


def homogenous(a):
	return len(set(map(type, a))) == 1

def filter_homogenous(arrays):
	return list(filter(homogenous, arrays))


def is_homogenous(array):
	return len(set(map(type, array))) == 1

def filter_homogenous(arrays):
	return list(filter(is_homogenous, arrays))


def filter_homogenous(arrays):
	return [a for a in arrays if a and all((type(a[0]) == type(b) for b in a))]


def filter_homogenous(arrays):
	list = []
	for list1 in arrays:
		n = 0
		x = 0
		while n < len(list1):
			if type(list1[0]) == type(list1[n]):
				n += 1
				x = 1
			else:
				x = -1
				break
		if x == 1:
			list.append(list1)
	return list


def filter_homogenous(b):
	return [a for a in b if bool(a) and all([type(v1) == type(v2) for (v1, v2) in zip(a, a[1:])])]


def f(array):
	if array == []:
		return False
	return all([type(array[0]) == type(e) for e in array])

def filter_homogenous(arrays):
	return [a for a in arrays if f(a)]


from typing import Any, List

def filter_homogenous(arrays: List[Any]) -> List[Any]:
	return list(filter(lambda _: len(_) and len(set(map(type, _))) == 1, arrays))


def filter_homogenous(arrays):
	k = []
	while arrays:
		x = arrays.pop()
		if x and all((type(x[0]) == type(i) for i in x[1:])):
			k.insert(0, x)
	return k