Monday, September 11, 2006

The quest for the ninja monkey.

I rechristened the site name, in the hopes of reviving something which was lost. Behold for I give you the random permutation.
Language: Python
# Deutsche bank's online banking page uses a O(n^3) algorithm to generate a
# random character array.
# This is a O(n) way to do the same, for any array.

def randomPermute(array):
    import random
    for i in range(len(array)):
        randIndex=random.randint(i,len(array)-1)                               
        (array[i],array[randIndex])=(array[randIndex],array[i])
    return array

# Lets experiment with the english alphabet
import string
print randomPermute(list(string.ascii_letters[:26]))

No comments: