2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,44 @@
|
|||
set alist to {1, 2, 3, 4, 5, 6, 7, 8}
|
||||
set med to medi(alist)
|
||||
|
||||
on medi(alist)
|
||||
|
||||
set temp to {}
|
||||
set lcount to count every item of alist
|
||||
if lcount is equal to 2 then
|
||||
return (item (random number from 1 to 2) of alist)
|
||||
else if lcount is less than 2 then
|
||||
return item 1 of alist
|
||||
else --if lcount is greater than 2
|
||||
set min to findmin(alist)
|
||||
set max to findmax(alist)
|
||||
repeat with x from 1 to lcount
|
||||
if x is not equal to min and x is not equal to max then set end of temp to item x of alist
|
||||
end repeat
|
||||
set med to medi(temp)
|
||||
end if
|
||||
return med
|
||||
|
||||
end medi
|
||||
|
||||
on findmin(alist)
|
||||
|
||||
set min to 1
|
||||
set alength to count every item of alist
|
||||
repeat with x from 1 to alength
|
||||
if item x of alist is less than item min of alist then set min to x
|
||||
end repeat
|
||||
return min
|
||||
|
||||
end findmin
|
||||
|
||||
on findmax(alist)
|
||||
|
||||
set max to 1
|
||||
set alength to count every item of alist
|
||||
repeat with x from 1 to alength
|
||||
if item x of alist is greater than item max of alist then set max to x
|
||||
end repeat
|
||||
return max
|
||||
|
||||
end findmax
|
||||
107
Task/Averages-Median/AppleScript/averages-median-2.applescript
Normal file
107
Task/Averages-Median/AppleScript/averages-median-2.applescript
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
-- median :: [Num] -> Num
|
||||
on median(xs)
|
||||
-- nth :: [Num] -> Int -> Maybe Num
|
||||
script nth
|
||||
on lambda(xxs, n)
|
||||
if length of xxs > 0 then
|
||||
set {x, xs} to uncons(xxs)
|
||||
|
||||
script belowX
|
||||
on lambda(y)
|
||||
y < x
|
||||
end lambda
|
||||
end script
|
||||
|
||||
set {ys, zs} to partition(belowX, xs)
|
||||
set k to length of ys
|
||||
if k = n then
|
||||
x
|
||||
else
|
||||
if k > n then
|
||||
lambda(ys, n)
|
||||
else
|
||||
lambda(zs, n - k - 1)
|
||||
end if
|
||||
end if
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end lambda
|
||||
end script
|
||||
|
||||
set n to length of xs
|
||||
if n > 0 then
|
||||
tell nth
|
||||
if n mod 2 = 0 then
|
||||
(lambda(xs, n div 2) + lambda(xs, (n div 2) - 1)) / 2
|
||||
else
|
||||
lambda(xs, n div 2)
|
||||
end if
|
||||
end tell
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end median
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
map(median, [¬
|
||||
[], ¬
|
||||
[5, 3, 4], ¬
|
||||
[5, 4, 2, 3], ¬
|
||||
[3, 4, 1, -8.4, 7.2, 4, 1, 1.2]])
|
||||
|
||||
--> {missing value, 4, 3.5, 2.1}
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- partition :: predicate -> List -> (Matches, nonMatches)
|
||||
-- partition :: (a -> Bool) -> [a] -> ([a], [a])
|
||||
on partition(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {{}, {}}
|
||||
repeat with x in xs
|
||||
set v to contents of x
|
||||
set end of item ((lambda(v) as integer) + 1) of lst to v
|
||||
end repeat
|
||||
end tell
|
||||
{item 2 of lst, item 1 of lst}
|
||||
end partition
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
if length of xs > 0 then
|
||||
{item 1 of xs, rest of xs}
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end uncons
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, 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 lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- 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 lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1 @@
|
|||
{missing value, 4, 3.5, 2.1}
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
set alist to {1,2,3,4,5,6,7,8}
|
||||
set med to medi(alist)
|
||||
|
||||
on medi(alist)
|
||||
|
||||
set temp to {}
|
||||
set lcount to count every item of alist
|
||||
if lcount is equal to 2 then
|
||||
return (item (random number from 1 to 2) of alist)
|
||||
else if lcount is less than 2 then
|
||||
return item 1 of alist
|
||||
else --if lcount is greater than 2
|
||||
set min to findmin(alist)
|
||||
set max to findmax(alist)
|
||||
repeat with x from 1 to lcount
|
||||
if x is not equal to min and x is not equal to max then set end of temp to item x of alist
|
||||
end repeat
|
||||
set med to medi(temp)
|
||||
end if
|
||||
return med
|
||||
|
||||
end medi
|
||||
|
||||
on findmin(alist)
|
||||
|
||||
set min to 1
|
||||
set alength to count every item of alist
|
||||
repeat with x from 1 to alength
|
||||
if item x of alist is less than item min of alist then set min to x
|
||||
end repeat
|
||||
return min
|
||||
|
||||
end findmin
|
||||
|
||||
on findmax(alist)
|
||||
|
||||
set max to 1
|
||||
set alength to count every item of alist
|
||||
repeat with x from 1 to alength
|
||||
if item x of alist is greater than item max of alist then set max to x
|
||||
end repeat
|
||||
return max
|
||||
|
||||
end findmax
|
||||
Loading…
Add table
Add a link
Reference in a new issue