Unsorting(randomizing) a sequence
Jeremy Hylton
jeremy at cnri.reston.va.us
Thu Aug 19 12:05:52 EDT 1999
>>>>> "DS" == Dan Schmidt <dfan at harmonixmusic.com> writes:
DS> Ian Clarke <I.Clarke at strs.co.uk> writes:
| I did some timing tests using the code I have attached below, here are
| the results (in seconds) for sorting a list of 200 items 10 times:
|
| Dan Schmidt's solution (using sort): 14.98
|
| def s1(list):
| """Dan Schmidt's solution - (mis)using sort"""
| def randomize(a,b):
| return random.choice([-1, 0, 1])
| list.sort(randomize)
| return
DS> That was Jeremy Hylton's solution. I just complained about it!
Let's not attach anyone's name to it :-). I like it because it's a
obscure/silly approach, not because it produces a good result. It
sounds like a shorter way to spell it, though, would be:
list.sort(lambda x,y:random.choice([0,1]))
Jeremy
More information about the Python-list
mailing list