+ 1

False == 0 problem

So there is a challenge in a website which is taking away my mental health The chalange is to move zeros of a list to the end without messing other items indices. My code: def move_zeros(array): for i in array: if str(i).isnumeric(): if i == 0: array.remove(i) array.append(i) return array The problem is the condition in my function doesn't filter the False if its present in the list. For example in this list : ["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9] It keeps removing False and adding 0 to the end I wrote another condition: for i in array: if type(i) == int and i == 0: array.remove(i) array.append(i) return array And even I tried (if str(i) == "0") But it just doesn't work and honestly I don't know why and what I'm missing here. Please help me understand.

30th Dec 2020, 11:32 AM
pedram ch
pedram ch - avatar
15 Respostas
+ 4
Try if this will work: First, I made 2 new lists inside the function (arr and arr2). These 2 list will serve as our temporary storage of elements. - - - - - - - - - - - - - - - If it is 0, store it to "arr2". Otherwise, store it to "arr". For Example: INPUT: [1, 2, 0, False, 0, 4] # after iteration (zeroes to arr2, non-zeroes to arr) >> arr = [1, 2, False, 4] >> arr2 = [0, 0] - - - - - - - - - - - - - - Lastly, we used "arr.extend(arr2)" or just simply "arr += arr2" to concatenate them together. And this will be the output. >> [1, 2, False, 4, 0, 0] And I think the issue on your code is not the False == 0, I think it is the >> arr.remove(i) >> arr.append(i) - - - - - - - - - - - - - - - However this is just my own implementation. I will still try to solve and stick with your code. https://code.sololearn.com/c63TT79OzpPG/?ref=app
30th Dec 2020, 12:16 PM
noteve
noteve - avatar
+ 3
You can: -loop through an ENUMERATED list -whenever you find a zero, pop the index -append a 0 to the end And keep in mind: 0 will never equal "0" False == 0
30th Dec 2020, 12:31 PM
Slick
Slick - avatar
+ 3
pedram ch The False is filtered out. See this for further explanation. If something is still not clear, please feel free to ask. Thanks! https://code.sololearn.com/cSMfsY7PHMiq/?ref=app
30th Dec 2020, 12:54 PM
noteve
noteve - avatar
+ 2
pedram ch Thanks! I'll check that site some time. And by the way, those solutions are good though.
1st Jan 2021, 1:46 AM
noteve
noteve - avatar
+ 2
pedram ch Yeah, thanks for recommending that site. Its good for practicing. 👍
1st Jan 2021, 8:21 AM
noteve
noteve - avatar
+ 1
《 Nicko12 》I understood your code thank you If we add (or str(array[i]) == '0.0') to the if condition it works since we also may have 0.0 in the list. But my question is why those conditions which I mentioned in the question doesn't filter out False. Type(False) is boolean , str(False).isnumeric() will give us false and str(False) == "0" is also False but none of them filters out the False and thats what confused me as hell. But that was a nice solution thank you for your help
30th Dec 2020, 12:43 PM
pedram ch
pedram ch - avatar
+ 1
《 Nicko12 》 thank you for the clear explanation in your code I really appreciate it Also I tried your edited code and it worked fine That was a life saver 😅 Now that I solved it other people answers is unlocked for me to see I can share some of them with you if you are interested
30th Dec 2020, 1:07 PM
pedram ch
pedram ch - avatar
+ 1
《 Nicko12 》 sorry for delay The website is codewars they have legit challanges Two of people answers : def move_zeros(arr): l = [i for i in arr if isinstance(i, bool) or i!=0] return l+[0]*(len(arr)-len(l)) def move_zeros(array): return sorted(array, key=lambda x: x==0 and type(x) is not bool) đŸ€ŻđŸ€Ż
1st Jan 2021, 12:03 AM
pedram ch
pedram ch - avatar
+ 1
《 Nicko12 》 yes they are super smart
1st Jan 2021, 8:03 AM
pedram ch
pedram ch - avatar
+ 1
《 Nicko12 》 offcours man Happy coding 👍
1st Jan 2021, 8:51 AM
pedram ch
pedram ch - avatar
0
Slick you are right I meant str(i) == "0" But why those conditions wont filter out the False from list I ran each of them separately in my ide and it worked (out of function) but it seems when they go in the function they just stop doing what they should do
30th Dec 2020, 12:50 PM
pedram ch
pedram ch - avatar
0
《 Nicko12 》 when I tried to submit your code it gave another error It should work for random inputs too: [False, 10, True, -2, 8, 1, 'string', 8, True, 7, 'y', 0, '0', 0, 0, 0] should equal [False, 10, True, -2, 8, '0', 1, 'string', 8, True, 7, 'y', 0, 0, 0, 0] Its because we may also have "0" in the list members.
30th Dec 2020, 12:55 PM
pedram ch
pedram ch - avatar
0
《 Nicko12 》 no just 0 integers should be at the end
30th Dec 2020, 12:58 PM
pedram ch
pedram ch - avatar
0
pedram ch I edited my code above. Try it if it works.
30th Dec 2020, 1:00 PM
noteve
noteve - avatar
0
pedram ch Oh do you mean other people's solution to this problem/challenge? Well.. Yes Ok, I think that will help me on my futures code and maybe I can learn from it too. Thanks! I appreciate it!
30th Dec 2020, 1:56 PM
noteve
noteve - avatar