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,122 @@
on quickselect(theList, l, r, k)
script o
property lst : theList's items -- Shallow copy.
end script
repeat
-- Median-of-3 pivot selection.
set leftValue to item l of o's lst
set rightValue to item r of o's lst
set pivot to item ((l + r) div 2) of o's lst
set leftGreaterThanRight to (leftValue > rightValue)
if (leftValue > pivot) then
if (leftGreaterThanRight) then
if (rightValue > pivot) then set pivot to rightValue
else
set pivot to leftValue
end if
else if (pivot > rightValue) then
if (leftGreaterThanRight) then
set pivot to leftValue
else
set pivot to rightValue
end if
end if
-- Initialise pivot store indices and swap the already compared outer values here if necessary.
set pLeft to l - 1
set pRight to r + 1
if (leftGreaterThanRight) then
set item r of o's lst to leftValue
set item l of o's lst to rightValue
if (leftValue = pivot) then
set pRight to r
else if (rightValue = pivot) then
set pLeft to l
end if
else
if (leftValue = pivot) then set pLeft to l
if (rightValue = pivot) then set pRight to r
end if
-- Continue three-way partitioning.
set i to l + 1
set j to r - 1
repeat until (i > j)
set leftValue to item i of o's lst
repeat while (leftValue < pivot)
set i to i + 1
set leftValue to item i of o's lst
end repeat
set rightValue to item j of o's lst
repeat while (rightValue > pivot)
set j to j - 1
set rightValue to item j of o's lst
end repeat
if (j > i) then
if (leftValue = pivot) then
set pRight to pRight - 1
if (pRight > j) then
set leftValue to item pRight of o's lst
set item pRight of o's lst to pivot
end if
end if
if (rightValue = pivot) then
set pLeft to pLeft + 1
if (pLeft < i) then
set rightValue to item pLeft of o's lst
set item pLeft of o's lst to pivot
end if
end if
set item j of o's lst to leftValue
set item i of o's lst to rightValue
else if (i > j) then
exit repeat
end if
set i to i + 1
set j to j - 1
end repeat
-- Swap stored pivot(s) into a central partition.
repeat with p from l to pLeft
if (j > pLeft) then
set item p of o's lst to item j of o's lst
set item j of o's lst to pivot
set j to j - 1
else
set j to p - 1
exit repeat
end if
end repeat
repeat with p from r to pRight by -1
if (i < pRight) then
set item p of o's lst to item i of o's lst
set item i of o's lst to pivot
set i to i + 1
else
set i to p + 1
exit repeat
end if
end repeat
-- If k's in either of the outer partitions, repeat for that partition. Othewise return the item in slot k.
if (k i) then
set l to i
else if (k j) then
set r to j
else
return item k of o's lst
end if
end repeat
end quickselect
-- Task code:
set theVector to {9, 8, 7, 6, 5, 0, 1, 2, 3, 4}
set selected to {}
set vectorLength to (count theVector)
repeat with i from 1 to vectorLength
set end of selected to quickselect(theVector, 1, vectorLength, i)
end repeat
return selected

View file

@ -0,0 +1 @@
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

View file

@ -0,0 +1,109 @@
----------------------- QUICKSELECT ------------------------
-- quickSelect :: Ord a => [a] -> Int -> a
on quickSelect(xxs)
script
on |λ|(k)
script go
on |λ|(xxs, k)
set {x, xs} to {item 1 of xxs, rest of xxs}
set {ys, zs} to partition(gt(x), xs)
set lng to length of ys
if k < lng then
|λ|(ys, k)
else
if k > lng then
|λ|(zs, k - lng - 1)
else
x
end if
end if
end |λ|
end script
if 0 k and k < length of xxs then
tell go to |λ|(xxs, k)
else
missing value
end if
end |λ|
end script
end quickSelect
--------------------------- TEST ---------------------------
on run
set xs to {9, 8, 7, 6, 5, 0, 1, 2, 3, 4}
map(quickSelect(xs), enumFromTo(0, (length of xs) - 1))
end run
----------- GENERAL AND REUSABLE PURE FUNCTIONS ------------
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
lst
else
{}
end if
end enumFromTo
-- gt :: Ord a => a -> a -> Bool
on gt(x)
script
on |λ|(y)
x > y
end |λ|
end script
end gt
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- partition :: (a -> Bool) -> [a] -> ([a], [a])
on partition(p, xs)
tell mReturn(p)
set {ys, zs} to {{}, {}}
repeat with x in xs
set v to contents of x
if |λ|(v) then
set end of ys to v
else
set end of zs to v
end if
end repeat
end tell
{ys, zs}
end partition