Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in algorithms/array/flatten.py, line 10 #173

Closed
TheoYuan opened this issue Mar 28, 2018 · 1 comment
Closed

Bug in algorithms/array/flatten.py, line 10 #173

TheoYuan opened this issue Mar 28, 2018 · 1 comment

Comments

@TheoYuan
Copy link

TheoYuan commented Mar 28, 2018

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.

@goswami-rahul
Copy link
Collaborator

Nice catch @TheoYuan . if outputArr is None works fine 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants