Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Combinations/AppleScript/combinations-1.applescript
Normal file
27
Task/Combinations/AppleScript/combinations-1.applescript
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
on comb(n, k)
|
||||
set c to {}
|
||||
repeat with i from 1 to k
|
||||
set end of c to i's contents
|
||||
end repeat
|
||||
set r to {c's contents}
|
||||
repeat while my next_comb(c, k, n)
|
||||
set end of r to c's contents
|
||||
end repeat
|
||||
return r
|
||||
end comb
|
||||
|
||||
on next_comb(c, k, n)
|
||||
set i to k
|
||||
set c's item i to (c's item i) + 1
|
||||
repeat while (i > 1 and c's item i ≥ n - k + 1 + i)
|
||||
set i to i - 1
|
||||
set c's item i to (c's item i) + 1
|
||||
end repeat
|
||||
if (c's item 1 > n - k + 1) then return false
|
||||
repeat with i from i + 1 to k
|
||||
set c's item i to (c's item (i - 1)) + 1
|
||||
end repeat
|
||||
return true
|
||||
end next_comb
|
||||
|
||||
return comb(5, 3)
|
||||
1
Task/Combinations/AppleScript/combinations-2.applescript
Normal file
1
Task/Combinations/AppleScript/combinations-2.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5}, {1, 4, 5}, {2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5}}
|
||||
110
Task/Combinations/AppleScript/combinations-3.applescript
Normal file
110
Task/Combinations/AppleScript/combinations-3.applescript
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
----------------------- COMBINATIONS ---------------------
|
||||
|
||||
-- comb :: Int -> [a] -> [[a]]
|
||||
on comb(n, lst)
|
||||
if 1 > n then
|
||||
{{}}
|
||||
else
|
||||
if not isNull(lst) then
|
||||
set {h, xs} to uncons(lst)
|
||||
|
||||
map(cons(h), ¬
|
||||
comb(n - 1, xs)) & comb(n, xs)
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end if
|
||||
end comb
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on run
|
||||
|
||||
intercalate(linefeed, ¬
|
||||
map(unwords, comb(3, enumFromTo(0, 4))))
|
||||
|
||||
end run
|
||||
|
||||
-------------------- GENERIC FUNCTIONS -------------------
|
||||
|
||||
-- cons :: a -> [a] -> [a]
|
||||
on cons(x)
|
||||
script
|
||||
on |λ|(xs)
|
||||
{x} & xs
|
||||
end |λ|
|
||||
end script
|
||||
end cons
|
||||
|
||||
-- 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
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
return strJoined
|
||||
end intercalate
|
||||
|
||||
-- isNull :: [a] -> Bool
|
||||
on isNull(xs)
|
||||
if class of xs is string then
|
||||
xs = ""
|
||||
else
|
||||
xs = {}
|
||||
end if
|
||||
end isNull
|
||||
|
||||
-- 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 |λ|(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 |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
set lng to length of xs
|
||||
if lng > 0 then
|
||||
if class of xs is string then
|
||||
set cs to text items of xs
|
||||
{item 1 of cs, rest of cs}
|
||||
else
|
||||
{item 1 of xs, rest of xs}
|
||||
end if
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end uncons
|
||||
|
||||
-- unwords :: [String] -> String
|
||||
on unwords(xs)
|
||||
intercalate(space, xs)
|
||||
end unwords
|
||||
Loading…
Add table
Add a link
Reference in a new issue