I hope there are people here who also decide on the Checkio fan problem. Faced the solution of the problem Flatten a list. https://py.checkio.org/mission/flatten-list/

In PyCharme my code is:

a = [] def flat_list(array): global a [flat_list(i) if isinstance(i,list) else a.append(int(i)) for i in array] return a print(flat_list([1,[2,2,2],4])) 

gives the result: [1, 2, 2, 2, 4]

On Chekio, the result: [1,2,3,1,2,2,2,4] and the test does not pass (rewrote the solution when the test passed, but the question is exactly why this code is wrapped with this result.

Question: why such a different interpretation of the code? (python versions are the same)

  • I tried to write myself in one line: flat_list = lambda array: [j for i in array for j in (flat_list(i) if isinstance(i, list) else [i])] - andreymal
  • And How? Checkio passed tests? - Oleg
  • Too lazy to check) - andreymal
  • I do not want to grieve before the new year))) - Oleg

1 answer 1

For some reason you are using a global variable.

Naturally, when you run the function several times, the results will accumulate in this variable. And with each subsequent call, the result will be contaminated by previous results.

You run the function locally once once and you see no error. And chekio runs a sequence of tests, and already on the second test there will be an error.

In general, global variables are not highly recommended.

The funny thing is that in your case the use of the variable does not even have any practical meaning. You could safely declare an empty list already inside the function. In your case, the use of such a slippery tool as global variables is generally not justified.

  • In Checkio, I did not see a ban on the use of global variables. A global variable here in the code is needed so that it is not affected by recursion. And yes, you are right, this is a bug check, programmers do not clean global variables with different tests (I think this is just a bug, because the code solves the given task). Values ​​added from previous test. - Oleg
  • The need for a global variable is needed here, because I wanted to make a decision in one line. I could not bring to mind the expression: def flat_list(array): t = [] [t += flat_list(i) if isinstance(i,list) else t.append(int(i)) for i in array] return t I had to make a decision in a few lines - Oleg
  • If you managed to do it in one line without a global variable, then it would be interesting to see. - Oleg
  • Yes, really, did not pay attention to the fact that you have a recursive call there. In this case, you can create a nested function inside the main one, so that it will go after the list is declared, and recursively call the nested one. I would not call it a checkio bug - if the output of the function being tested depends on something other than the arguments it explicitly passes to it, then this is a function problem. - Xander
  • Additionally, to describe the embedded function is not Camille. Here I wanted to show that the logic for solving this problem actually fits into one line. I only managed to do this using a global variable. - Oleg