1
\$\begingroup\$

Chained Binary Operations

Here's a challenge involving truth tables and binary operations. For this specific challenge, your task is to use the following table of operations:

Binary operation table.

To create a function that takes in two inputs f(p,a), where p is the initial truthy/falsy value and a is a list of tuples representing multiple values of q and the operations to be performed. For instance, f(T,[[T,0],[T,1]]) would return ContradictionOP(T,T)=F. You would then chain the output of this result into the next operation, which would be LogicalNOR(F,T)=F, where the 2nd p is the previous output of F. The final result being false. A better breakdown of what each operation actually is is described below:

Ordinal operator descriptors.

The first part should be rather simplistic in nature, as the operators are arranged in an increasing binary pattern (hint, hint). However, recursing through the additional inputs to chain binary operations is probably the harder part to accomplish.

Examples:

f(T,[])=T
f(F,[[F,15]])=T
f(T,[[F,15],[T,0],[F,6]])=F
f(T,[[F,15],[F,14],[F,13],[F,12],[F,11]])=F

Extrapolated Example:

f(T,[[F,15],[F,14],[F,13],[F,12],[F,11]])=F
[T,[F,15]]=T*
[T*,[F,14]=T*
[T*,[F,13]=T*
[T*,[F,12]=T*
[T*,[F,11]=F
FALSE.

Rules:

  • You MUST use the order specified for the operations as stated above.
  • The input format for a can be a 2D array, array of tuples, command line pairwise arguments or any other representation of pairs of boolean/operations. Could even be a single array that you parse pairwise.
  • You may use 0 for false and 1 for true, it is not acceptable to reverse the two.
  • The output should be simply a truthy or falsy value.
  • No loopholes, this is code-golf, standard CG rules apply.

Example Single Iteration Algorithm:

f={
    p,q,o->
    op=Integer.toBinaryString(o);     // Calculate the binary OP string (15=1111)
    binaryIndex="$p$q"                // Calculate binary combination of p/q (T/F=10)
    i=Integer.parseInt(binaryIndex,2) // Convert to decimal index (T/F=2)
    op.charAt(i)                      // Get the binary char at the calculated index (T/F/15=1).
}
f(1,0,15)                             // "1"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
\$\endgroup\$
  • \$\begingroup\$ Can I accept e.g. f([T,[F,15],[T,0],[F,6]])? \$\endgroup\$ – Neil Oct 28 '16 at 19:01
  • \$\begingroup\$ @Neil, yeah, sure. That's fine. As long as p is distinct then you have pairs of operators. Can also be[true,[false,15],...]. Whatever a boolean is in your language. You do not have to use the character F or T. I was just using that to represent a boolean object. \$\endgroup\$ – Magic Octopus Urn Oct 28 '16 at 19:06
  • \$\begingroup\$ This falls just short of my threshold for voting to close as unclear what you're asking, but only just. Is there any good reason for referring to the same operation as F in the first table, FalsyOp in the first example, and Contradiction in the second table? Using the same name consistently would make the question a whole lot more intelligible. \$\endgroup\$ – Peter Taylor Oct 28 '16 at 20:32
  • \$\begingroup\$ The ordinals are in the second image for each operation. A ContradictionOP (FalsyOP) returns false regardless of inputs. Look at the first table for all PQ, at ordinal 0 it should return false. Just like for ordinal 6 (XOR) it should return 0, 1, 1 or 0 depending on the PQ. Its actually quite simple if you read the provided code snippet and connect a few dots. Each operation's results can be accessed using the binary value of p+q and the binary operation code. \$\endgroup\$ – Magic Octopus Urn Oct 28 '16 at 20:47
  • \$\begingroup\$ @PeterTaylor sorry... Its one of those things seeming complicated, but is quite simple due to a few factors. \$\endgroup\$ – Magic Octopus Urn Oct 28 '16 at 22:29
1
\$\begingroup\$

CJam (15 bytes)

{{+)2@2b#/1&}/}

Online demo. This is an anonymous block (function) which takes the two values on the stack and leaves the result on the stack.

Dissection

{         e# Define a block:
  {       e#   For each pair in the array...
    +)2@  e#     Prepend the accumulator, pop the op, push a 2, and rot 3
          e#     Stack is now: op 2 [accum arg]
    2b#   e#     Find 2**(2*accum + arg)
    /1&   e#     Test the corresponding bit of op, setting it as the new accum
  }/
}
\$\endgroup\$
  • \$\begingroup\$ I like it! Nice job. I should add the rest of the OPs as test cases too. \$\endgroup\$ – Magic Octopus Urn Oct 28 '16 at 20:50
3
\$\begingroup\$

JavaScript (ES6), 34 bytes

a=>a.reduce((l,[r,o])=>o>>l+l+r&1)

Accepts true/false but prefers 1/0 format.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.