You are given an array that of arbitrary depth that needs to be nearly flattened into a 2 dimensional array. The given array's depth is also non-uniform, so some parts may be deeper than others.

All of lowest level arrays (most deeply nested) will contain only integers and none of the higher level arrays will contain anything but other arrays. All arrays given will be at least 2 dimensional. All lowest level arrays will contain at least one element.

Your solution should be an array containing all of the lowest level arrays and only these. The sub-arrays should be ordered by the smallest element within each, so `[1,2]` should preceed `[3,4,5]`. Note: integers will not be repeated.

For example:

If you receive `[[[1,2,3],[4,5]],[6,7]]`, your answer should be `[[1,2,3],[4,5],[6,7]]`.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def near_flatten(nested):
	

Solutions:
def near_flatten(a):
	r = []
	for x in a:
		if isinstance(x[0], int):
			r.append(x)
		else:
			r.extend(near_flatten(x))
	return sorted(r)


def near_flatten(xs):

	def f(xs):
		if all((not isinstance(x, list) for x in xs)):
			yield xs
			return
		for x in xs:
			yield from f(x)
	return sorted(f(xs))


from itertools import chain

def f(t):
	return [sorted(t)] if not isinstance(t[0], list) else chain(*(f(x) for x in t))

def near_flatten(lst):
	return sorted(chain(*(f(x) for x in lst)))


def near_flatten(a):
	li = []

	def do(a):
		for i in a:
			li.append(i if all((isinstance(k, int) for k in i)) else do(i))
	do(a)
	return sorted([i for i in li if i])


def near_flatten(nested):
	result = []
	for item in nested:
		if type(item[0]) == list:
			result.extend(near_flatten(item))
		else:
			result.append(item)
	return sorted(result, key=lambda l: l[0])


from itertools import chain

def flatten(list_):
	return [list_] if isinstance(list_[0], int) else chain(*(flatten(l) for l in list_))

def near_flatten(nested):
	return sorted(chain(*(flatten(list_) for list_ in nested)))


def near_flatten(nested):
	ret = []

	def flatten(arr):
		for (i, ar) in enumerate(arr):
			if type(ar[0]) == int:
				ret.append(ar)
			else:
				flatten(ar)
	flatten(nested)
	return sorted(ret)


import re
r = re.compile('(?<=\\[)\\[|(?<=\\])\\]')

def near_flatten(nested):
	return sorted(eval('[' + r.sub('', str(nested)) + ']'), key=lambda x: x[0])


import re

def near_flatten(nested):
	nested = str(nested)
	flatten_regex = re.compile('\\[+(\\d+)(?:, )?(\\d+)?(?:, )?(\\d+)?(?:, )?(\\d+)?\\]+')
	matches = flatten_regex.finditer(nested)
	fixed_ls = [flatten_regex.sub('\\1 \\2 \\3 \\4', i.group()).split() for i in matches]
	fixed_ls = [[int(x) for x in i] for i in fixed_ls]
	return sorted(fixed_ls)


def near_flatten(A):
	s = str(A)[1:-1]
	while '[[' in s:
		s = s.replace('[[', '[')
	while ']]' in s:
		s = s.replace(']]', ']')
	return sorted((list(map(int, x.split(','))) for x in s[1:-1].split('], [')))