inside-out range function

Scott David Daniels Scott.Daniels at Acm.Org
Tue Apr 28 18:27:59 CEST 2009


Steven D'Aprano wrote:
> ... I wrote a similar function to do this:
> 
> def monge_shuffle(deck):
>     if len(deck) % 2: # Odd number of items.
>         deck[:] = deck[0::2] + deck[1::2][::-1]
>     else: # Even number of items.
>         deck[:] = deck[1::2] + deck[0::2][::-1]
>     return deck

Oooh, shiny fun!!!  How's this for that:

def monge_shuffle2(deck):
     even = (len(deck) ^ 1) & 1
     return deck[even::2] + deck[-2::-2]

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list