Python Forum
Where is the loophole in my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where is the loophole in my code
#1
Question is:

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

   You must do this in-place without making a copy of the array.
   Minimize the total number of operations.

My code has passed a simple case, but failed an extreme case which has the fifth last element as 0. I don't know where is the loophole in my code.

L

Here is my code:

class Solution(object):
    def moveZeroes(self, nums):
        i=0
        Count=0
        ZeroCount=0
        MaxCount=(len(nums)-ZeroCount)
        print("Input Size=",len(nums))
        while Count <MaxCount-ZeroCount:
            Count=Count+1
            if nums[i]==0:
               nums.pop(i)
               nums.append(0)
               ZeroCount=ZeroCount+1
               i=i-1
            i=i+1
        return nums    


print(Solution().moveZeroes([0,1,0,3,12]))
print(Solution().moveZeroes([-959151711,623836953,209446690,-1950418142,1339915067,-733626417,481171539,-2125997010,-1225423476,1462109565,147434687,-1800073781,-1431212205,-450443973,50097298,753533734,-747189404,-2070885638,0,-1484353894,-340296594,-2133744570,619639811,-1626162038,669689561,0,112220218,502447212,-787793179,0,-726846372,-1611013491,204107194,1605165582,-566891128,2082852116,0,532995238,-1502590712,0,2136989777,-2031153343,371398938,-1907397429,342796391,609166045,-2007448660,-1096076344,-323570318,0,-2082980371,2129956379,-243553361,-1549960929,1502383415,0,-1394618779,694799815,78595689,-1439173023,-1416578800,685225786,-333502212,-1181308536,-380569313,772035354,0,-915266376,663709718,1443496021,-777017729,-883300731,-387828385,1907473488,-725483724,-972961871,-1255712537,383120918,1383877998,1722751914,0,-1156050682,1952527902,-560244497,1304305692,1173974542,-1313227247,-201476579,-298899493,-1828496581,-1724396350,1933643204,1531804925,1728655262,-955565449,0,-69843702,-461760848,268336768,1446130876]))
Output is:
Quote:Input Size= 5
[1, 3, 12, 0, 0]
Input Size= 100
[-959151711, 623836953, 209446690, -1950418142, 1339915067, -733626417, 481171539, -2125997010, -1225423476, 1462109565, 147434687, -1800073781, -1431212205, -450443973, 50097298, 753533734, -747189404, -2070885638, -1484353894, -340296594, -2133744570, 619639811, -1626162038, 669689561, 112220218, 502447212, -787793179, -726846372, -1611013491, 204107194, 1605165582, -566891128, 2082852116, 532995238, -1502590712, 2136989777, -2031153343, 371398938, -1907397429, 342796391, 609166045, -2007448660, -1096076344, -323570318, -2082980371, 2129956379, -243553361, -1549960929, 1502383415, -1394618779, 694799815, 78595689, -1439173023, -1416578800, 685225786, -333502212, -1181308536, -380569313, 772035354, -915266376, 663709718, 1443496021, -777017729, -883300731, -387828385, 1907473488, -725483724, -972961871, -1255712537, 383120918, 1383877998, 1722751914, -1156050682, 1952527902, -560244497, 1304305692, 1173974542, -1313227247, -201476579, -298899493, -1828496581, -1724396350, 1933643204, 1531804925, 1728655262, -955565449, 0, -69843702, -461760848, 268336768, 1446130876, 0, 0, 0, 0, 0, 0, 0, 0, 0]



I highlighted the 0 in red that is missed by the expected operation.
Reply


Messages In This Thread
Where is the loophole in my code - by landlord1984 - Jan-22-2017, 04:05 AM
RE: Where is the loophole in my code - by Mekire - Jan-22-2017, 04:40 AM
RE: Where is the loophole in my code - by micseydel - Jan-22-2017, 05:53 AM
RE: Where is the loophole in my code - by wavic - Jan-22-2017, 07:09 AM
RE: Where is the loophole in my code - by Mekire - Jan-22-2017, 07:11 AM
RE: Where is the loophole in my code - by Ofnuts - Jan-24-2017, 08:59 AM
RE: Where is the loophole in my code - by Mekire - Jan-24-2017, 11:01 AM
RE: Where is the loophole in my code - by hsunteik - Jan-24-2017, 11:22 AM
RE: Where is the loophole in my code - by Mekire - Jan-24-2017, 11:40 AM
RE: Where is the loophole in my code - by micseydel - Jan-24-2017, 06:34 PM
RE: Where is the loophole in my code - by Ofnuts - Jan-24-2017, 09:34 PM
RE: Where is the loophole in my code - by micseydel - Jan-24-2017, 09:37 PM
RE: Where is the loophole in my code - by micseydel - Jan-27-2017, 12:42 AM
RE: Where is the loophole in my code - by micseydel - Jan-27-2017, 01:02 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020