4 ms·
How about: def kill(size, nth): lst = range(size) offset = nth-1 len_ = len(lst) while len_ > 1: del lst[offset
by algorias 17y ago
How about:
def kill(size, nth):
lst = range(size)
offset = nth-1
len_ = len(lst)
while len_ > 1:
del lst[offset::nth]
offset = (offset - len_)% nth
len_ = len(lst)
return lst[0]
It will reduce the list by 1/3rd each pass, so it's significantly faster at larger sizes.
- devinj 17y agoLooks great, except for the corner case of nth = 1. I was too lazy to figure out the slicing solution myself. :)