Find the missing permutation: Difference between revisions

m
Line 213:
<lang AppleScript>use framework "Foundation" -- ( sort )
 
-- RAREST LETTER IN EACH COLUMN --------------------------------- RAREST LETTER IN EACH COLUMN -------------
on run
concat(map(composeList({¬
intercalate("", ¬
map(composeAll({head, ¬
headminimumBy(comparing(|length|)), ¬
returngroup, v¬
curry(minimumBy)'s |λ|(comparing(|length|)), ¬
groupsort}), ¬
sort})transpose(map(chars, ¬
transpose|words|(map(chars,"ABCD CABD ACDB DACB BCDA ACBD " & ¬
|words|("ABCDADCB CABDCDAB ACDBDABC DACBBCAD BCDACADB ACBDCDBA " & ¬
"ADCBCBAD CDABABDC DABCADBC BCADBDCA CADBDCBA CDBABACD " & ¬
"CBADBADC ABDCBDAC ADBCCBDA BDCA DCBA BACDDBCA DCAB" & ¬)))))
"BADC BDAC CBDA DBCA DCAB")))))
--> "DBAC"
end run
 
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
-------------------- GENERIC FUNCTIONS -------------------
 
-- chars :: String -> [String]
Line 236:
characters of s
end chars
 
 
-- Ordering :: (-1 | 0 | 1)
Line 248 ⟶ 249:
end if
end compare
 
 
-- comparing :: (a -> b) -> (a -> a -> Ordering)
Line 258 ⟶ 260:
end comparing
 
 
-- composeAllcomposeList :: [(a -> a)] -> (a -> a)
on composeAll(fs)
on composeList(fs)
script
on |λ|(x)
script go
on |λ|(f, a)
mReturn(f)'s |λ|(a)
end |λ|
end script
foldr(go, x, fs)
foldr(result, x, fs)
end |λ|
end script
end composeAllcomposeList
 
 
-- concat :: [[a]] -> [a]
-- concat :: [String] -> String
on concat(xs)
set lng to length of xs
if 0 < lng and string is class of (item 1 of xs) then
onset |λ|(a)acc to ""
else
endset |λ|acc to {}
end scriptif
repeat with i from 1 to lng
set acc to acc & item i of xs
end repeat
acc
end concat
 
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on |λ|(a)
script
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end |λ|
end script
end curry
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 297 ⟶ 303:
end tell
end foldl
 
 
-- foldr :: (b -> a -> a) -> a -> [b] -> a
Line 309 ⟶ 316:
end tell
end foldr
 
 
-- group :: Eq a => [a] -> [[a]]
Line 320 ⟶ 328:
groupBy(eq, xs)
end group
 
 
-- groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
Line 352 ⟶ 361:
end if
end groupBy
 
 
-- head :: [a] -> a
Line 361 ⟶ 371:
end if
end head
 
 
-- intercalate :: Text -> [Text] -> Text
Line 369 ⟶ 380:
return strJoined
end intercalate
 
 
-- length :: [a] -> Int
Line 374 ⟶ 386:
length of xs
end |length|
 
 
-- map :: (a -> b) -> [a] -> [b]
Line 386 ⟶ 399:
end tell
end map
 
 
-- minimumBy :: (a -> a -> Ordering) -> [a] -> a
on minimumBy(f, xs)
script
if length of xs < 1 then return missing value
tell mReturn on |λ|(fxs)
set v to item 1if length of xs < 1 then return missing value
repeat with x in xstell mReturn(f)
if |λ|(x, v) < 0 then set v to xitem 1 of xs
end repeat with x in xs
if |λ|(ax, bv) of< mReturn(f)0 then set v to x
return v
end tellrepeat
onreturn |λ|(b)v
foldr(result,end x, fs)tell
end |λ|
end script
end minimumBy
 
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 410 ⟶ 429:
end if
end mReturn
 
 
-- sort :: [a] -> [a]
Line 416 ⟶ 436:
sortedArrayUsingSelector:"compare:") as list
end sort
 
 
-- tail :: [a] -> [a]
Line 425 ⟶ 446:
end if
end tail
 
 
-- transpose :: [[a]] -> [[a]]
Line 442 ⟶ 464:
map(column, item 1 of xss)
end transpose
 
 
-- words :: String -> [String]
9,655

edits