Challenge:

Given a two-dimensional array of integers, return the flattened version of the array with all the integers in the sorted (ascending) order.

Example:

Given [[3, 2, 1], [4, 6, 5], [], [9, 7, 8]], your function should return [1, 2, 3, 4, 5, 6, 7, 8, 9].

Kata's link: Flatten and sort an array

Best Practices

Py First:

from itertools import chain
def flatten_and_sort(array):
    return sorted((chain(*array)))

Py Second:

def flatten_and_sort(array):
    return sorted([j for i in array for j in i])

Py Third:

def flatten_and_sort(array):
    return sorted(sum(array, []))

Py Fourth:

from itertools import chain

def flatten_and_sort(array):
    return sorted(chain.from_iterable(array))

Py Fifth:

#from functools import reduce    # for Python 3

def flatten_and_sort(array):
    return sorted(reduce(list.__add__, array, []))

results matching ""

    No results matching ""