Skip to content

Instantly share code, notes, and snippets.

@lqez
Last active August 29, 2015 14:10
Show Gist options
  • Save lqez/e0acd5fcf8db9ec985b7 to your computer and use it in GitHub Desktop.
Save lqez/e0acd5fcf8db9ec985b7 to your computer and use it in GitHub Desktop.
Let's try monkeysort
from random import shuffle
s = list('typewriter')
tried = 0
while(True):
tried += 1
shuffle(s)
if ''.join(s) == 'eeiprrttwy':
break
print tried, s
@lqez
Copy link
Author

lqez commented Nov 27, 2014

$ time python monkeysort.py
206832 ['e', 'e', 'i', 'p', 'r', 'r', 't', 't', 'w', 'y']

real 0m1.416s
user 0m1.289s
sys 0m0.044s

@lqez
Copy link
Author

lqez commented Nov 27, 2014

P R O F I T

@aanoaa
Copy link

aanoaa commented Nov 27, 2014

use List::Util 'shuffle';
@s = split //, 'typewriter';
$tried = 0;
while (1) {
    @s = shuffle(@s);
    last if join( '', @s ) eq 'eeiprrttwy';
    $tried++;
}

print "$tried [@s]\n";

___DATA__
$ time perl a.pl 
653881 [e e i p r r t t w y]
1.235 secs

@aanoaa
Copy link

aanoaa commented Nov 27, 2014

노잼.. i cri..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment