Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69738771fb5a7b8b24cca29d Challenge 171: Flatten the Array 29 challenge-171

--description--

Given an array that contains nested arrays, return a new array with all values flattened into a single, one-dimensional array. Retain the original order of the items in the arrays.

--hints--

flatten([1, [2, 3], 4]) should return [1, 2, 3, 4].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(flatten([1, [2, 3], 4]), [1, 2, 3, 4])`)
}})

flatten([5, [4, [3, 2]], 1]) should return [5, 4, 3, 2, 1].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(flatten([5, [4, [3, 2]], 1]), [5, 4, 3, 2, 1])`)
}})

flatten(["A", [[[["B"]]]], "C"]) should return ["A", "B", "C"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(flatten(["A", [[[["B"]]]], "C"]), ["A", "B", "C"])`)
}})

flatten([["L", "M", "N"], ["O", ["P", "Q", ["R", ["S", ["T", "U"]]]]], "V", ["W", ["X", ["Y", ["Z"]]]]]) should return ["L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(flatten([["L", "M", "N"], ["O", ["P", "Q", ["R", ["S", ["T", "U"]]]]], "V", ["W", ["X", ["Y", ["Z"]]]]]), ["L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"])`)
}})

flatten([["red", ["blue", ["green", ["yellow", ["purple"]]]]], "orange", ["pink", ["brown"]]]) should return ["red","blue","green","yellow","purple","orange","pink","brown"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(flatten([["red", ["blue", ["green", ["yellow", ["purple"]]]]], "orange", ["pink", ["brown"]]]), ["red","blue","green","yellow","purple","orange","pink","brown"])`)
}})

--seed--

--seed-contents--

def flatten(arr):

    return arr

--solutions--

def flatten(arr):
    result = []
    for item in arr:
        if isinstance(item, list):
            result.extend(flatten(item))
        else:
            result.append(item)
    return result