DEV Community

Discussion on: Day-3 Move Zeroes

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.