<<echo=FALSE, results=hide>>=
## Base64-encoded PNG clipart images (CC0)
## created by base64enc::base64encode("file.png")
banana    <- "[encoded data, 9708 characters]"
orange    <- "[encoded data, 9348 characters]"
pineapple <- "[encoded data, 18108 characters]"
writeBin(base64enc::base64decode(banana),    "banana.png")
writeBin(base64enc::base64decode(orange),    "orange.png")
writeBin(base64enc::base64decode(pineapple), "pineapple.png")
img0 <- sprintf("\\\\includegraphics[width=0.85cm]{%s.png}", c("banana", "orange", "pineapple"))
img <- function(x = "b") img0[factor(x, levels = c("b", "o", "p"))]

sc <- NULL
while(is.null(sc)) {
## prices ("in cent")
b <- sample(20:99, size = 1)
o <- sample(20:99, size = 1)
p <- sample(199:499, size = 1)
x <- c(b, o, p)

## three simple fruit baskets
G <- sample(c("bbo", "bbp", "boo", "bpp", "oop", "opp"), 3)
G <- t(sapply(G, function(x) sample(strsplit(x, "")[[1]])))
A <- t(apply(G, 1, function(x) table(factor(x, levels = c("b", "o", "p")))))

## corresponding prices
d <- A %*% x

## new fruit basket with all fruits and corresponding price
sol <- sum(x)

## single-choice list
err1 <- sample(d, 1)
err2 <- x[3]
sc <- num_to_schoice(sol, wrong = c(err1, err2), delta = 2, digits = 0)
}
@


\begin{question}
Given the following information:
\begin{center}
\begin{tabular}{ccccccr}
\Sexpr{img(G[1, 1])} & $+$ & \Sexpr{img(G[1, 2])} & $+$ & \Sexpr{img(G[1, 3])} & = & $\Sexpr{d[1]}$ \\
\Sexpr{img(G[2, 1])} & $+$ & \Sexpr{img(G[2, 2])} & $+$ & \Sexpr{img(G[2, 3])} & = & $\Sexpr{d[2]}$ \\
\Sexpr{img(G[3, 1])} & $+$ & \Sexpr{img(G[3, 2])} & $+$ & \Sexpr{img(G[3, 3])} & = & $\Sexpr{d[3]}$
\end{tabular}
\end{center}

Compute:
\begin{center}
\begin{tabular}{ccccccr}
\Sexpr{img("b")} & $+$ & \Sexpr{img("o")} & $+$ & \Sexpr{img("p")} & = & $\text{?}$
\end{tabular}
\end{center}

<<echo=FALSE, results=tex>>=
answerlist(sc$questions)
@
\end{question}

\begin{solution}
The information provided can be interpreted as the price for three fruit baskets
with different combinations of the three fruits. This corresponds to a system of
linear equations where the price of the three fruits is the vector of unknowns $x$:
\begin{center}
\begin{tabular}{clclcl}
$x_1 =$ & \Sexpr{img("b")} & $x_2 =$ & \Sexpr{img("o")} & $x_3 =$ & \Sexpr{img("p")}
\end{tabular}
\end{center}
The system of linear equations is then:
\begin{eqnarray*}
\Sexpr{toLatex(A)} \cdot \Sexpr{toLatex(matrix(paste0("x_", 1:3), ncol = 1))} & = & \Sexpr{toLatex(matrix(d, ncol = 1))}
\end{eqnarray*}
This can be solved using any solution algorithm, e.g., elimination:
$$
x_1 = \Sexpr{b}, \, x_2 = \Sexpr{o}, \, x_3 = \Sexpr{p}.
$$
Based on the three prices for the different fruits it is straightforward to
compute the total price of the fourth fruit basket via:
\begin{center}
\begin{tabular}{ccccccr}
\Sexpr{img("b")} & $+$ & \Sexpr{img("o")} & $+$ & \Sexpr{img("p")} & $=$ & \\
$x_1$ & $+$ & $x_2$ & $+$ & $x_3$ & $=$ & \\
$\Sexpr{x[1]}$ & $+$ & $\Sexpr{x[2]}$ & $+$ & $\Sexpr{x[3]}$ & $=$ & $\Sexpr{sol}$.
\end{tabular}
\end{center}
\end{solution}


%% \exname{Fruit baskets (single-choice)}
%% \extype{schoice}
%% \exsolution{\Sexpr{mchoice2string(sc$solutions)}}
%% \extol{0}

