Find the missing permutation: Difference between revisions
→Python:Counting lowest frequency character at each position: functional: Added an XOR variant.
(→JS ES6: Added an XOR fold variant.) |
(→Python:Counting lowest frequency character at each position: functional: Added an XOR variant.) |
||
Line 2,542:
'DBAC'
>>> </lang>
===Python:Folding XOR over the set of strings===
Surfacing the missing bits:
{{Trans|JavaScript}}
<lang Python>'''Find the missing permutation'''
from functools import reduce
# main :: IO ()
def main():
'''Missing bits surfaced by folding XOR
over the set of strings.
'''
xs = [
'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD',
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD',
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'
]
print(''.join([
chr(i) for i in reduce(
lambda a, s: map(xor, a, codes(s)),
xs[1:],
codes(xs[0])
)
]))
# ------------------------ GENERIC -------------------------
# codes :: String -> [Int]
def codes(s):
'''The ordinal code of each character in s.
'''
return [ord(c) for c in list(s)]
# xor :: (Int, Int) -> Int
def xor(a, b):
'''Exclusive OR over the bits of integers a and b.
'''
return a ^ b
# MAIN ---
if __name__ == '__main__':
main()</lang>
{{Out}}
<pre>DBAC</pre>
=={{header|R}}==
|