Preparing a multiple choice test

Introduction

Note: this is not some kind of method to encourage cheating. As Cᴏɴᴏʀ O'Bʀɪᴇɴ already said, studying is the best solution for passing a test :3.

Consider the following answers to the multiple choice test:

ABCBCAAB


Here is a table which indicates whether the answers match:

    A B C B C A A B

A   1 0 0 0 0 1 1 0
B   0 1 0 1 0 0 0 1
C   0 0 1 0 1 0 0 0


This gives us the following numbers:

10000110, 01010001, 00101000


The challenge is to print these binary numbers. But it is important to see which letters are used in the multiple choice test. For example:

ABCDCDBCDABC


This highest letter is D, which is the 4th letter in the alphabet. Therefore, we need to output 4 different binary numbers. Namely:

100000000100, 010000100010, 001010010001, 000101001000


Note that you have to look at the highest letter. Consider the following example:

AACCACAC


Although the B is not used, we need to output the binary result for B. That means, the answer would be:

11001010, 00000000, 00110101


Given the answers to a multiple choice test, output the binary numbers. You may assume that the input will be non-empty and only containing the letters [A-Z] . Instead of ones and zeroes, you may also use true and false.

Test cases:

Input: ABBBABABA
Output: 100010101, 011101010

Input: AAAAAAAAA
Output: 111111111

Input: AFGHEEHFD
Output: 100000000 000000000 000000000 000000001 000011000 010000010 001000000 000100100

Input: Z
Output: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1

Input: ABCDCDBCDABC
Output: 100000000100, 010000100010, 001010010001, 000101001000


This is , so the submission with the least amount of bytes wins!

• Can we use [a-z] instead? – FryAmTheEggman Feb 9 '16 at 15:05
• @FryAmTheEggman Of course :) – Adnan Feb 9 '16 at 15:06
• Related – Adnan Feb 9 '16 at 15:06
• there are no rules for the output specified, is a 2D array of bools allowed? – Eumel Feb 9 '16 at 15:30
• that seems kinda unreasonable but works for me as well^^ – Eumel Feb 9 '16 at 15:40

Pyth, 12 bytes

mqLdzfsgRTzG


Outputs as a nested array of booleans.

                Implicit: z=input
m               Map lambda d:
qLdz            map equal-to-d over z
f           over all letters T in the
G     lowercase alphabet for which
s           At least one char in z
gRTz       >= T.


Try it here.

Python 3, 71

Saved 22 bytes thanks to Ogaday.
Saved 3 bytes thanks to DSM.

Saved a bunch of bytes thanks to an array of bools being valid.

*k,=map(ord,input())
x=65
while x<=max(k):print([v==x for v in k]);x+=1


Takes uppercase command line input.

• Use extended iterable unpacking: *k,=map(ord,input()) – Ogaday Feb 9 '16 at 15:48
• An array of bools is now also possible, if that helps. – Adnan Feb 9 '16 at 16:57

PowerShell, 9594 73 bytes

param([char[]]$a)0..(($a|sort)[-1]-65)|%{$d=$_;-join($a|%{+!($_-$d-65)})}  Takes input as an uppercase string, but immediately casts it [char[]]. We then loop from 0.. to the maximal value of $a taken alphabetically (hence the -65 to convert from ASCII). For example, with ADCEB, this can be thought of as looping from A to E.

Each iteration, we set a helper variable $d equal to the current alphabetical (not ASCII) value. We then loop through all of $a, each time putting either 0 or 1 on the pipeline, based on whether $_-$d-65 is truthy or falsey (i.e., whether we're in the right "slot"). This works because any non-zero value in PowerShell is truthy, meaning if our current letter $_ doesn't "equal" what slot we're in $d, then the ! of that is $false, or 0. Each of those arrays of 0s and 1s is then -joined together and re-put on the pipeline. When the outer loop ends, we have an array of strings, which will print one string per line. Examples PS C:\Tools\Scripts\golfing> .\preparing-a-multiple-choice-test.ps1 ABCDEFGH 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001 PS C:\Tools\Scripts\golfing> .\preparing-a-multiple-choice-test.ps1 AFGHEEHFD 100000000 000000000 000000000 000000001 000011000 010000010 001000000 000100100  Edit 1 -- saved a byte by using Boolean-not instead of -eq Edit 2 -- saved another 21 bytes by eliminating the extra array$b

• An array of bools is now also possible, if that helps. – Adnan Feb 9 '16 at 16:58

LabVIEW, 3022 20 LabVIEW Primitives

Goes through from a-z until the sum of all bools is equal to the input lenght. then Transforms the bools to numbers.

Now directly takes the max instead of checking the bool sum.

Since 2D bools are viable now im saving 2 Primitives by outputting the green wire in front of ?1:0 could redo it but im too lazy...

• An array of bools is now also possible, if that helps. – Adnan Feb 9 '16 at 16:58

Cjam, 25 bytes

l{'A-i}%_:e>),\f{f=", "}


Sigh,

Explanation

l{'A-i}%_:e>),\f{f=", "}
l                        e# get the input
{'A-i}%                 e# get the normalized array
_:e>             e# get the maximum value
),           e# create the array from 1..N
\f{      } e# map over the created array
f=      e# 1 if match, 0 if not

• Congrats on 1k rep BTW! – Blue Feb 9 '16 at 15:47
• An array of bools is now also possible, if that helps. – Adnan Feb 9 '16 at 16:57

g x=(<$>x).(==)<$>['A'..maximum x]


Usage example: g "ACDC"-> [[True,False,False,False],[False,False,False,False],[False,True,False,True],[False,False,True,False]].

How it works:

        <$>['A'..maximum x] -- for every letter from A to the highest letter in x <$>x                          -- loop over x and
==                      -- compare the letter with the current element in x
-- which results in a boolean


Pyth, 20191715 14 bytes

VhxGeSzmq@GNdz


Explanation:

               - autoassign z = input()
V              - For N in range(V)
hxGeSz
eSz        - max(z)
xG           - lowercase_alphabet.index(^)
h             - +1
mq@GNdz
m     z - [V for d in z]
@GN   - lowercase_alphabet[N]
q   d  - is_equal(^, ^^)
- print "".join(^)


Outputs a 2D array of bools

Try it here

ES6, 92 bytes

s=>[..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"].map(c=>[...s].map(m=>m==c&&!!(l=n),n++),n=0).slice(0,l)


Returns an array of arrays of falses and trues. If you prefer an array of strings of zeros and ones, then for 97 bytes:

s=>[..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"].map(c=>s.replace(/./g,m=>m==c?(l=n,1):0,n++),n=0).slice(0,l)


Octave, 19 bytes

@(s)s==[65:max(s)]'


Uses Octave's automatic broadcasting over the range A to the max element in the input to produce 2d boolean array of matching elements.

Example:

Key = ABCDCDBCDABC

ans =

1   0   0   0   0   0   0   0   0   1   0   0
0   1   0   0   0   0   1   0   0   0   1   0
0   0   1   0   1   0   0   1   0   0   0   1
0   0   0   1   0   1   0   0   1   0   0   0


Try it here on ideone.

Lua, 208 189 Bytes

That was tricky in lua, as usual, we have to do everything from scratch, and it takes a lot of place ! This program takes a string as argument, and print out the result :).

Edit: @Adnan told me we're now allowed to return a matrix of booleans, so here's a new version! It is now a function that takes a string of capitalised character and return the matrix :).

function f(x)a={}b={}for i=1,#x do a[i]=x:sub(i,i)end table.sort(a)for i=1,a[#a]:byte()-64 do b[i]={}end x:gsub(".",function(c)for i=1,#b do z=b[i]z[#z+1]=i+64==c:byte()end end)return b end


Old 208 Byte version

This is the one that works with argument and print results.

a={}b={}x=arg[1]for i=1,#x do a[i]=x:sub(i,i)end table.sort(a)for i=1,a[#a]:byte()-64 do b[i]=""end x:gsub(".",function(c)for i=1,#b do b[i]=b[i]..(i+64==c:byte()and"1"or"0")end end)print(table.concat(b,","))


Ungolfed and explanations

function f(x)
a={}                   -- We need 2 arrays, and while b=a would have been shorter
b={}                   -- arrays in lua are passed by pointer, so it wouldn't have worked

for i=1,#x             -- iterate over the inpute to transform the string
do                     -- into an array
a[i]=x:sub(i,i)      -- with each cells containing a characyer
end
table.sort(a)          -- sort the array/string

for i=1,               -- iterate n times were n is the place in the alphabet
a[#a]:byte()-64 -- of the last element of the (sorted) array a
do
b[i]={}              -- initialise cells in b up to n with empty arrays
end                    -- a cell's index is the position of a letter in the alphabet

x:gsub(".",function(c) -- for each character in the input
for i=1,#b           -- iterate over each cells in b
do
z=b[i]             -- define z pointing on the array b[i]
z[#z+1]=           -- insert into b[i] the result of the
i+64==c:byte()  -- comparison of the current char, and the answer
end
end)
return b
end


Trying to print an array in Lua would result in printing its address and concatenating bools is impossible. So here's a function that will help you if you want to test this submission

function f(x)a={}b={}for i=1,#x do a[i]=x:sub(i,i)end table.sort(a)for i=1,a[#a]:byte()-64 do b[i]={}end x:gsub(".",function(c)for i=1,#b do z=b[i]z[#z+1]=i+64==c:byte()end end)return b end

function printBooleanMatrix(m)
s="{"
for i=1,#m
do
s=s.."{"
for j=1,#m[i]
do
s=s..(m[i][j]and"1"or"0")..(j==#m[i]and""or",")
end
s=s.."}"..(i==#m and""or",\n")
end
print(s.."}")
end

printBooleanMatrix(f("ABBCCDDC"))

• An array of bools is now also possible, if that helps. – Adnan Feb 9 '16 at 16:58
• @Adnan well, it allows me to drop a lot of bytes. I'm writing a function to have a clear output for testing purpose, then post a revised version :) – Katenkyo Feb 10 '16 at 7:27

Perl, 84 bytes

$\="\n";@i=split//,<>;pop@i;for$c('a'..(reverse sort@i)[0]){print map{/$c/?1:0}@i;}  Oh dear, I seem to have broken the highlighter. Ungolfed version: # output formatting$\ = "\n";
# get input chars as array
@i = split //, <>;
# lose the newline
pop @i;
# iterate over characters up to the max
for $c ('a'..(reverse sort @i)[0]) { # print 1 for each match, 0 otherwise print map { /$c/ ? 1 : 0 } @i;
}


PHP, 1069290 87 bytes

Uses Windows-1252 encoding.

for($x=A;$x++<=max($z=str_split($argv[1]));print~Ó)for(;$c=$z[+$$x++];)echo+(++c==x);  Run like this (-d added for aesthetics only): php -d error_reporting=30709 -r 'for(x=A;x++<=max(z=str_split(argv[1]));print~Ó)for(;c=z[+$$x++];)echo+(++$c==$x); echo"\n";' ABCDDHFHUYFSJGK

• Saved 14 bytes by nesting the loops the other way around
• Saved 2 bytes by using variable variables to prevent $i=0 • Saved a byte by inverting string and dropping string delimiters • Saved a byte by moving the echo (changed to a print to fit) inside the first for loop and dropping curly braces • Saved a byte by incrementing $x somewhere else, and incrementing \$c to compensate

C#, 68 bytes

c=>Enumerable.Range(65,c.Max()-64).Select(x=>c.Select(y=>x==y?1:0));


This anonymous function takes a char[] as input and outputs an IEnumerable<IEnumerable<int>>, with only 0's and 1's.
{x=/:65_!|/1+x}