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
#2
Not positive where the error is in your current (likely some index issue) but if you simplify it a bit it works fine.  Note however it is a O(n^2) solution.
class Solution(object):
    def moveZeroes(self, nums):
        i = 0
        moved = 0
        zero_count = nums.count(0)
        while moved < zero_count:
            if nums[i] == 0:
               nums.pop(i)
               nums.append(0)
               moved += 1
               continue
            i += 1
        return nums
I prefer this, though I almost guarantee your teacher will hate it:  
nums.sort(key=lambda x: x == 0)
Why work harder when you can work smarter?
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