Find the missing permutation: Difference between revisions
Content added Content deleted
(Added a new C# implementation) |
(→{{header|AppleScript}}: Taking the 3rd (statistical) approach and composing functional primitives) |
||
Line 141: | Line 141: | ||
Put (Missing_Permutation); |
Put (Missing_Permutation); |
||
end Missing_Permutations;</lang> |
end Missing_Permutations;</lang> |
||
=={{header|AppleScript}}== |
|||
Taking the third (statistical) approach, and composing from functional primitives: |
|||
<lang AppleScript>-- UNDER-REPRESENTED CHARACTER IN EACH OF 4 COLUMNS |
|||
script missingChar |
|||
on lambda(xs) |
|||
script onlyFive |
|||
on lambda(a, x, i) |
|||
if a is missing value then |
|||
if x < 6 then |
|||
character id (64 + i) |
|||
else |
|||
missing value |
|||
end if |
|||
else |
|||
a |
|||
end if |
|||
end lambda |
|||
end script |
|||
foldl(onlyFive, missing value, xs) |
|||
end lambda |
|||
end script |
|||
-- Count of each character type in each character column |
|||
script colCounts |
|||
on lambda(xs) |
|||
script tally |
|||
on lambda(tuple, x) |
|||
set i to ord(x) - 64 |
|||
set item i of tuple to (item i of tuple) + 1 |
|||
tuple |
|||
end lambda |
|||
end script |
|||
foldl(tally, {0, 0, 0, 0}, xs) |
|||
end lambda |
|||
end script |
|||
-- TEST |
|||
on run |
|||
map(missingChar, map(colCounts, ¬ |
|||
transpose(map(curry(splitOn)'s lambda(""), ¬ |
|||
splitOn(space, ¬ |
|||
"ABCD CABD ACDB DACB BCDA ACBD " & ¬ |
|||
"ADCB CDAB DABC BCAD CADB CDBA " & ¬ |
|||
"CBAD ABDC ADBC BDCA DCBA BACD " & ¬ |
|||
"BADC BDAC CBDA DBCA DCAB"))))) as text |
|||
--> "DBAC" |
|||
end run |
|||
-- GENERIC FUNCTIONS |
|||
-- transpose :: [[a]] -> [[a]] |
|||
on transpose(xss) |
|||
script column |
|||
on lambda(_, iCol) |
|||
script row |
|||
on lambda(xs) |
|||
item iCol of xs |
|||
end lambda |
|||
end script |
|||
map(row, xss) |
|||
end lambda |
|||
end script |
|||
map(column, item 1 of xss) |
|||
end transpose |
|||
-- curry :: (Script|Handler) -> Script |
|||
on curry(f) |
|||
set mf to mReturn(f) |
|||
script |
|||
on lambda(a) |
|||
script |
|||
on lambda(B) |
|||
mf's lambda(a, B) |
|||
end lambda |
|||
end script |
|||
end lambda |
|||
end script |
|||
end curry |
|||
-- 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 |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
|||
on foldl(f, startValue, xs) |
|||
tell mReturn(f) |
|||
set v to startValue |
|||
set lng to length of xs |
|||
repeat with i from 1 to lng |
|||
set v to lambda(v, item i of xs, i, xs) |
|||
end repeat |
|||
return v |
|||
end tell |
|||
end foldl |
|||
-- splitOn :: Text -> Text -> [Text] |
|||
on splitOn(strDelim, strMain) |
|||
set {dlm, my text item delimiters} to {my text item delimiters, strDelim} |
|||
set xs to text items of strMain |
|||
set my text item delimiters to dlm |
|||
return xs |
|||
end splitOn |
|||
-- ord :: Character -> Int |
|||
on ord(x) |
|||
id of x |
|||
end ord |
|||
-- 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</lang> |
|||
{{Out}} |
|||
<pre>"DBAC"</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |