Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Knuth-shuffle/AppleScript/knuth-shuffle-1.applescript
Normal file
14
Task/Knuth-shuffle/AppleScript/knuth-shuffle-1.applescript
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
set n to 25
|
||||
|
||||
set array to {}
|
||||
repeat with i from 1 to n
|
||||
set end of array to i
|
||||
end repeat
|
||||
copy {array, array} to {unshuffled, shuffled}
|
||||
repeat with i from n to 1 by -1
|
||||
set j to (((random number) * (i - 1)) as integer) + 1
|
||||
set shuffled's item i to array's item j
|
||||
if j ≠ i's contents then set array's item j to array's item i
|
||||
end repeat
|
||||
|
||||
return {unshuffled, shuffled}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
|
||||
{14, 25, 3, 1, 12, 18, 11, 20, 16, 15, 21, 5, 22, 19, 2, 24, 8, 10, 13, 6, 17, 23, 9, 7, 4}}
|
||||
25
Task/Knuth-shuffle/AppleScript/knuth-shuffle-3.applescript
Normal file
25
Task/Knuth-shuffle/AppleScript/knuth-shuffle-3.applescript
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
-- Fisher-Yates (aka Durstenfeld, aka Knuth) shuffle.
|
||||
on shuffle(theList, l, r)
|
||||
set listLength to (count theList)
|
||||
if (listLength < 2) then return array
|
||||
if (l < 0) then set l to listLength + l + 1
|
||||
if (r < 0) then set r to listLength + r + 1
|
||||
if (l > r) then set {l, r} to {r, l}
|
||||
script o
|
||||
property lst : theList
|
||||
end script
|
||||
|
||||
repeat with i from l to (r - 1)
|
||||
set j to (random number from i to r)
|
||||
set v to o's lst's item i
|
||||
set o's lst's item i to o's lst's item j
|
||||
set o's lst's item j to v
|
||||
end repeat
|
||||
|
||||
return theList
|
||||
end shuffle
|
||||
|
||||
local array
|
||||
set array to {"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike"}
|
||||
-- Shuffle all items (1 thru -1).
|
||||
shuffle(array, 1, -1)
|
||||
|
|
@ -0,0 +1 @@
|
|||
{"Golf", "Foxtrot", "Echo", "Delta", "Kilo", "Charlie", "Mike", "Alpha", "Lima", "Juliett", "India", "Bravo", "Hotel"}
|
||||
24
Task/Knuth-shuffle/AppleScript/knuth-shuffle-5.applescript
Normal file
24
Task/Knuth-shuffle/AppleScript/knuth-shuffle-5.applescript
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use AppleScript version "2.5" -- OS X 10.11 (El Capitan) or later
|
||||
use framework "Foundation"
|
||||
use framework "GameplayKit"
|
||||
|
||||
on shuffle(theList, l, r)
|
||||
set listLength to (count theList)
|
||||
if (listLength < 2) then return theList
|
||||
if (l < 0) then set l to listLength + l + 1
|
||||
if (r < 0) then set r to listLength + r + 1
|
||||
if (l > r) then set {l, r} to {r, l}
|
||||
script o
|
||||
property lst : theList
|
||||
end script
|
||||
|
||||
set rndGenerator to current application's class "GKRandomDistribution"'s distributionWithLowestValue:(l) highestValue:(r)
|
||||
repeat with i from r to (l + 1) by -1
|
||||
set j to (rndGenerator's nextIntWithUpperBound:(i))
|
||||
set v to o's lst's item i
|
||||
set o's lst's item i to o's lst's item j
|
||||
set o's lst's item j to v
|
||||
end repeat
|
||||
|
||||
return theList
|
||||
end shuffle
|
||||
72
Task/Knuth-shuffle/AppleScript/knuth-shuffle-6.applescript
Normal file
72
Task/Knuth-shuffle/AppleScript/knuth-shuffle-6.applescript
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
-- KNUTH SHUFFLE -------------------------------------------------------------
|
||||
|
||||
-- knuthShuffle :: [a] -> [a]
|
||||
on knuthShuffle(xs)
|
||||
|
||||
-- randomSwap :: [Int] -> Int -> [Int]
|
||||
script randomSwap
|
||||
on |λ|(a, i)
|
||||
if i > 1 then
|
||||
set iRand to random number from 1 to i
|
||||
tell a
|
||||
set tmp to item iRand
|
||||
set item iRand to item i
|
||||
set item i to tmp
|
||||
it
|
||||
end tell
|
||||
else
|
||||
a
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldr(randomSwap, xs, enumFromTo(1, length of xs))
|
||||
end knuthShuffle
|
||||
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
knuthShuffle(["alpha", "beta", "gamma", "delta", "epsilon", ¬
|
||||
"zeta", "eta", "theta", "iota", "kappa", "lambda", "mu"])
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m > n then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
-- foldr :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldr(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from lng to 1 by -1
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldr
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
{"mu", "theta", "alpha", "delta", "zeta", "gamma",
|
||||
"iota", "kappa", "lambda", "epsilon", "beta", "eta"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue