# To Apply Answers to MCQs

I am now extending my own answers to MCQs, as made How to SED these paragraphs to MCQ format?.

Data in file.txt

1   c
2   a


Data in exam.tex

\item
Which of the following lorem ipsun are the best playstation games you have played?

A Pattman

B Pokemon

C Lorem

D Ipsun

E Heillui

\item
Which of the following lorem ipsun are the best playstation games you have played?

A Pattman

B Pokemon

C Lorem

D Ipsun

E Heillui


\item
Which of the following lorem ipsun are the best playstation games you have played?

A Pattman

B Pokemon

\textbf{C Lorem}

D Ipsun

E Heillui

\item
Which of the following lorem ipsun are the best playstation games you have played?

\textbf{A Pattman}

B Pokemon

C Lorem

D Ipsun

E Heillui


## Pseudocode

• for loop \item's in Perl paragraph mode
• for loop empty lines in each paragaph by the count in the list (a=1, b=2, c=3, d=4, e=5)
• apply \textbf to the beginning of the line; and } to the end of the line
• step out of the current paragraph
• end

Pseudocode with Python-Perl

for item in items

perl -00pe 's/\\item\n.*\n{$item}^/\textbf{/;' file perl -00pe 's/\\item\n.*\n{$item}\$/}/;' file

end


where I do not like that I handle the beginning and the end in separate commands.

How can you apply answer to MCQs by Perl/SED/Python?

{   printf '[13*%d-n[bs]pc]s%c\n' 9 a 7 b 5 c 3 d 1 e
tr  -s ' \n' lx <file.txt; }  |  dc |
sed -f- -eb -e:s -e's/.*/\\textbf{&}/' exam.txt


That uses the dc reverse-polish-notation calculator to generate a sed script that looks like:

8bs
17bs


...which is then concatenated with the sed scripts entered on the command-line and so results in every line number which is not included in the generated script to be branched away, and for the those that are included to have the string \textbf{ inserted before whatever exists on the line and } appended to its tail.

It will work for more than just the two sections, of course. Basically it multiplies the leading number on each line in file.txt by 13 and then subtracts from the product 1, 3, 5, 7, 9 for either of e, d, c, b, a respectively to arrive at the list of targeted line numbers.

In any case, all sed has to do at the end is the default print it does for every line which it doesn't edit before branching away, or else to do the one substitution for your string. tr and dc are both very fast utilities, and they handle nearly all of the pre-processing.

• No comparisons need be made between the two files.

• No regular expression matching is required at all.

• While true, sed's processing might benefit slightly from an initial additional filter to prevent it attempting to match every line number against those in the list, like:

... | sed -e'/^[ABCDE] /!b' -f- ...

• The answers in file.txt need not be in any particular order or even completely represent every question in exam.txt.

• Each answer letter in file.txt must be lowercase.

• use tr -s '[:upper:] \n' \[:lower:]lx to handle either/or.
• At least one intermediate space and intervening newline is required between the answer number and the answer letter and between each answer respectively in file.txt, though any larger number is permitted with the exception that no leading blank-lines can be found before the first answer, and no line can begin or end with spaces.

• Every question/answer block in exam.txt must be comprised of exactly 13 lines (excepting the last, which need not include a trailing blank-line).

This depends on one or two GNU dc extensions as written. Here is a more portable version:

{   printf '[13*%d-p[s]pc]s%c\n' 9 a 7 b 5 c 3 d 1 e
tr  -s ' \n' lx <file.txt;}| dc | paste -db - -|
sed -f- -eb -e:s -e's/.*/\\textbf{&}/' exam.txt


...either way you write it, though:

\item
Which of the following lorem ipsun are the best playstation games you have played?

A Pattman

B Pokemon

\textbf{C Lorem}

D Ipsun

E Heillui

\item
Which of the following lorem ipsun are the best playstation games you have played?

\textbf{A Pattman}

B Pokemon

C Lorem

D Ipsun

E Heillui