Assessment for Team Lead - Quality Testing at Emoha
Last Updated: 27th Jan 2025
Q1. Write a function to flatten a nested array using a recursive approach.
The function should take an array that may contain nested arrays of
arbitrary depth as input and return a single, flattened array containing all
the elements.
Input: [1, [2, [3, [4]], 5], 6]
Output: [1, 2, 3, 4, 5, 6]
Code:
def flatten_array(nested_array):
a = []
for item in nested_array:
if isinstance(item, list):
a.extend(flatten_array(item))
else:
a.append(item)
return a
# Test the function
input_array = [1, [2, [3, [4]], 5], 6]
print(f"The array of arbitrary depth : {input_array} ")
result = flatten_array(input_array)
print (f"The flattened array with all the element are: {result}")
Output:
The array of arbitrary depth : [1, [2, [3, [4]], 5], 6]
The flattened array with all the element are: [1, 2, 3, 4, 5, 6]
Q2. Implement a function to find all subsets of an array.
Code:
def find_subset(array):
sets = [[]]
for element in array:
new = []
for i in sets:
new.append(i + [element])
sets = new + sets
return sets
# Testing the function
input_array = [1, 2, 3]
print(f'The array entered: {input_array}')
result = find_subset(input_array)
print(f'The subsets of the array : {result}')
Output:
The array entered: [1, 2, 3]
The subsets of the array : [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
** Process exited - Return Code: 0 **
Press Enter to exit terminal