Find the missing permutation: Difference between revisions

Content added Content deleted
m (→‎Python:Folding XOR over the set of strings: (:Pruned out some scaffolding))
Line 2,591: Line 2,591:


from functools import reduce
from functools import reduce
from operator import xor




print(''.join([
# main :: IO ()
chr(i) for i in reduce(
def main():
lambda a, s: map(
'''Missing bits surfaced by folding XOR
over the set of strings.
xor, a,
[ord(c) for c in list(s)]
'''
), [

'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD',
xs = [
'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD',
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD',
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD',
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'
],
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'
]
[0, 0, 0, 0]
print(''.join([
)
]))</lang>
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}}
{{Out}}
<pre>DBAC</pre>
<pre>DBAC</pre>