Find the missing permutation: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m Use correct markup for subheaders. Alternates getting counted as entries. |
→JS ES6: Added an XOR fold variant. |
||
Line 1,630: | Line 1,630: | ||
// -> "DBAC" |
// -> "DBAC" |
||
})();</lang> |
|||
{{Out}} |
|||
<pre>DBAC</pre> |
|||
====XOR==== |
|||
Folding an xor operator over the list of character codes: |
|||
<lang javaScript>(() => { |
|||
'use strict'; |
|||
// main :: IO () |
|||
const main = () => { |
|||
const xs = [ |
|||
'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', |
|||
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA', |
|||
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD', |
|||
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB' |
|||
]; |
|||
return xs.slice(1).reduce( |
|||
(a, x) => zipWith(xor)(a)(codes(x)), |
|||
codes(xs[0]) |
|||
).map(x => String.fromCodePoint(x)).join('') |
|||
}; |
|||
// ---------------------- GENERIC ---------------------- |
|||
// codes :: String -> [Int] |
|||
const codes = s => |
|||
s.split('').map(c => c.codePointAt(0)); |
|||
// xor :: Int -> Int -> Int |
|||
const xor = a => |
|||
b => (a ^ b) |
|||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
|||
const zipWith = f => |
|||
// A list constructed by zipping with a |
|||
// custom function, rather than with the |
|||
// default tuple constructor. |
|||
xs => ys => xs.slice(0).map( |
|||
(x, i) => f(x)(ys[i]) |
|||
); |
|||
return main() |
|||
})();</lang> |
})();</lang> |
||
{{Out}} |
{{Out}} |