You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run the flatten(inputArr, outputArr=None) function in python3.5.
It seams ok when the test case is [2, 1, [3, [4, 5], 6], 7, [8]].
But when using [[3, [4, 5], 6], 7, [8]], function outputs [7, 8] not [3, 4, 5, 6, 7, 8].
That is because not None and not [] will be both True in line 10.
Take a = [[3, [4, 5], 6], 7, [8]] as example, first call flatten(a, None).
It will call flatten([3, [4, 5], 6], []). Run till line 10, not outputArr is True where outputArr is [].
Then outputArr will be assigned a new empty list. So it lose [3, 4, 5, 6] in recursive.
It is fine because the first element is single so not [2] is False in given test case.
Bug can fix in: if outputArr is None:
or def flatten(inputArr, outputArr=[]) and delete the if statement.
The text was updated successfully, but these errors were encountered:
Run the
flatten(inputArr, outputArr=None)
function in python3.5.It seams ok when the test case is [2, 1, [3, [4, 5], 6], 7, [8]].
But when using [[3, [4, 5], 6], 7, [8]], function outputs [7, 8] not [3, 4, 5, 6, 7, 8].
That is because
not None
andnot []
will be both True in line 10.Take a = [[3, [4, 5], 6], 7, [8]] as example, first call
flatten(a, None)
.It will call
flatten([3, [4, 5], 6], [])
. Run till line 10,not outputArr
is True where outputArr is [].Then outputArr will be assigned a new empty list. So it lose [3, 4, 5, 6] in recursive.
It is fine because the first element is single so
not [2]
is False in given test case.Bug can fix in:
if outputArr is None:
or
def flatten(inputArr, outputArr=[])
and delete the if statement.The text was updated successfully, but these errors were encountered: