Last active
November 23, 2022 16:08
-
-
Save jalcoding8/9da8afa96f3e3f42e094c5015279fadd to your computer and use it in GitHub Desktop.
Coding-Challenge-Python/Flatten-Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten_array(arr): | |
# Write your code here | |
flat_list = [] | |
# Iterate through the outer list | |
for element in arr: | |
#if type(element) is list: #using type() to check for nested array OR | |
if isinstance(element, list): #using isinstance() to check for nested (multi-dimensional array | |
# If the element is of type list, iterate through the sublist or inner list | |
for item in element: | |
flat_list.append(item) | |
else: | |
flat_list.append(element) | |
return flat_list | |
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9])) | |
print(flatten_array([[2], [8, 9, 10,], 5, 4, []] )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5, 6, 7, 8, 9] | |
[2, 8, 9, 10, 5, 4] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
All tests passed! | |
Submit your solution when you're ready. Once you submit, all code will be saved as final. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment