DEV Community

Discussion on: Day-3 Move Zeroes

Collapse
 
mridubhatnagar profile image
Mridu Bhatnagar

Thank you, Martin.

That's a good approach as well. It looks similar to the initial approach I mentioned. However, [0] multiplied by the value of the counter would create a new list and then you are extending the list. So, the solution would not remain in-place solution. I liked capturing the ValueError approach.

Same here, I don't enjoy time challenges. Also, you can do these at your own pace. I don't do these with time. The place I feel these problems help is in understanding when to use and what to use.

Like through the initial approach I learned why index, pop would not work here, thought in the direction of in-place approach, remove, count, etc ... also have their time complexity to be O(n). So, the time complexity overall turns out to be O(n^2) likewise more similar learnings.

Collapse
 
clamytoe profile image
Martin Uribe

Extending nums doesn't really create a new list. If you check its id before and after they are still the same. Regardless, that portion of the code could be changed to:

#nums.extend([0] * count)
for _ in range(count):
    nums.insert(len(nums), 0)

And still accomplish the same and be just as fast.

~$: python move-zeroes.py
   mridu: 0.0000188351
clamytoe: 0.0000166893
 striker: 0.0000388622

I still have a lot to learn about big O notation myself. I'm not a professional coder so finding the time to learn takes a lot of effort. Also, not having a use/case for things makes it that much harder to stay focused on certain subjects.

Once again, I'm really impressed with all that you've been doing. It's very inspirational and motivates me to get my butt in gear!

Thread Thread
 
mridubhatnagar profile image
Mridu Bhatnagar • Edited

I am an absolute beginner in this time complexity thing as well. What I meant by extra memory was. When you do

nums.extend([0] * count)

[0]*count would result in [0, 0, 0]. And this list with [0, 0, 0] will have a different memory id then your nums list.

That try expect thing is really nice. I recently read about it in some blog as well. That this is a good way to do.