Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,10 @@
from random import randrange
def knuth_shuffle(x):
for i in range(len(x)-1, 0, -1):
j = randrange(i + 1)
x[i], x[j] = x[j], x[i]
x = list(range(10))
knuth_shuffle(x)
print("shuffled:", x)

View file

@ -0,0 +1,95 @@
'''Knuth shuffle as a fold'''
from functools import reduce
from random import randint
# knuthShuffle :: [a] -> IO [a]
def knuthShuffle(xs):
'''A pseudo-random shuffle of the elements in xs.'''
return reduce(
swapped,
enumerate(randoms(len(xs))), xs
)
# swapped :: (Int, Int) -> [a] -> [a]
def swapped(xs, ij):
'''New list in which the elements at indices
i and j of xs are swapped.
'''
def go(a, b):
if a != b:
m, n = (a, b) if b > a else (b, a)
l, ht = splitAt(m)(xs)
ys, zs = splitAt((n - m) - 1)(ht[1:])
return l + [zs[0]] + ys + [ht[0]] + zs[1:]
else:
return xs
i, j = ij
z = len(xs) - 1
return xs if i > z or j > z else go(i, j)
# randoms :: Int -> IO [Int]
def randoms(n):
'''Pseudo-random list of n - 1 indices.
'''
return list(map(randomRInt(0)(n - 1), range(1, n)))
# TEST ----------------------------------------------------
# main :: IO ()
def main():
'''Repeated Knuth shuffles of ['a' .. 'k']'''
print(
fTable(main.__doc__ + ':\n')(str)(lambda x: ''.join(x))(
lambda _: knuthShuffle(list('abcdefghijk'))
)(range(1, 11))
)
# GENERIC -------------------------------------------------
# randomRInt :: Int -> Int -> IO () -> Int
def randomRInt(m):
'''The return value of randomRInt is itself
a function. The returned function, whenever
called, yields a a new pseudo-random integer
in the range [m..n].
'''
return lambda n: lambda _: randint(m, n)
# splitAt :: Int -> [a] -> ([a], [a])
def splitAt(n):
'''A tuple pairing the prefix of length n
with the rest of xs.
'''
return lambda xs: (xs[0:n], xs[n:])
# FORMATTING -----------------------------------------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -0,0 +1,21 @@
[ [] swap dup size times
[ dup size random pluck
nested rot join swap ]
drop ] is shuffle ( [ --> [ )
[ temp put
2dup swap
temp share swap peek
temp share rot peek
dip
[ swap
temp take
swap poke
temp put ]
swap
temp take
swap poke ] is [exch] ( n n [ --> [ )
[ dup size 1 - times
[ i 1+ dup 1+ random
rot [exch] ] ] is knuffle ( [ --> [ )