Cheating a multiple choice test
Jelly, 7 6 bytes
_/ị“ḃ»
Typing on phone. Will add description.
(1,0)
goes to A
, (0,1)
to B
, and (0,0)
to C
. Arrays in Jelly are 1-based, and the indexing function ị
works cyclically. Therefore, we can just fold subtraction over the input.
_ [vectorized] subtraction
_/ Fold subtraction over the input
“ḃ» "ABC" compressed. '»' terminates a compressed string.
ị Vectorized cyclic index.
Try it here.
Retina, 44 bytes
T`d`BA
B(?=(.)* .*B(?<-1>.)*(?(1)!)$)
C
.+
The trailing linefeed is significant. Input is like
001101010 100010100
Try it online!
Explanation
T`d`BA
Start by turning 0
s into B
and 1
s into A
. That makes the first half correct, except that it lists B
when it should contain C
. We can identify those erroneous B
s by checking whether there's a B
in the same position of the second string:
B(?=(.)* .*B(?<-1>.)*(?(1)!)$)
C
The lookahead is a classic balancing group counting technique to match up the positions of the two B
s. The (.)*
counts the suffix after the first B
by pushing one capture onto group 1
for each character. Then (?<-1>.)*
pops from that group again. The $
ensures that we can reach the end of the string like that, and the (?(1)!)
ensures that we've actually depleted the entire group.
Finally, we get rid of the separating space and the second string:
.+
MATL, 14 9 bytes
2*+ 67w-c
Uses current version (10.1.0)
Try it online!
Explanation
Summary of what the code does
2* % implicitly input array and multiply it by 2
+ % implicitly input array and add it to the first one
67w- % subtract from 67
c % convert to char. Implicitly display
Detailed explanation of how it works
2 % Push number 2 onto the stack
* % Multiply the top two stack elements. Since there's only one
% element, this triggers implicit input of a (yet inexistent)
% element below the existing one. This is the first input array,
% which will be called "A". Both "A" and number 2 are consumed,
% and the array 2*A is left on the stack.
+ % Add the top two stack elements. Again, since there's only
% one element (namely array 2*A) this triggers implicit input
% of the second array, call it "B". Both 2*A and B are consumed
% and 2*A+B is left on the stack
% A blank space is needed after the "+" symbol to prevent it from
% being interpreted as part of number "+67"
67 % Push number 67 onto the stack. The stack now contains, bottom
% to top, 2*A+B and 67.
w % Swap top two elements. The stack now contains 67 and 2*A+B
- % Subtract top two elements. These are consumed and the result
% 67-A*B is left on the stack
c % Convert to char array, interpreting each number as ASCII code.
% Number 67 corresponds to letter 'C'. Therefore at positions
% where both arrays A and B were 0 this gives 'C' as result.
% Where A was 1 and B was 0, i.e. 2*A+B is 2, this gives 'A'.
% Where A was 0 and B was 1, i.e. 2*A+B is 1, this gives 'B'.
% The stack contents, namely this char array, are implicitly
% displayed at the end of the program.