Debian Codegraph @

texmf-dist/source/latex/probsoln/probsoln.dtx

raw
1 %\iffalse
2 % probsoln.dtx generated using makedtx version 1.1 (c) Nicola Talbot
3 % Command line args:
4 % -doc "probsoln-manual.tex"
5 % -src "(.+)\.(sty)\Z=>\1.\2"
6 % -src "(sample.*)\.(tex)\Z=>\1.\2"
7 % -src "(prob-.*)\.(tex)\Z=>\1.\2"
8 % -comment ".+\.tex"
9 % -macrocode ".+\.tex"
10 % -author "Nicola Talbot"
11 % probsoln
12 % Created on 2017/7/10 11:58
13 %\fi
14 %\iffalse
15 %<*package>
16 %% \CharacterTable
17 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
18 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
19 %% Digits \0\1\2\3\4\5\6\7\8\9
20 %% Exclamation \! Double quote \" Hash (number) \#
21 %% Dollar \$ Percent \% Ampersand \&
22 %% Acute accent \' Left paren \( Right paren \)
23 %% Asterisk \* Plus \+ Comma \,
24 %% Minus \- Point \. Solidus \/
25 %% Colon \: Semicolon \; Less than \<
26 %% Equals \= Greater than \> Question mark \?
27 %% Commercial at \@ Left bracket \[ Backslash \\
28 %% Right bracket \] Circumflex \^ Underscore \_
29 %% Grave accent \` Left brace \{ Vertical bar \|
30 %% Right brace \} Tilde \~}
31 %</package>
32 %\fi
33 % \iffalse
34 % Doc-Source file to use with LaTeX2e
35 % Copyright (C) 2017 Nicola Talbot, all rights reserved.
36 % \fi
37 % \iffalse
38 %<*driver>
39 \documentclass[a4paper]{nlctdoc}
40
41 \usepackage[utf8]{inputenc}
42 \usepackage[T1]{fontenc}
43 \usepackage{lmodern}
44 \usepackage{color}
45
46 \usepackage{probsoln}
47
48 \usepackage[colorlinks,
49 bookmarks,
50 hyperindex=false,
51 pdfauthor={Nicola L.C. Talbot},
52 pdftitle={probsoln: creating problem sheets optionally with solutions}]{hyperref}
53 \doxitem{Option}{option}{package options}
54
55 \RecordChanges
56 \PageIndex
57 \CheckSum{1830}
58
59 \newcommand*{\dq}[1]{``#1''}
60
61 \begin{document}
62 \DocInput{probsoln.dtx}
63 \end{document}
64 %</driver>
65 %\fi
66 %\MakeShortVerb{"}
67 %\DeleteShortVerb{\|}
68 %
69 % \title{probsoln v3.05:
70 %creating problem sheets optionally with solutions}
71 % \author{Nicola L.C. Talbot\\
72 %\url{http://www.dickimaw-books.com/}}
73 %
74 % \date{2017-07-10}
75 % \maketitle
76 %\tableofcontents
77 %
78 % \section{Introduction}
79 %The \styfmt{probsoln} package is designed for teachers or lecturers
80 %who want to create problem sheets for their students. This package
81 %was designed with mathematics problems in mind, but can be used for
82 %other subjects as well. The idea is to create a file containing a
83 %large number of problems with their solutions which can be read in
84 %by \LaTeX, and then select a number of problems to typeset. This
85 %means that once the database has been set up, each year you can
86 %easily create a new problem sheet that is sufficiently different
87 %from the previous year, thus preventing the temptation of current
88 %students seeking out the previous year's students, and checking out
89 %their answers. There is also an option that can be passed to the
90 %package to determine whether or not the solutions should be printed.
91 %In this way, one file can either produce the student's version or
92 %the teacher's version.
93 %
94 %You may want to consider using \sty{datatool} with \app{datatooltk}
95 %instead of \styfmt{probsoln}. See
96 %\href{http://www.dickimaw-books.com/latex/admin/html/datatoolexam.shtml}{Using
97 %the \styfmt{datatool} Package for Exams or Assignment Sheets}.
98 %
99 %\section{Package Options}\label{sec:pkgopt}
100 %The following options may be passed to this package:
101 %\begin{description}
102 %\item[\pkgopt{answers}] Show the answers
103 %\item[\pkgopt{noanswers}] Don't show the answers (default)
104 %\item[\pkgopt{draft}] Display the label and dataset name when a problem is used
105 %\item[\pkgopt{final}] Don't display label and dataset name when a problem is used
106 %\item[\pkgopt{usedefaultargs}] Make \ics{thisproblem} use the
107 %default arguments supplied in the problem definition.
108 %\item[\pkgopt{nousedefaultargs}] Make \ics{thisproblem} prompt for
109 %problem arguments (default).
110 %\end{description}
111 %
112 %\section{Verbatim}\label{sec:verbatim}
113 %
114 %As from version 3.02, problems and solutions may contain verbatim
115 %text, but you must use the \iterm{fragile}\texttt{fragile} (or
116 %\texttt{fragile=true}) option for the associated environments.
117 %
118 %Alternatively, if most of your problems contain verbatim, you can
119 %globally set this option using:
120 %\begin{verbatim}
121 %\setkeys{probsoln}{fragile}
122 %\end{verbatim}
123 %You can switch off this option using \texttt{fragile=false}.
124 %
125 %The \texttt{fragile} option writes information to a temporary file.
126 %This defaults to "\jobname.vrb" but the name may be changed. The
127 %extension (".vrb") is given by:
128 %\begin{definition}[\DescribeMacro{\ProbSolnFragileExt}]
129 %\cs{ProbSolnFragileExt}
130 %\end{definition}
131 %The base name (\cs{jobname}) is given by:
132 %\begin{definition}[\DescribeMacro{\ProbSolnFragileFile}]
133 %\cs{ProbSolnFragileFile}
134 %\end{definition}
135 %
136 %\section{Showing and Hiding Solutions}\label{sec:showanswers}
137 %
138 %In addition to the \pkgopt{answers} and \pkgopt{noanswers} package
139 %options, it is also possible to show or suppress the solutions
140 %using
141 %\begin{definition}[\DescribeMacro{\showanswers}]
142 %\cs{showanswers}
143 %\end{definition}
144 %and
145 %\begin{definition}[\DescribeMacro{\hideanswers}]
146 %\cs{hideanswers}
147 %\end{definition}
148 %respectively.
149 %
150 %The boolean variable \bool{showanswers} determines whether the
151 %answers should be displayed. You can use this value with the
152 %\sty{ifthen} package to specify different text depending on
153 %whether the solutions should be displayed. For example:
154 %\begin{verbatim}
155 %Assignment 1\ifthenelse{\boolean{showanswers}}{ (Solution Sheet)}{}
156 %\end{verbatim}
157 %Alternatively you can use \ics{ifshowanswers}\ldots\cs{else}\ldots
158 %\cs{fi}:
159 %\begin{verbatim}
160 %Assignment 1\ifshowanswers\space (Solution Sheet)\fi
161 %\end{verbatim}
162 %
163 %For longer passages, you can use the environments
164 %\begin{definition}[\DescribeEnv{onlyproblem}]
165 %\cs{begin}\marg{onlyproblem}\oarg{option}
166 %\end{definition}
167 %and
168 %\begin{definition}[\DescribeEnv{onlysolution}]
169 %\cs{begin}\marg{onlysolution}\oarg{option}
170 %\end{definition}
171 %For example:
172 %\begin{verbatim}
173 %\begin{onlyproblem}%
174 %What is the derivative of $f(x) = x^2$?
175 %\end{onlyproblem}%
176 %\begin{onlysolution}%
177 %$f'(x) = 2x$
178 %\end{onlysolution}
179 %\end{verbatim}
180 %The above will only display the question if \bool{showanswers}
181 %is false and will only display the solution if \bool{showanswers}
182 %is true. If you want the question to appear in the answer
183 %sheet as well as the solution, then don't put the question in
184 %the \env{onlyproblem} environment:
185 %\begin{verbatim}
186 %What is the derivative of $f(x) = x^2$?
187 %\begin{onlysolution}%
188 %Solution: $f'(x) = 2x$
189 %\end{onlysolution}
190 %\end{verbatim}
191 %
192 %\begin{important}
193 %If you want to include verbatim text in the body of
194 %\env{onlyproblem} or \env{onlysolution}, you need to specify
195 %\texttt{fragile} in the optional argument of the environment.
196 %(See \sectionref{sec:verbatim} for further details.)
197 %\end{important}
198 %
199 %If you use \envfmt{onlysolution} within the \env{defproblem}
200 %environment, the problem will be tagged as having a solution
201 %and will be added to the list used by \ics{foreachsolution}.
202 %The optional argument of \envfmt{onlysolution} (and \env{onlyproblem})
203 %is inherited from the parent \env{defproblem} setting.
204 %
205 %\section{General Formatting Commands}\label{sec:formatting}
206 %
207 %The commands and environments described in this section are
208 %provided to assist formatting problems and their solutions.
209 %\begin{definition}[\DescribeEnv{solution}]
210 %\verb|\begin{solution}|\meta{text}\verb|\end{solution}|
211 %\end{definition}
212 %By default, this is equivalent to
213 %\begin{display}
214 %\verb|\par\noindent\textbf{\solutionname}: |\meta{text}
215 %\end{display}
216 %where \DescribeMacro{\solutionname}\cs{solutionname} defaults
217 %to \dq{\solutionname}. Note that you must place the \env{solution}
218 %environment inside the \envfmt{onlysolution} environment or
219 %between \ics{ifshowanswers}\ldots\cs{fi} to ensure that it
220 %is suppressed when the solutions are not wanted. (See
221 %\sectionref{sec:showanswers}.)
222 %
223 %
224 %Note that the \styfmt{probsoln} package will only define the
225 %\env{solution} environment if it is not already defined.
226 %
227 %\begin{definition}[\DescribeEnv{textenum}]
228 %\verb|\begin{textenum}|\ldots\verb|\end{textenum}|
229 %\end{definition}
230 %The \envfmt{textenum} environment is like the \env{enumerate}
231 %environment but is in-line. It uses the same counter that the
232 %\envfmt{enumerate} environment would use at that level so the
233 %question can be compact but the answer can use \envfmt{enumerate}
234 %instead. For example:
235 %\begin{verbatim}
236 %\begin{onlyproblem}%
237 % Differentiate the following:
238 % \begin{textenum}
239 % \item $f(x)=2^x$; \item $f(x)=\cot(x)$
240 % \end{textenum}
241 %\end{onlyproblem}
242 %\begin{onlysolution}
243 % \begin{enumerate}
244 % \item
245 % \begin{align*}
246 % f(x) &= 2^x = \exp(\ln(x^2)) =\exp(2\ln(x))\\
247 % f'(x) &= \exp(2\ln(x))\times \frac{2}{x}\\
248 % &= f(x)\frac{2}{x}
249 % \end{align*}
250 % \item
251 % \begin{align*}
252 % f(x) &= \cot(x) = (\tan(x))^{-2}\\
253 % f'(x) &= -(\tan(x))^{-2}\times\sec^2(x)\\
254 % &=-\csc^2x
255 % \end{align*}
256 % \end{enumerate}
257 %\end{onlysolution}
258 %\end{verbatim}
259 %In this example, the items in the question are brief, so an
260 %\env{enumerate} environment would result in a lot of unnecessary
261 %white space, but the answers require more space, so an
262 %\envfmt{enumerate} environment is more appropriate. Since the
263 %\envfmt{textenum} environment uses the same counters as the
264 %\envfmt{enumerate} environment, the question and answer sheets use
265 %consistent labelling. Note that there are other packages available
266 %on CTAN that you can use to create in-line lists. Check the
267 %\urlfootref{http://www.tex.ac.uk/tex-archive/help/Catalogue/bytopic.html\#enumeration}{TeX
268 %Catalogue} for further details.
269 %
270 %\DescribeMacro{\correctitem}\DescribeMacro{\incorrectitem}
271 %\begin{definition}
272 %\cs{correctitem}\\
273 %\cs{incorrectitem}
274 %\end{definition}
275 %You can use the commands \cs{correctitem} and \cs{incorrectitem}
276 %in place of \ics{item}. If the solutions are suppressed, these
277 %commands behave in the same way as \cs{item}, otherwise they
278 %format the item label using one of the commands:
279 %\DescribeMacro{\correctitemformat}\DescribeMacro{\incorrectitemformat}
280 %\begin{definition}
281 %\cs{correctitemformat}\marg{label}\\
282 %\cs{incorrectitemformat}\marg{label}
283 %\end{definition}
284 %For example:
285 %\begin{verbatim}
286 %Under which of the following functions does $S=\{a_1,a_2\}$
287 %become a probability space?
288 %\begin{enumerate}
289 %\incorrectitem $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
290 %\correctitem $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$
291 %\correctitem $P(a_1)=1$, $P(a_2)=0$
292 %\incorrectitem $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
293 %\end{enumerate}
294 %\end{verbatim}
295 %The default definition of \cs{correctitemformat} puts a frame around
296 %the label.
297 %
298 %\section{Defining a Problem}\label{sec:defproblem}
299 %
300 %It is possible to construct a problem sheet with solutions using the
301 %commands described in the previous sections, however it is also
302 %possible to define a set of problems for later use. In this way you
303 %can create an external file containing many problems some or all of
304 %which can be loaded and used in a document. The \styfmt{probsoln}
305 %package has a default data set labelled \dq{default} in which you
306 %can store problems. Alternatively, you can create multiple data
307 %sets. You can then iterate through each problem in a problem set.
308 %You can use a previously defined problem more than once, which means
309 %that by judicious use of \env{onlyproblem}, \env{onlysolution} or
310 %the \bool{showanswers} boolean variable in conjunction with
311 %\ics{showanswers} and \ics{hideanswers}, you can print the solutions
312 %in a different location to the questions (for example in an
313 %appendix).
314 %
315 %\begin{definition}[\DescribeEnv{defproblem}]
316 %\verb|\begin{defproblem}|\oarg{n}\oarg{default args}\marg{label}\oarg{option}\newline
317 %\meta{definition}\newline
318 %\verb|\end{defproblem}|
319 %\end{definition}
320 %This defines the problem whose label is given by \meta{label}. The
321 %label must be unique for a given data set and should not contain
322 %active characters or a comma. (Active characters include the special characters
323 %such as \$ and \&, but some packages may make other symbols active,
324 %such as the colon (:) character. For example, the \sty{ngerman} and
325 %\sty{babel} packages make certain punctuation active. Check the
326 %relevant package documentation for details.)
327 %
328 %\begin{important}
329 %The final optional argument \meta{option} may be \texttt{fragile} to
330 %indicate that the problem contains verbatim text. Any occurrences of
331 %\env{onlyproblem} or \env{onlysolution} contained within
332 %\envfmt{defproblem} are inherited from \envfmt{defproblem}. (See
333 %\sectionref{sec:verbatim} for further details.)
334 %\end{important}
335 %
336 %If \env{defproblem} occurs in the document or is included via
337 %\ics{input} or \ics{include}, then the problem will be added to
338 %the default data set. If \envfmt{defproblem} occurs in an external
339 %file that is loaded using one of the commands defined in
340 %\sectionref{sec:load} then the problem will be added to
341 %the specified data set.
342 %
343 %The contents of the \env{defproblem} environment should be the text
344 %that defines the problem. This may include any of the commands
345 %defined in \sectionref{sec:showanswers} and
346 %\sectionref{sec:formatting}.
347 %
348 %The problem may optionally take \meta{n} arguments (where
349 %\meta{n} is from 0 to 9). The arguments can be referenced
350 %in the definition via \texttt{\#1},\ldots,\texttt{\#9}.
351 %If \meta{n} is omitted then the problem doesn't take any
352 %arguments.
353 %The following example defines a problem with one argument:
354 %\begin{verbatim}
355 %\begin{defproblem}[1]{diffsin}
356 %Differentiate $f(x)=\sin(#1x)$.
357 %\begin{onlysolution}%
358 % \begin{solution}
359 % $f'(x) = #1\cos(#1x)$
360 % \end{solution}
361 %\end{onlysolution}
362 %\end{defproblem}
363 %\end{verbatim}
364 %
365 %The second optional argument \meta{default args} supplies
366 %default problem arguments that will automatically be used within
367 %\ics{thisproblem} when used in \ics{foreachproblem} in conjunction
368 %with the package option \pkgopt{usedefaultargs}. (See \sectionref{sec:foreach}.)
369 %For example:
370 %\begin{verbatim}
371 %\begin{defproblem}[1][{2}]{diffsin}
372 %Differentiate $f(x)=\sin(#1x)$.
373 %\begin{onlysolution}%
374 % \begin{solution}
375 % $f'(x) = #1\cos(#1x)$
376 % \end{solution}
377 %\end{onlysolution}
378 %\end{defproblem}
379 %\end{verbatim}
380 %\begin{important}
381 %If you don't use \cs{thisproblem} or you don't
382 %use the package option \pkgopt{usedefaultargs}, then you must supply
383 %the arguments.
384 %\end{important}
385 %
386 %\begin{definition}[\DescribeMacro{\newproblem}]
387 %\cs{newproblem}\oarg{n}\oarg{default args}\marg{label}\marg{problem}\marg{solution}
388 %\end{definition}
389 %This is a shortcut command for:
390 %\begin{ttfamily}\obeylines
391 %\cs{begin}\{defproblem\}\oarg{n}\oarg{default args}\marg{label}\%
392 %\meta{problem}\%
393 %\cs{begin}\{onlysolution\}\%
394 %\cs{begin}\{solution\}\%
395 %\meta{solution}\%
396 %\cs{end}\{solution\}\%
397 %\cs{end}\{onlysolution\}\%
398 %\cs{end}\{defproblem\}
399 %\end{ttfamily}
400 %For example:
401 %\begin{verbatim}
402 %\newproblem[1]{diffcos}{%
403 % \(f(x) = \cos(#1x)\)
404 %}%
405 %{%
406 % \(f'(x) = -#1\sin(#1x)\)
407 %}
408 %\end{verbatim}
409 %is equivalent to
410 %\begin{verbatim}
411 %\begin{defproblem}[1]{diffcos}%
412 % \(f(x) = \cos(#1x)\)
413 %\begin{onlysolution}%
414 % \begin{solution}%
415 % \(f'(x) = -#1\sin(#1x)\)
416 % \end{solution}%
417 %\end{onlysolution}%
418 %\end{defproblem}
419 %\end{verbatim}
420 %(In this example, the argument will need to be a positive number
421 %to avoid a double minus in the answer. If you want to perform
422 %floating point arithmetic on the arguments, then try the
423 %\sty{fp} or \sty{pgfmath} packages.)
424 %
425 %Alternatively, if you want to supply default arguments to use when
426 %iterating through problems with \ics{foreachproblem}:
427 %\begin{verbatim}
428 %\newproblem[1][{3}]{diffsin}{%
429 % \(f(x) = \sin(#1x)\)
430 %}%
431 %{%
432 % \(f'(x) = #1\cos(#1x)\)
433 %}
434 %\end{verbatim}
435 %
436 %
437 %\begin{definition}[\DescribeMacro{\newproblem*}]
438 %\cs{newproblem*}\oarg{n}\oarg{default args}\marg{label}\marg{definition}
439 %\end{definition}
440 %This is a shortcut for:
441 %\begin{ttfamily}\obeylines
442 %\cs{begin}\{defproblem\}\oarg{n}\oarg{default args}\marg{label}\%
443 %\meta{definition}\%
444 %\cs{end}\{defproblem\}
445 %\end{ttfamily}
446 %
447 %\begin{important}
448 %Note that you can't use verbatim text with \cs{newproblem} or
449 %\cs{newproblem*}. Use the \env{defproblem} environment instead with
450 %the \texttt{fragile option}.
451 %\end{important}
452 %
453 %\section{Using a Problem}\label{sec:useproblem}
454 %
455 %Once you have defined a problem using \env{defproblem} or
456 %\ics{newproblem} (see \sectionref{sec:defproblem}), you can
457 %later display the problem using:
458 %\begin{definition}[\DescribeMacro{\useproblem}]
459 %\cs{useproblem}\oarg{data set}\marg{label}\marg{arg$_1$}\ldots
460 %\marg{arg$_N$}
461 %\end{definition}
462 %where \meta{data set} is the name of the data set that contains
463 %the problem (the default data set is used if omitted),
464 %\meta{label} is the label identifying the required problem and
465 %\meta{arg$_1$}, \ldots, \meta{arg$_N$}
466 %are the arguments to pass to the problem, if the problem was
467 %defined to have arguments (where $N$ is the number
468 %of arguments specified when the problem was defined).
469 %
470 %For example, in the previous section the problem \texttt{diffcos}
471 %was defined to have one argument, so it can be used as follows:
472 %\begin{verbatim}
473 %\useproblem{diffcos}{3}
474 %\end{verbatim}
475 %This will be equivalent to:
476 %\begin{verbatim}
477 %\(f(x) = \cos(3x)\)
478 %\begin{onlysolution}%
479 %\begin{solution}%
480 %\(f'(x) = -3\sin(3x)\)
481 %\end{solution}%
482 %\end{onlysolution}%
483 %\end{verbatim}
484 %
485 %\section{Loading Problems From External Files}\label{sec:load}
486 %
487 %You can store all your problem definitions (see
488 %\sectionref{sec:defproblem}) in an external file.
489 %These problems can all be appended to the default data set by
490 %including the file via \ics{input} or they can be appended
491 %to other data sets using one of the commands described below.
492 %Once you have loaded all the required problems, you can
493 %iterate through the data sets using the commands described
494 %in \sectionref{sec:foreach}. Note that the commands below
495 %will create a new data set, if the named data set doesn't
496 %exist.
497 %
498 %\begin{definition}[\DescribeMacro{\loadallproblems}]
499 %\cs{loadallproblems}\oarg{data set}\marg{filename}
500 %\end{definition}
501 %This will load all problems defined in \meta{filename} and
502 %append them to the specified data set, in the order in which
503 %they are defined in the file. If \meta{data set} is
504 %omitted, the default data set will be used. If \meta{data set}
505 %doesn't exist, it will be created.
506 %
507 %\begin{definition}[\DescribeMacro{\loadselectedproblems}]
508 %\cs{loadselectedproblems}\oarg{data set}\marg{labels}\marg{filename}
509 %\end{definition}
510 %This is like \cs{loadallproblems}, but only those problems whose
511 %label is listed in the comma-separated list \meta{labels} are
512 %loaded. For example, if I have some problems defined in the
513 %file \texttt{derivatives.tex}, then
514 %\begin{verbatim}
515 %\loadselectedproblems{diffsin,diffcos}{derivatives}
516 %\end{verbatim}
517 %will only load the problems whose labels are \texttt{diffsin}
518 %and \texttt{diffcos}, respectively. All the other problems in
519 %the file will remain undefined.
520 %
521 %\begin{definition}[\DescribeMacro{\loadexceptproblems}]
522 %\cs{loadexceptproblems}\oarg{data set}\marg{exception list}\marg{filename}
523 %\end{definition}
524 %This is the reverse of \cs{loadselectedproblems}. This loads all
525 %problems except those whose labels are listed in \meta{exception
526 %list}.
527 %
528 %\begin{definition}[\DescribeMacro{\loadrandomproblems}]
529 %\cs{loadrandomproblems}\oarg{data set}\marg{n}\marg{filenames}
530 %\end{definition}
531 %This randomly loads \meta{n} problems from the comma-separated
532 %list\footnote{The list form was added to v3.05. Earlier versions
533 %only allow a single filename.}
534 %of \meta{filenames} and adds them to the given data set. If
535 %\meta{data set} is omitted, the default data set is assumed. Note
536 %that the problems will be added to the data set in a random order,
537 %not in the order in which they were defined. There must be at least
538 %\meta{n} problems defined across the given list of files.
539 %
540 %Note that there's a difference between
541 %\begin{verbatim}
542 %\loadrandomproblems{5}{problemset1}
543 %\loadrandomproblems{5}{problemset2}
544 %\end{verbatim}
545 %and
546 %\begin{verbatim}
547 %\loadrandomproblems{10}{problemset1,problemset2}
548 %\end{verbatim}
549 %In the first case, the data set will contain 5 problems randomly
550 %selected from \texttt{problemset1} and 5 problems randomly selected
551 %from \texttt{problemset2}. Whereas in the second case, the data set
552 %will contain 10 problems randomly selected across both files.
553 %
554 %\begin{definition}[\DescribeMacro{\loadrandomexcept}]
555 %\cs{loadrandomexcept}\oarg{data set}\marg{n}\marg{filenames}\marg{exception list}
556 %\end{definition}
557 %This is similar to \cs{loadrandomproblems} except that it won't load
558 %those problems whose labels are listed in \meta{exception list}.
559 %\textbf{If you want to automatically exclude problems included in
560 %previous documents, see \sectionref{sec:exprev}.}
561 %
562 %Note that the random number generator has been modified in version
563 %3.01 in order to fix a bug. If you want to ensure that your random
564 %numbers are compatible with earlier versions, you can switch to the
565 %old generator using
566 %\begin{definition}[\DescribeMacro{\PSNuseoldrandom}]
567 %\cs{PSNuseoldrandom}
568 %\end{definition}
569 %
570 %\begin{important}
571 %It is generally not a good idea to place anything in
572 %\meta{filename} that is not inside the body of \env{defproblem}
573 %or in the arguments to \ics{newproblem} or \ics{newproblem*}.
574 %All the commands in this section input the external file within
575 %a local scope, so command definitions would need to be made
576 %global to have any effect. In addition, \cs{loadrandomproblems}
577 %has to load each file twice, which means that anything outside
578 %a problem definition will be parsed twice.
579 %\end{important}
580 %
581 %\subsection{Randomly Selecting Problems Not Selected in Previous
582 %Documents}
583 %\label{sec:exprev}
584 %
585 %Suppose you have a large set of questions that you want to randomly
586 %select for assignments and exams. The chances are, you don't want to
587 %include questions that have been previously set for, say, the last
588 %three years. That is, you don't want to select questions the
589 %students may already have seen. As from version 3.03, you can now do
590 %this.
591 %
592 %The \sty{probsoln} package defaults to the UK academic year, which
593 %starts in September. If this isn't appropriate, you can change it
594 %using:
595 %\begin{definition}[\DescribeMacro{\SetStartMonth}]
596 %\cs{SetStartMonth}\marg{n}
597 %\end{definition}
598 %where \meta{n} is the number of the month. (1 = January, 2 =
599 %February, etc.)
600 %
601 %The \emph{start year} is the calender year in effect when the
602 %academic year started. For example, if this is the academic year
603 %2011/12, then the start year is 2011. This is automatically set to
604 %the start of the current academic year. It is also updated when
605 %\cs{SetStartMonth} is used.\footnote{So don't use \cs{SetStartMonth}
606 %after \cs{SetStartYear}.} If you want to set it to a specific year,
607 %you can use:
608 %\begin{definition}[\DescribeMacro{\SetStartYear}]
609 %\cs{SetStartYear}\marg{year}
610 %\end{definition}
611 %For example: \verb|\SetStartYear{2008}| indicates the academic year
612 %2008/9.
613 %
614 %There are two files concerned with previously used labels. They are:
615 %\begin{description}
616 %
617 % \item[The previously used labels file] This keeps track of all
618 % problems used in previous years, as well as problems used by
619 % other documents that have this as their previously used labels
620 % file, and it contains the problem labels from the last run of
621 % the current document.
622 %
623 % \item[The current used labels file] This defaults to
624 %\cs{jobname}\texttt{.prb}, but the name can be changed using:
625 % \begin{definition}[\DescribeMacro{\SetUsedFileName}]
626 % \cs{SetUsedFileName}\marg{name}
627 % \end{definition}
628 % This file keeps track of all the labels used in the current
629 % document from the previous \LaTeX\ run. Note that if you want to
630 % delete this file, first clear it using
631 % \begin{definition}[\DescribeMacro{\ClearUsedFile}]
632 % \cs{ClearUsedFile}\marg{file}
633 % \end{definition}
634 % in place of \cs{ExcludePreviousFile}\marg{file}, described below.
635 % The argument \meta{file} is the previously used labels file
636 % described above. \cs{ClearUsedFile} will remove all labels in
637 % the current used labels file from the previously used labels file
638 % and clear the current used labels file. Once this file is empty,
639 % it may then be deleted.
640 %
641 %\end{description}
642 %
643 %Before loading randomly selected problems, first specify the
644 %previously used labels file with the command:
645 %\begin{definition}[\DescribeMacro{\ExcludePreviousFile}]
646 %\cs{ExcludePreviousFile}\oarg{number of years}\marg{file name}
647 %\end{definition}
648 %where \meta{file name} is the name of the previously used file. The
649 %optional argument \meta{number of years} specifies the year cut-off.
650 %This defaults to 3, which means that only those labels used this
651 %year or the previous 2 years will be kept. Any problems used before
652 %then may be reused.
653 %
654 %Suppose I'm lecturing a first year undergraduate mathematics course
655 %(designated, say, mth101). I want to set assignments on each topic
656 %and an exam at the end of the year (as well as a resit or second
657 %sitting paper). I've got databases with problems for each topic, but
658 %the first and second sitting exams mustn't include any of the
659 %problems used in the assignments or any problems used in assignments
660 %or exams for the previous two academic years. I'm going to arrange
661 %my directory structure as follows:
662 %\begin{itemize}
663 %\item \texttt{mth101/}
664 % \begin{itemize}
665 % \item \texttt{assignment1/} (differentiation)
666 % \begin{itemize}
667 % \item \texttt{assignment1.tex}
668 % \end{itemize}
669 % \item \texttt{assignment2/} (probability spaces)
670 % \begin{itemize}
671 % \item \texttt{assignment2.tex}
672 % \end{itemize}
673 % \item \texttt{assignment3/} (linear algebra)
674 % \begin{itemize}
675 % \item \texttt{assignment3.tex}
676 % \end{itemize}
677 % \item \texttt{exams/}
678 % \begin{itemize}
679 % \item \texttt{exam.tex} (first sitting)
680 % \item \texttt{resit.tex} (second sitting)
681 % \end{itemize}
682 % \item \texttt{databases/}
683 % \begin{itemize}
684 % \item \texttt{differentiation.tex}
685 % \item \texttt{probabilityspaces.tex}
686 % \item \texttt{linearalgebra.tex}
687 % \end{itemize}
688 % \item \texttt{previouslabels.tex} (created by \sty{probsoln})
689 % \end{itemize}
690 %\end{itemize}
691 %
692 %\section{Iterating Through Datasets}\label{sec:foreach}
693 %
694 %Once you have defined all your problems for a given data set, you
695 %can use an individual problem with \ics{useproblem} (see
696 %\sectionref{sec:useproblem}) but it is more likely that you will
697 %want to iterate through all the problems so that you don't need to
698 %remember the labels of all the problems you have defined.
699 %
700 %\begin{definition}[\DescribeMacro{\foreachproblem}]
701 %\cs{foreachproblem}\oarg{data set}\marg{body}
702 %\end{definition}
703 %This does \meta{body} for each problem in the given data set.
704 %If \meta{data set} is omitted, the default data set is used.
705 %Within \meta{body} you can use
706 %\begin{definition}[\DescribeMacro{\thisproblem}]
707 %\cs{thisproblem}
708 %\end{definition}
709 %to use the current problem and
710 %\begin{definition}[\DescribeMacro{\thisproblemlabel}]
711 %\cs{thisproblemlabel}
712 %\end{definition}
713 %to access the current label. If the problem requires arguments,
714 %and no default arguments were supplied in the problem definition or
715 %the package option \pkgopt{usedefaultargs} was not used, then
716 %you will be prompted for arguments, so if you want to use this
717 %approach you will need to use \LaTeX\ in interactive mode. If
718 %you do provide arguments, they will be stored in the event that
719 %you need to iterate through the data set again. The
720 %arguments will be included in \cs{thisproblem}, so you only
721 %need to use \cs{thisproblem} without having to specify
722 %\ics{useproblem}.
723 %
724 %For example, to iterate through all problems
725 %in the default data set:
726 %\begin{verbatim}
727 %\begin{enumerate}
728 %\foreachproblem{\item\thisproblem}
729 %\end{enumerate}
730 %\end{verbatim}
731 %
732 %\begin{definition}[\DescribeMacro{\foreachsolution}]
733 %\cs{foreachsolution}\oarg{data set}\marg{body}
734 %\end{definition}
735 %This is equivalent to \cs{foreachproblem}, but only iterates
736 %through problems that contain the \env{onlysolution} environment.
737 %Note that you still need to use \ics{showanswers} or the
738 %\pkgopt{answers} package option for the contents of the
739 %\env{onlysolution} environment to appear.
740 %
741 %\begin{definition}[\DescribeMacro{\foreachdataset}]
742 %\cs{foreachdataset}\marg{cmd}\marg{body}
743 %\end{definition}
744 %This does \meta{body} for each of the defined data sets. Within
745 %\meta{body}, \meta{cmd} will be set to the name of the current
746 %data set. For example, to display all problems in all data sets:
747 %\begin{verbatim}
748 %\begin{enumerate}
749 %\foreachdataset{\thisdataset}{%
750 %\foreachproblem[\thisdataset]{\item\thisproblem}}
751 %\end{enumerate}
752 %\end{verbatim}
753 %
754 %Suppose I have two external files called
755 %\texttt{derivatives.tex} and \texttt{probspaces.tex} which
756 %define problems using both \env{onlyproblem} and
757 %\env{onlysolution} for example:
758 %\begin{verbatim}
759 %\begin{defproblem}{cosxsqsinx}%
760 %\begin{onlyproblem}%
761 %$y = \cos(x^2)\sin x$.%
762 %\end{onlyproblem}%
763 %\begin{onlysolution}%
764 %\[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]
765 %\end{onlysolution}%
766 %\end{defproblem}
767 %\end{verbatim}
768 %I can write a document that creates two data sets, one for
769 %the derivative problems and one for the problems about
770 %probability spaces. I can then use \ics{hideanswers} and
771 %iterate through the require data set to produce the problems.
772 %Later, I can use \ics{showanswers} and iterate over all problems defined in both data
773 %sets to produce the chapter containing all the answers. When
774 %displaying the questions, I have taken advantage of the fact that
775 %I can cross-reference items within an \env{enumerate} environment,
776 %and redefined \ics{theenumi} to label the questions according to
777 %the chapter. The cross-reference label is constructed from
778 %the problem label and is referenced in the answer section to
779 %ensure that the answers have the same label as the questions.
780 %\begin{verbatim}
781 %\documentclass{report}
782 %\usepackage{probsoln}
783 %\begin{document}
784 %\hideanswers
785 %\chapter{Differentiation}
786 %% randomly select 25 problems from derivatives.tex and add to
787 %% the data set called 'deriv'
788 %\loadrandomproblems[deriv]{25}{derivatives}
789 %
790 %% Display the problems
791 %\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
792 %\begin{enumerate}
793 %\foreachproblem[deriv]{\item\label{prob:\thisproblemlabel}\thisproblem}
794 %\end{enumerate}
795 %% You may need to change \theenumi back here
796 %
797 %\chapter{Probability Spaces}
798 %% randomly select 25 problems from probspaces.tex and add to
799 %% the data set called 'spaces'
800 %\loadrandomproblems[spaces]{25}{probspaces}
801 %
802 %% Display the problems
803 %\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
804 %\begin{enumerate}
805 %\foreachproblem[spaces]{\item\label{prob:\thisproblemlabel}\thisproblem}
806 %\end{enumerate}
807 %% You may need to change \theenumi back here
808 %
809 %\appendix
810 %
811 %\chapter{Solutions}
812 %\showanswers
813 %\begin{itemize}
814 %\foreachdataset{\thisdataset}{%
815 %\foreachproblem[\thisdataset]{\item[\ref{prob:\thisproblemlabel}]\thisproblem}
816 %}
817 %\end{itemize}
818 %
819 %\end{document}
820 %\end{verbatim}
821 %
822 %\section{Random Number Generator}\label{sec:random}
823 %
824 %This package provides a pseudo-random number generator that is used
825 %by \ics{loadrandomproblems}. As noted earlier the random number
826 %generator has been modified in version 3.01 in order to fix a bug.
827 %If you want to ensure that your random numbers are compatible with
828 %earlier versions, you can switch to the old generator using
829 %\begin{definition}[\DescribeMacro{\PSNuseoldrandom}]
830 %\cs{PSNuseoldrandom}
831 %\end{definition}
832 %
833 %\begin{definition}[\DescribeMacro{\PSNrandseed}]
834 %\cs{PSNrandseed}\marg{n}
835 %\end{definition}
836 %This sets the seed to \meta{n} which must be a non-zero integer.
837 %For example, to generate a different set of random numbers
838 %every time you \LaTeX\ your document,\footnote{assuming you
839 %leave at least a minute between runs.} put the following in your
840 %preamble:
841 %\begin{verbatim}
842 %\PSNrandseed{\time}
843 %\end{verbatim}
844 %or to generate a different set of random numbers every year you
845 %\LaTeX\ your document:
846 %\begin{verbatim}
847 %\PSNrandseed{\year}
848 %\end{verbatim}
849 %
850 %\begin{definition}[\DescribeMacro{\PSNgetrandseed}]
851 %\cs{PSNgetrandseed}\marg{register}
852 %\end{definition}
853 %This stores the current seed in the count register specified by
854 %\meta{register}.
855 %For example:
856 %\begin{verbatim}
857 %\newcount\myseed
858 %\PSNgetrandseed{\myseed}
859 %\end{verbatim}
860 %
861 %\begin{definition}[\DescribeMacro{\PSNrandom}]
862 %\cs{PSNrandom}\marg{register}\marg{n}
863 %\end{definition}
864 %Generates a random integer from 1 to \meta{n} and stores in
865 %the count register specified by \meta{register}. For example,
866 %the following generates an integer from 1 to 10 and stores it
867 %in the register \cs{myreg}:
868 %\begin{verbatim}
869 %\newcount\myreg
870 %\PSNrandom{\myreg}{10}
871 %\end{verbatim}
872 %
873 %\begin{definition}[\DescribeMacro{\random}]
874 %\cs{random}\marg{counter}\marg{min}\marg{max}
875 %\end{definition}
876 %Generates a random integer from \meta{min} to \meta{max} and
877 %stores in the given counter. For example, the following generates
878 %a random number between 3 and 8 (inclusive) and stores it in
879 %the counter \texttt{myrand}.
880 %\begin{verbatim}
881 %\newcounter{myrand}
882 %\random{myrand}{3}{8}
883 %\end{verbatim}
884 %
885 %\begin{definition}[\DescribeMacro{\doforrandN}]
886 %\cs{doforrandN}\marg{n}\marg{cmd}\marg{list}\marg{text}
887 %\end{definition}
888 %Randomly selects \meta{n} values from the comma-separated list
889 %given by \meta{list} and iterates through this subset. On
890 %each iteration it sets \meta{cmd} to the current value and
891 %does \meta{text}. For example, the following will load a
892 %randomly selected problem from two of the listed files (where
893 %\texttt{file1.tex}, \texttt{file2.tex} and \texttt{file3.tex}
894 %are files containing at least one problem):
895 %\begin{verbatim}
896 %\doforrandN{2}{\thisfile}{file1,file2,file3}{%
897 %\loadrandomproblems{1}{\thisfile}}
898 %\end{verbatim}
899 %
900 %\section{Compatibility With Versions Prior to 3.0}
901 %
902 %Version 3.0 of the \sty{probsoln} package completely changed the
903 %structure of the package, but the commands described in this
904 %section have been provided to maintain compatibility with
905 %earlier versions. The only problems that are likely to occur are
906 %those where commands are contained within groups. This will
907 %effect any commands that are contained in external files that are
908 %outside of the arguments to \ics{newproblem} and \ics{newproblem*}.
909 %However, since the external files had to be parsed twice in
910 %order to load the problems, this shouldn't be an issue as adding
911 %anything other than problem definitions in those files would
912 %be problematic anyway.
913 %
914 %The other likely difference is where the random generator is
915 %used in a group. This includes commands such as
916 %\ics{selectrandomly}. For example, if your document contained
917 %something like:
918 %\begin{verbatim}
919 %\begin{enumerate}
920 %\selectrandomly{file1}{8}
921 %
922 %\item Solve the following:
923 %\begin{enumerate}
924 %\selectrandomly{file2}{4}
925 %\end{enumerate}
926 %
927 %\selectrandomly{file3}{2}
928 %\end{enumerate}
929 %\end{verbatim}
930 %Then using versions prior to v3.0 will produce a different
931 %set of random numbers since the second \cs{selectrandomly}
932 %is in a different level of grouping. If you want to ensure
933 %that the document produces exactly the same random set with
934 %the new version as with the old version, you will need to
935 %get and set the random number seed. For example, the above
936 %would need to be modified so that it becomes:
937 %\begin{verbatim}
938 %\begin{enumerate}
939 %\selectrandomly{file1}{8}
940 %
941 %\item Solve the following:
942 %\newcount\oldseed
943 %\PSNgetrandseed{\oldseed}
944 %\begin{enumerate}
945 %\selectrandomly{file2}{4}
946 %\end{enumerate}
947 %\PSNrandseed{\oldseed}
948 %
949 %\selectrandomly{file3}{2}
950 %\end{enumerate}
951 %\end{verbatim}
952 %
953 %\begin{definition}[\DescribeMacro{\selectrandomly}]
954 %\cs{selectrandomly}\marg{filename}\marg{n}
955 %\end{definition}
956 %This is now equivalent to:
957 %\begin{ttfamily}\obeylines
958 %\{\cs{loadrandomproblems}\oarg{filename}\marg{n}\marg{filename}\}\%
959 %\cs{foreachproblem}\oarg{filename}\{\cs{PSNitem}\cs{thisproblem}\cs{endPSNitem}\}
960 %\end{ttfamily}
961 %
962 %\begin{definition}[\DescribeMacro{\selectallproblems}]
963 %\cs{selectallproblems}\marg{filename}
964 %\end{definition}
965 %This is now equivalent to:
966 %\begin{ttfamily}\obeylines
967 %\{\cs{loadallproblems}\oarg{filename}\marg{filename}\}\%
968 %\cs{foreachproblem}\oarg{filename}\{\cs{PSNitem}\cs{thisproblem}\cs{endPSNitem}\}
969 %\end{ttfamily}
970 %
971 %Note that in both the above cases, a new data set is created
972 %with the same name as the file name.
973 %
974 % \StopEventually{\clearpage\phantomsection\addcontentsline{toc}{section}{Index}\PrintIndex}
975 %
976 %
977 %
978 %\section{The Code}
979 %\iffalse
980 % \begin{macrocode}
981 %<*probsoln.sty>
982 % \end{macrocode}
983 %\fi
984 %\subsection{Package Definition}
985 % This package requires \LaTeXe.
986 % \begin{macrocode}
987 \NeedsTeXFormat{LaTeX2e}
988 % \end{macrocode}
989 % Identify this package and version:
990 % \begin{macrocode}
991 \ProvidesPackage{probsoln}[2017/07/10 v3.05 (NLCT)]
992 % \end{macrocode}
993 % Required packages:
994 %\changes{3.01}{2011/08/22}{substr package no longer required}
995 % \begin{macrocode}
996 \RequirePackage{ifthen}
997 \RequirePackage{amsmath}
998 % \end{macrocode}
999 %\changes{3.03}{2011/01/16}{added etoolbox to list of required
1000 %packages}
1001 % \begin{macrocode}
1002 \RequirePackage{etoolbox}
1003 % \end{macrocode}
1004 % \subsection{Package Options}
1005 %\begin{macro}{\ifshowanswers}
1006 % Define boolean to determine whether or not to show the
1007 % solutions. This governs whether the contents of \env{onlysolution}
1008 % and \env{onlyproblem} are displayed.
1009 % \begin{macrocode}
1010 \newif\ifshowanswers
1011 \showanswersfalse
1012 % \end{macrocode}
1013 %\end{macro}
1014 %\begin{macro}{\showanswers}
1015 % Define synonym for \cs{showanswerstrue}
1016 % \begin{macrocode}
1017 \let\showanswers\showanswerstrue
1018 % \end{macrocode}
1019 %\end{macro}
1020 %\begin{macro}{\hideanswers}
1021 % Define synonym for \cs{showanswersfalse}
1022 % \begin{macrocode}
1023 \let\hideanswers\showanswersfalse
1024 % \end{macrocode}
1025 %\end{macro}
1026 % The package option \pkgopt{answers} displays the solutions.
1027 % \begin{macrocode}
1028 \DeclareOption{answers}{\showanswerstrue}
1029 % \end{macrocode}
1030 % The package option \pkgopt{noanswers} hides the solutions.
1031 % \begin{macrocode}
1032 \DeclareOption{noanswers}{\showanswersfalse}
1033 % \end{macrocode}
1034 % Determine whether or not to use default arguments for problems.
1035 %\begin{macro}{\ifusedefaultprobargs}
1036 % \begin{macrocode}
1037 \newif\ifusedefaultprobargs
1038 % \end{macrocode}
1039 %\end{macro}
1040 %\begin{option}{usedefaultargs}
1041 % \begin{macrocode}
1042 \DeclareOption{usedefaultargs}{\usedefaultprobargstrue}
1043 % \end{macrocode}
1044 %\end{option}
1045 %\begin{option}{usedefaultargs}
1046 % \begin{macrocode}
1047 \DeclareOption{nousedefaultargs}{\usedefaultprobargsfalse}
1048 \usedefaultprobargsfalse
1049 % \end{macrocode}
1050 %\end{option}
1051 %
1052 %\begin{macro}{\prob@showdraftlabel}
1053 %\begin{definition}
1054 %\cs{prob@showdraftlabel}\marg{db name}\marg{label}
1055 %\end{definition}
1056 % Used by \cs{useproblem} to display data base name and problem
1057 % label when in draft mode.
1058 % \begin{macrocode}
1059 \newcommand*{\prob@showdraftlabel}[2]{}
1060 % \end{macrocode}
1061 %\end{macro}
1062 %\begin{macro}{\draftproblemlabel}
1063 %\begin{definition}
1064 %\cs{draftproblemlabel}\marg{db name}\marg{label}
1065 %\end{definition}
1066 % Displays the data base name and label.
1067 % \begin{macrocode}
1068 \newcommand*{\draftproblemlabel}[2]{[#1,#2]}
1069 % \end{macrocode}
1070 %\end{macro}
1071 % Draft mode displays the problem label using \cs{draftproblemlabel}
1072 % \begin{macrocode}
1073 \DeclareOption{draft}{%
1074 \renewcommand*{\prob@showdraftlabel}[2]{\draftproblemlabel{#1}{#2}}}
1075 % \end{macrocode}
1076 % Final mode:
1077 % \begin{macrocode}
1078 \DeclareOption{final}{%
1079 \renewcommand*{\prob@showdraftlabel}[2]{}}
1080 % \end{macrocode}
1081 % Process package options:
1082 % \begin{macrocode}
1083 \ProcessOptions
1084 % \end{macrocode}
1085 %\changes{3.02}{2011/12/10}{added xkeyval to required package list}
1086 % \begin{macrocode}
1087 \RequirePackage{xkeyval}
1088 % \end{macrocode}
1089 %\begin{macro}{\if@prob@fragile}
1090 %\changes{3.02}{2011/12/10}{new}
1091 %Need a conditional to determine whether \cs{long@collect@body}
1092 %needs to be aware of verbatim contents.
1093 % \begin{macrocode}
1094 \define@boolkey{probsoln}[@prob@]{fragile}[true]{}
1095 % \end{macrocode}
1096 %\end{macro}
1097 %\begin{macro}{\ProbSolnFragileExt}
1098 %\changes{3.02}{2011/12/10}{new}
1099 % The extension used for temporary files dealing with fragile
1100 % contents.
1101 % \begin{macrocode}
1102 \newcommand*{\ProbSolnFragileExt}{vrb}
1103 % \end{macrocode}
1104 %\end{macro}
1105 %\begin{macro}{\ProbSolnFragileFile}
1106 %\changes{3.02}{2011/12/10}{new}
1107 % The filename used for temporary files dealing with fragile
1108 % contents.
1109 % \begin{macrocode}
1110 \newcommand*{\ProbSolnFragileFile}{\jobname}
1111 % \end{macrocode}
1112 %\end{macro}
1113 %\begin{macro}{\probsoln@write}
1114 %\changes{3.02}{2011/12/10}{new}
1115 %File handle for temporary files.
1116 % \begin{macrocode}
1117 \newwrite\probsoln@write
1118 % \end{macrocode}
1119 %\end{macro}
1120 %
1121 %\begin{macro}{\probsoln@startyear}
1122 %\changes{3.03}{2011/01/16}{new}
1123 % The year as at the start of the new academic year. (For example,
1124 % if the academic year starts in September and today is any date
1125 % between 2011-09-01 and 2012-08-30, then the start year is 2011.)
1126 % \begin{macrocode}
1127 \newcount\probsoln@startyear
1128 % \end{macrocode}
1129 %\end{macro}
1130 %
1131 %\begin{macro}{\SetStartYear}
1132 %\changes{3.03}{2011/01/16}{new}
1133 % Provide command to set the starting year manually.
1134 % \begin{macrocode}
1135 \newcommand*{\SetStartYear}[1]{%
1136 \probsoln@startyear=#1\relax
1137 \renewcommand\SetStartMonth[1]{%
1138 \PackageError{probsoln}{\string\SetStartMonth\space
1139 can't be used after \string\SetStartYear}{}}%
1140 }
1141 % \end{macrocode}
1142 %\end{macro}
1143 %
1144 %\begin{macro}{\GetStartYear}
1145 % Gets the value of the start year count register:
1146 % \begin{macrocode}
1147 \newcommand*{\GetStartYear}{\probsoln@startyear}
1148 % \end{macrocode}
1149 %\end{macro}
1150 %
1151 %\begin{macro}{\probsoln@startmonth}
1152 %\changes{3.03}{2011/01/16}{new}
1153 % The month starting the academic year. (1=January, 2=February,
1154 % etc).
1155 % \begin{macrocode}
1156 \newcount\probsoln@startmonth
1157 % \end{macrocode}
1158 %\end{macro}
1159 %\begin{macro}{\SetStartMonth}
1160 %\changes{3.03}{2011/01/16}{new}
1161 % Define command to set the month starting the academic year. This
1162 % also sets the starting year.
1163 % \begin{macrocode}
1164 \newcommand*{\SetStartMonth}[1]{%
1165 \probsoln@startmonth=#1\relax
1166 \probsoln@startyear=\year\relax
1167 \ifnum\month<\probsoln@startmonth
1168 \advance\probsoln@startyear by -1\relax
1169 \fi
1170 }
1171 % \end{macrocode}
1172 %\end{macro}
1173 %
1174 % Set the default starting month to 9 (September):
1175 % \begin{macrocode}
1176 \SetStartMonth{9}
1177 % \end{macrocode}
1178 %
1179 %\begin{macro}{\probsoln@prev}
1180 %\changes{3.03}{2011/01/16}{new}
1181 % File handle for file containing previous labels.
1182 % \begin{macrocode}
1183 \newwrite\probsoln@prev
1184 % \end{macrocode}
1185 %\end{macro}
1186 %
1187 %\begin{macro}{\probsoln@used}
1188 %\changes{3.03}{2011/01/16}{new}
1189 % File handle for file containing previous used.
1190 % \begin{macrocode}
1191 \newwrite\probsoln@used
1192 % \end{macrocode}
1193 %\end{macro}
1194 %
1195 %\begin{macro}{\probsoln@prev@cutoff}
1196 %\changes{3.03}{2011/01/16}{new}
1197 % Cut-off year. Problems excluded if the year they were set is
1198 % greater than the cut-off year.
1199 % \begin{macrocode}
1200 \newcount\probsoln@prev@cutoff
1201 % \end{macrocode}
1202 %\end{macro}
1203 %
1204 %\begin{macro}{\@probsoln@usedfilename}
1205 % Stores the file name for the used problems file. (Defaults to
1206 % \cs{jobname}\texttt{.prb})
1207 % \begin{macrocode}
1208 \newcommand*{\@probsoln@usedfilename}{\jobname.prb}
1209 % \end{macrocode}
1210 %\end{macro}
1211 %
1212 %\begin{macro}{\SetUsedFileName}
1213 % Set the name of the used problems file.
1214 % \begin{macrocode}
1215 \newcommand*{\SetUsedFileName}[1]{%
1216 \renewcommand*{\@probsoln@usedfilename}{#1}%
1217 }
1218 % \end{macrocode}
1219 %\end{macro}
1220 %
1221 %\begin{macro}{\ClearUsedFile}
1222 %\changes{3.03}{2011/01/16}{new}
1223 % Clear the contents of the used file (\cs{@probsoln@usedfilename})
1224 % and remove corresponding entries from the previous file (as
1225 % specified in \cs{ExcludePreviousFile}).
1226 % Not to be used after \cs{ExcludePreviousFile}.
1227 % \begin{macrocode}
1228 \newcommand*{\ClearUsedFile}[1]{%
1229 \probsoln@prev@cutoff=0\relax
1230 \@probsoln@readprev{#1}%
1231 % \end{macrocode}
1232 % Only write labels that aren't in the used file.
1233 % \begin{macrocode}
1234 \@for\@this@db:=\prob@databases\do{%
1235 {%
1236 \edef\@prev@list{\csname probsoln@prev@list@\@this@db\endcsname}%
1237 \ifdefempty{\@prev@list}%
1238 {}%
1239 {%
1240 \@for\@this@label:=\@prev@list\do{%
1241 \ifcsundef{@used@problem@\@this@db @\@this@label}%
1242 {%
1243 \immediate\write\probsoln@prev{%
1244 \string\previousproblem{\@this@label}{\@this@db}%
1245 {\csname @prev@problem@\@this@db @\@this@label\endcsname}}%
1246 }%
1247 {}%
1248 }%
1249 }%
1250 }%
1251 }%
1252 \immediate\closeout\probsoln@prev
1253 \immediate\closeout\probsoln@used
1254 \@disable@exclude@prev
1255 }
1256 % \end{macrocode}
1257 %\end{macro}
1258 %
1259 %\begin{macro}{\ExcludePreviousFile}
1260 %\changes{3.03}{2011/01/16}{new}
1261 % Exclude problems used in the last \meta{n} years.
1262 % \begin{macrocode}
1263 \newcommand*{\ExcludePreviousFile}[2][3]{%
1264 \probsoln@prev@cutoff=\probsoln@startyear\relax
1265 \advance\probsoln@prev@cutoff by -#1\relax
1266 \@probsoln@readprev{#2}%
1267 \@write@prev
1268 \def\ExcludePreviousFile[2][3]{\PackageError{probsoln}{Only one
1269 instance of \string\ExcludePreviousFile\space allowed}{You've
1270 already used this command. You are only allowed to use it once}}%
1271 \def\ClearUsedFile[1]{%
1272 \PackageError{probsoln}%
1273 {\string\ClearUsedFile\space may not be used after
1274 \string\ExcludePreviousFile}{}}%
1275 }
1276 % \end{macrocode}
1277 %\end{macro}
1278 %
1279 %\begin{macro}{\@probsoln@readprev}
1280 % Read contents of previous and used files and open for writing.
1281 % \begin{macrocode}
1282 \newcommand*{\@probsoln@readprev}[1]{%
1283 \@enable@exclude@prev
1284 \InputIfFileExists{#1}%
1285 {\PackageInfo{probsoln}%
1286 {Excluded problem file `#1' found}}%
1287 {\PackageInfo{probsoln}%
1288 {Excluded problem file `#1' not found. A new one will be
1289 created}}%
1290 \InputIfFileExists{\@probsoln@usedfilename}%
1291 {\PackageInfo{probsoln}%
1292 {Current problems file `\@probsoln@usedfilename' found}}%
1293 {\PackageInfo{probsoln}%
1294 {No current problem file `\@probsoln@usedfilename' found. A new one will be created}}%
1295 \immediate\openout\probsoln@prev=#1
1296 \immediate\openout\probsoln@used=\@probsoln@usedfilename
1297 }
1298 % \end{macrocode}
1299 %\end{macro}
1300 %
1301 %\begin{macro}{\probsoln@prev@list}
1302 %\changes{3.03}{2011/01/16}{new}
1303 % Maintain a list of all previous problem labels:
1304 % \begin{macrocode}
1305 \newcommand*{\probsoln@prev@list@default}{}
1306 % \end{macrocode}
1307 %\end{macro}
1308 %
1309 %\begin{macro}{\@write@prev}
1310 %\changes{3.03}{2011/01/16}{new}
1311 % Write all previous labels to file.
1312 % \begin{macrocode}
1313 \newcommand*{\@write@prev}{%
1314 \@for\@this@db:=\prob@databases\do{%
1315 {%
1316 \edef\@prev@list{\csname probsoln@prev@list@\@this@db\endcsname}%
1317 \ifdefempty{\@prev@list}%
1318 {}%
1319 {%
1320 \@for\@this@label:=\@prev@list\do{%
1321 \immediate\write\probsoln@prev{%
1322 \string\previousproblem{\@this@label}{\@this@db}%
1323 {\csname @prev@problem@\@this@db @\@this@label\endcsname}}%
1324 }%
1325 }%
1326 }%
1327 }%
1328 }
1329 % \end{macrocode}
1330 %\end{macro}
1331 %
1332 %\begin{macro}{\@enable@exclude@prev}
1333 %\changes{3.03}{2011/01/16}{new}
1334 % Enable commands for excluding previously selected problems.
1335 % \begin{macrocode}
1336 \newcommand*{\@enable@exclude@prev}{%
1337 % \end{macrocode}
1338 % Redefine macro that adds used problem to used problem file and
1339 % previous problem file.
1340 % \begin{macrocode}
1341 \renewcommand*{\@add@used@problem}[2]{%
1342 \immediate\write\probsoln@used{\string\usedproblem{##1}{##2}{\number\probsoln@startyear}}%
1343 \ifcsundef{@prev@problem@##2@##1}%
1344 {%
1345 \immediate\write\probsoln@prev{%
1346 \string\previousproblem{##1}{##2}{\number\probsoln@startyear}}%
1347 \expandafter\xdef\csname @prev@problem@##2@##1\endcsname{%
1348 \number\probsoln@startyear}%
1349 }%
1350 {%
1351 \expandafter\ifnum\csname @prev@problem@##2@##1\endcsname
1352 <\probsoln@startyear
1353 \immediate\write\probsoln@prev{%
1354 \string\previousproblem{##1}{##2}{\number\probsoln@startyear}}%
1355 \expandafter\xdef\csname @prev@problem@##2@##1\endcsname{%
1356 \number\probsoln@startyear}%
1357 \fi
1358 }%
1359 }%
1360 % \end{macrocode}
1361 % Redefine macro that fetches the exclusion list. (First argument is
1362 % the macro in which to store the list, the second argument is the
1363 % database.)
1364 % \begin{macrocode}
1365 \renewcommand*{\@fetch@excluded@list}[2]{%
1366 \def##1{}%
1367 \ifcsdef{probsoln@prev@list@##2}%
1368 {%
1369 \edef\@prev@list{\csname probsoln@prev@list@##2\endcsname}%
1370 \@for\@this@label:=\@prev@list\do{%
1371 % \end{macrocode}
1372 % If it isn't one of the used problems, it can be added to the
1373 % exclusion list:
1374 % \begin{macrocode}
1375 \ifcsundef{@used@problem@##2@\@this@label}%
1376 {%
1377 % \end{macrocode}
1378 % It isn't, so label can be added to the exclusion list:
1379 % \begin{macrocode}
1380 \ifcsempty{##1}%
1381 {\edef##1{\@this@label}}%
1382 {\edef##1{##1,\@this@label}}%
1383 }%
1384 {}%
1385 }%
1386 }%
1387 {}%
1388 }%
1389 % \end{macrocode}
1390 % Add new previous list for given database:
1391 % \begin{macrocode}
1392 \renewcommand*{\@add@newprevlist}[1]{%
1393 \expandafter\gdef\csname probsoln@prev@list@##1\endcsname{}%
1394 }%
1395 % \end{macrocode}
1396 % Redefine macro that closes the exclusion-related files.
1397 % \begin{macrocode}
1398 \renewcommand*{\close@probsoln@prev}{%
1399 \closeout\probsoln@prev
1400 \closeout\probsoln@used
1401 }%
1402 }
1403 % \end{macrocode}
1404 %\end{macro}
1405 %\begin{macro}{\@disable@exclude@prev}
1406 %\changes{3.03}{2011/01/16}{new}
1407 % Disable commands for excluding previously selected problems.
1408 % \begin{macrocode}
1409 \newcommand*{\@disable@exclude@prev}{%
1410 \renewcommand*{\@add@used@problem}[2]{}%
1411 \renewcommand*{\@fetch@excluded@list}[2]{\def##1{}}%
1412 \renewcommand*{\@add@newprevlist}[1]{}%
1413 \renewcommand*{\close@probsoln@prev}{}%
1414 }
1415 % \end{macrocode}
1416 %\end{macro}
1417 %
1418 % By default, the commands for excluding previously selected
1419 % problems are disabled.
1420 %\begin{macro}{\@add@used@problem}
1421 %\changes{3.03}{2011/01/16}{new}
1422 % Adds problem to used problems list. (First argument is the label,
1423 % the second argument is the database name.)
1424 % \begin{macrocode}
1425 \newcommand*{\@add@used@problem}[2]{}
1426 % \end{macrocode}
1427 %\end{macro}
1428 %
1429 %\begin{macro}{\@fetch@excluded@list}
1430 %\changes{3.03}{2011/01/16}{new}
1431 % Fetches the excluded list. (First argument macro in which to store
1432 % the list. The second argument is the database name.)
1433 % \begin{macrocode}
1434 \newcommand*{\@fetch@excluded@list}[2]{%
1435 \def#1{}%
1436 }
1437 % \end{macrocode}
1438 %\end{macro}
1439 %
1440 %\begin{macro}{\@add@newprevlist}
1441 %\changes{3.03}{2011/01/16}{new}
1442 % Adds a new previous list for the given database:
1443 % \begin{macrocode}
1444 \newcommand*{\@add@newprevlist}[1]{}
1445 % \end{macrocode}
1446 %\end{macro}
1447 %
1448 %\begin{macro}{\close@probsoln@prev}
1449 %\changes{3.03}{2011/01/16}{new}
1450 % Close file used for previous labels
1451 % \begin{macrocode}
1452 \newcommand*{\close@probsoln@prev}{}
1453 % \end{macrocode}
1454 %\end{macro}
1455 %
1456 % At the end of the document, close file if required:
1457 % \begin{macrocode}
1458 \AtEndDocument{\close@probsoln@prev}
1459 % \end{macrocode}
1460 %
1461 %\begin{macro}{\previousproblem}
1462 %\changes{3.03}{2011/01/16}{new}
1463 % Identifies problem that has been selected and the year it was
1464 % selected. (First argument label, second argument database name,
1465 % third argument year.)
1466 % \begin{macrocode}
1467 \newcommand*{\previousproblem}[3]{%
1468 \ifnum#3>\probsoln@prev@cutoff
1469 % \end{macrocode}
1470 % If data set hasn't been defined, define it:
1471 % \begin{macrocode}
1472 \ifcsundef{prob@db@#2}{\prob@newdb{#2}}{}%
1473 % \end{macrocode}
1474 % Define command that stores the year the problem was used:
1475 % \begin{macrocode}
1476 \expandafter\gdef\csname @prev@problem@#2@#1\endcsname{#3}%
1477 % \end{macrocode}
1478 % Add label to the previous list for this data set:
1479 % \begin{macrocode}
1480 \edef\@prev@list{\csname probsoln@prev@list@#2\endcsname}%
1481 \ifdefempty{\@prev@list}%
1482 {%
1483 \expandafter\xdef\csname probsoln@prev@list@#2\endcsname{#1}%
1484 }%
1485 {%
1486 \expandafter\xdef\csname probsoln@prev@list@#2\endcsname{%
1487 \@prev@list,#1}%
1488 }%
1489 \fi
1490 }
1491 % \end{macrocode}
1492 %\end{macro}
1493 %
1494 %\begin{macro}{\usedproblem}
1495 %\changes{3.03}{2011/01/16}{new}
1496 % Don't want to exclude problems that were selected in the previous
1497 % run of this document for the current year, so they need to be
1498 % identified in the aux file.
1499 % \begin{macrocode}
1500 \newcommand*{\usedproblem}[3]{%
1501 \ifnum#3=\probsoln@startyear
1502 \expandafter\def\csname @used@problem@#2@#1\endcsname{#3}%
1503 \fi
1504 }
1505 % \end{macrocode}
1506 %\end{macro}
1507 %
1508 %\subsection{Databases}
1509 % All the problems are stored in data bases. Each data base
1510 % \meta{name} is represented as a macro \cs{prob@db@}\meta{name}
1511 % which stores a comma-separated list of labels for each problem
1512 % associated with that data base. Each problem \meta{label} is
1513 % stored in the macro
1514 % \cs{prob@data@}\meta{name}"@"\meta{label}. Problems
1515 % loaded from an external file using \cs{loadproblems} are added
1516 % to the specified data base. Any problems that are defined in the
1517 % document or are \cs{input}ed from another file (without the
1518 % use of \cs{loadproblems}) are added to the default data base.
1519 %
1520 % Define the default data base:
1521 % \begin{macrocode}
1522 \newcommand*{\prob@db@default}{}
1523 % \end{macrocode}
1524 %
1525 %\begin{macro}{\prob@databases}
1526 % Store a list of all the defined data bases.
1527 % \begin{macrocode}
1528 \newcommand*{\prob@databases}{default}
1529 % \end{macrocode}
1530 %\end{macro}
1531 % Each defined database has a list of undisplayed solutions.
1532 % \begin{macrocode}
1533 \newcommand*{\prob@db@default@solutions}{}
1534 % \end{macrocode}
1535 %
1536 %\begin{macro}{\prob@newdb}
1537 %\begin{definition}
1538 %\cs{prob@newdb}\marg{name}
1539 %\end{definition}
1540 % Creates a new (empty) data base.
1541 % \begin{macrocode}
1542 \newcommand*{\prob@newdb}[1]{%
1543 \ifcsundef{prob@db@#1}%
1544 {%
1545 \expandafter\gdef\csname prob@db@#1\endcsname{}%
1546 \xdef\prob@databases{\prob@databases,#1}%
1547 \expandafter\gdef\csname prob@db@#1@solutions\endcsname{}%
1548 \@add@newprevlist{#1}%
1549 }%
1550 {%
1551 \PackageError{probsoln}{Data set `#1' is already defined}%
1552 {Data set names must be unique}%
1553 }%
1554 }
1555 % \end{macrocode}
1556 %\end{macro}
1557 %
1558 %\begin{macro}{\prob@currentdb}
1559 % Keep a track of the current data base
1560 % \begin{macrocode}
1561 \newcommand*{\prob@currentdb}{default}
1562 % \end{macrocode}
1563 %\end{macro}
1564 %
1565 %\begin{macro}{\moveproblem}
1566 %\begin{definition}
1567 %\cs{moveproblem}\marg{label}\marg{source}\marg{target}
1568 %\end{definition}
1569 % \begin{macrocode}
1570 \newcommand{\moveproblem}[3]{%
1571 \@moveproblem{#1}{#2}{#3}%
1572 \expandafter\let\expandafter\@tmpdblist
1573 \csname prob@db@#2\endcsname
1574 \expandafter\gdef\csname prob@db@#2\endcsname{}%
1575 \@for\@tmplab:=\@tmpdblist\do{%
1576 \ifthenelse{\equal{\@tmplab}{#1}}{}{%
1577 \expandafter\ifx\csname prob@db@#2\endcsname\@empty
1578 \expandafter\xdef\csname prob@db@#2\endcsname{\@tmplab}%
1579 \else
1580 \expandafter\xdef\csname prob@db@#2\endcsname{%
1581 \csname prob@db@#2\endcsname,%
1582 \@tmplab}%
1583 \fi
1584 }%
1585 }%
1586 }
1587 % \end{macrocode}
1588 %\end{macro}
1589 %
1590 %\begin{macro}{\@moveproblem}
1591 %\begin{definition}
1592 %\cs{@moveproblem}\marg{label}\marg{source}\marg{target}
1593 %\end{definition}
1594 % Moves problem identified by \meta{label} from the data base
1595 % \meta{source} to the data base \meta{target}. (Doesn't remove
1596 % label from \meta{source} --- that needs to be done separately.)
1597 % \begin{macrocode}
1598 \newcommand*{\@moveproblem}[3]{%
1599 % \end{macrocode}
1600 % Add label to target data base
1601 % \begin{macrocode}
1602 \ifcsempty{prob@db@#3}%
1603 {%
1604 \expandafter\xdef\csname prob@db@#3\endcsname{#1}%
1605 }%
1606 {%
1607 \expandafter\xdef\csname prob@db@#3\endcsname{%
1608 \csname prob@db@#3\endcsname,#1}%
1609 }%
1610 % \end{macrocode}
1611 % Redefine \cs{prob@data@}\meta{source}"@"\meta{label} as
1612 % \cs{prob@data@}\meta{target}"@"\meta{label}.
1613 % \begin{macrocode}
1614 \edef\do@movedata{%
1615 \noexpand\global\noexpand\let\expandafter\noexpand
1616 \csname prob@data@#3@#1\endcsname=%
1617 \expandafter\noexpand\csname prob@data@#2@#1\endcsname
1618 \noexpand\global\noexpand\let
1619 \expandafter\noexpand
1620 \csname prob@data@#2@#1\endcsname=\noexpand\undefined
1621 }%
1622 \do@movedata
1623 % \end{macrocode}
1624 % Redefine \cs{prob@argN@}\meta{source}"@"\meta{label} as
1625 % \cs{prob@argN@}\meta{target}"@"\meta{label}.
1626 % \begin{macrocode}
1627 \edef\do@moveargN{%
1628 \noexpand\global\noexpand\let\expandafter\noexpand
1629 \csname prob@argN@#3@#1\endcsname=%
1630 \expandafter\noexpand\csname prob@argN@#2@#1\endcsname
1631 \noexpand\global\noexpand\let
1632 \expandafter\noexpand
1633 \csname prob@argN@#2@#1\endcsname=\noexpand\undefined
1634 }%
1635 \do@moveargN
1636 % \end{macrocode}
1637 % Redefine \cs{prob@args@}\meta{source}"@"\meta{label} as
1638 % \cs{prob@args@}\meta{target}"@"\meta{label}, if defined.
1639 % \begin{macrocode}
1640 \@ifundefined{prob@args@#2@#1}{}{%
1641 \edef\do@moveargs{%
1642 \noexpand\global\noexpand\let\expandafter\noexpand
1643 \csname prob@args@#3@#1\endcsname=%
1644 \expandafter\noexpand\csname prob@args@#2@#1\endcsname
1645 \noexpand\global\noexpand\let
1646 \expandafter\noexpand
1647 \csname prob@args@#2@#1\endcsname=\noexpand\undefined
1648 }%
1649 \do@moveargs
1650 }%
1651 }
1652 % \end{macrocode}
1653 %\end{macro}
1654 %
1655 %\subsection{Defining New Problems}
1656 %\begin{macro}{\prob@newproblem}
1657 %\changes{3.01}{2011/08/22}{added extra argument to supply defaults}
1658 %\begin{definition}
1659 %\cs{prob@newproblem}\marg{n}\marg{db
1660 %name}\marg{label}\marg{definition}\marg{default args}
1661 %\end{definition}
1662 % Define a new problem identified by \meta{label} for data base
1663 % \meta{db name} with the given definition. The problem has \meta{n}
1664 % arguments (each represented by "#1" etc). The arguments may
1665 % either be appended to \cs{useproblem}\marg{label} or may
1666 % be stored in \cs{prob@args@}\meta{db name}"@"\meta{label} if
1667 % the problem is to be accessed via \cs{foreachproblem}. The
1668 % number of arguments is stored in
1669 % \cs{prob@argN@}\meta{db name}"@"\meta{label}
1670 % \begin{macrocode}
1671 \newcommand{\prob@newproblem}[5]{%
1672 % \end{macrocode}
1673 % If the given data base is not defined, create it:
1674 % \begin{macrocode}
1675 \@ifundefined{prob@db@#2}{\prob@newdb{#2}}{}%
1676 % \end{macrocode}
1677 % Check whether this entry has already been defined:
1678 % \begin{macrocode}
1679 \@ifundefined{prob@data@#2@#3}%
1680 {%
1681 % \end{macrocode}
1682 % Define the new problem and make it global:
1683 % \begin{macrocode}
1684 \let\@tmp=\undefined
1685 \newcommand\@tmp[#1]{#4}%
1686 \expandafter\global\expandafter\let
1687 \csname prob@data@#2@#3\endcsname=\@tmp
1688 \expandafter\xdef\csname prob@argN@#2@#3\endcsname{\number#1}%
1689 \let\@tmp=\undefined
1690 % \end{macrocode}
1691 % Add the label to the data base:
1692 % \begin{macrocode}
1693 \expandafter\ifx\csname prob@db@#2\endcsname\@empty
1694 \expandafter\xdef\csname prob@db@#2\endcsname{#3}%
1695 \else
1696 \expandafter\xdef\csname prob@db@#2\endcsname{%
1697 \csname prob@db@#2\endcsname,#3}%
1698 \fi
1699 % \end{macrocode}
1700 % If default arguments supplied, set them
1701 % \begin{macrocode}
1702 \ifx\@empty#5\@empty
1703 \else
1704 \edef\thisprob@currentdb{#2}%
1705 \edef\thisprob@currentlabel{#3}%
1706 \expandafter\@setdefaultprobargs\expandafter{#5}%
1707 \fi
1708 }%
1709 {%
1710 \PackageError{probsoln}{Problem `#3' is already defined for
1711 data base `#2'}{Problem labels must be unique for each data base}%
1712 }%
1713 }
1714 % \end{macrocode}
1715 %\end{macro}
1716 %
1717 %\begin{macro}{\@prob@gobblethree}
1718 % Ignore all three arguments
1719 % \begin{macrocode}
1720 \newcommand{\@prob@gobblethree}[3]{}
1721 % \end{macrocode}
1722 %\end{macro}
1723 %\begin{macro}{\@prob@getargs}
1724 %\begin{definition}
1725 %\cs{@prob@getargs}\meta{n}\meta{db name}\meta{label}
1726 %\end{definition}
1727 % Prompt user for \meta{n} arguments for problem \meta{label}
1728 % in data base \meta{db name}.
1729 % \begin{macrocode}
1730 \newcommand{\@prob@getargs}[3]{%
1731 \message{Problem `#3' (in data base `#2') requires #1 argument(s).^^J
1732 Please specify (e.g. \csname @prob@getargs@eg@\romannumeral#1\endcsname):}%
1733 \read-1to\@tmp
1734 \expandafter\global\expandafter\let
1735 \csname prob@args@#2@#3\endcsname=\@tmp
1736 }
1737 \def\@prob@getargs@eg@i{{12}}
1738 \def\@prob@getargs@eg@ii{{5}{3}}
1739 \def\@prob@getargs@eg@iii{{4}{5}{2}}
1740 \def\@prob@getargs@eg@iv{{5}{3}{10}{8}}
1741 \def\@prob@getargs@eg@v{{5}{3}{10}{8}{4}}
1742 \def\@prob@getargs@eg@vi{{5}{3}{10}{8}{4}{24}}
1743 \def\@prob@getargs@eg@vii{{5}{3}{10}{8}{4}{24}{32}}
1744 \def\@prob@getargs@eg@viii{{5}{3}{10}{8}{4}{24}{32}{9}}
1745 \def\@prob@getargs@eg@ix{{5}{3}{10}{8}{4}{24}{32}{9}{2}}
1746 % \end{macrocode}
1747 %\end{macro}
1748 %\begin{macro}{\@prob@do@getargs}
1749 % \begin{macrocode}
1750 \let\@prob@do@getargs\@prob@gobblethree
1751 % \end{macrocode}
1752 %\end{macro}
1753 %
1754 %\begin{macro}{\setprobargs}
1755 %\begin{definition}
1756 %\cs{setprobargs}\oarg{db name}\marg{label}\marg{args}
1757 %\end{definition}
1758 % Sets the arguments for the given problem. Each of the arguments
1759 % within \meta{args} should be grouped, e.g.
1760 % "\setprobargs{label}{{5}{3}}". Inner braces are still required if
1761 % there is only one argument, e.g. "\setprobargs{label}{{25}}"
1762 % \begin{macrocode}
1763 \newcommand*{\setprobargs}[3][default]{%
1764 \expandafter\gdef\csname prob@args@#1@#2\endcsname{#3}%
1765 }
1766 % \end{macrocode}
1767 %\end{macro}
1768 %
1769 %\begin{macro}{\@setdefaultprobargs}
1770 % Like \cs{setprobargs} but only sets the arguments if
1771 % \ics{usedefaultprobargstrue}. The database is given
1772 % by \cs{thisprob@currentdb} (the current database) and
1773 % the problem label is given by \cs{thisprob@currentlabel}.
1774 % This command should only be used within \env{defproblem}.
1775 % \begin{macrocode}
1776 \newcommand*{\@setdefaultprobargs}[1]{%
1777 \ifusedefaultprobargs
1778 \setprobargs[\thisprob@currentdb]{\thisprob@currentlabel}{#1}%
1779 \fi
1780 }
1781 % \end{macrocode}
1782 %\end{macro}
1783 %
1784 %\begin{macro}{\long@collect@body}
1785 % Need long versions of \cs{collect@body}. These macros are
1786 % adapted from the macros defined by \sty{amsmath}.
1787 % \begin{macrocode}
1788 \long\def\long@collect@body#1{%
1789 \@envbody{\@xp#1\@xp{\the\@envbody}}%
1790 \edef\process@envbody{\the\@envbody\@nx\end{\@currenvir}}%
1791 \@envbody\@emptytoks \def\begin@stack{b}%
1792 \begingroup
1793 \if@prob@fragile
1794 \obeylines\obeyspaces
1795 \@makeother\#
1796 \@makeother\%
1797 \fi
1798 \@xp\let\csname\@currenvir\endcsname\long@collect@@body
1799 \edef\process@envbody{\@xp\@nx\csname\@currenvir\endcsname}%
1800 \process@envbody
1801 }
1802 % \end{macrocode}
1803 %\end{macro}
1804 %\begin{macro}{\long@addto@envbody}
1805 %\changes{3.01}{2011/08/22}{modified to allow for floats in the body of the
1806 %environment}
1807 % \begin{macrocode}
1808 \long\def\long@addto@envbody#1{%
1809 \toks@{#1}%
1810 \edef\@psn@tmp{\the\@envbody\the\toks@}%
1811 \global\@envbody\@xp{\@psn@tmp}%
1812 }
1813 % \end{macrocode}
1814 %\end{macro}
1815 %\begin{macro}{\long@collect@@body}
1816 % \begin{macrocode}
1817 \long\def\long@collect@@body#1\end#2{%
1818 % \end{macrocode}
1819 %\changes{3.01}{2011/08/22}{changed \cs{edef} to \cs{protected@edef}}
1820 % \begin{macrocode}
1821 \protected@edef\begin@stack{%
1822 \long@push@begins#1\begin\end \@xp\@gobble\begin@stack
1823 }%
1824 \ifx\@empty\begin@stack
1825 \endgroup
1826 \@checkend{#2}%
1827 \long@addto@envbody{#1}%
1828 \else
1829 \long@addto@envbody{#1\end{#2}}%
1830 \fi
1831 \process@envbody
1832 }
1833 % \end{macrocode}
1834 %\end{macro}
1835 %\begin{macro}{\long@push@begins}
1836 % \begin{macrocode}
1837 \long\def\long@push@begins#1\begin#2{%
1838 \ifx\end#2\else b\@xp\long@push@begins\fi
1839 }
1840 % \end{macrocode}
1841 %\end{macro}
1842 %
1843 % Fragile contents are sanitized, written to file and read back in.
1844 % However, we don't want the new line character sanitized. Also,
1845 % the \env{verbatim} environment doesn't like a space after
1846 % \cs{begin} or \cs{end}, so these need to be replaced.
1847 %\begin{macro}{\@char@M}
1848 %\changes{3.02}{2011/12/10}{new}
1849 % First define a macro that contains the
1850 % newline marker:
1851 % \begin{macrocode}
1852 {\obeylines
1853 \gdef\@char@M{^^M}%
1854 }
1855 \@onelevel@sanitize\@char@M
1856 % \end{macrocode}
1857 %\end{macro}
1858 %
1859 %\begin{macro}{\@beg@env@string}
1860 %\changes{3.02}{2011/12/10}{new}
1861 % Define a macro that contains the begin environment marker:
1862 % \begin{macrocode}
1863 \def\@beg@env@string{\begin}
1864 \@onelevel@sanitize\@beg@env@string
1865 % \end{macrocode}
1866 %\end{macro}
1867 %\begin{macro}{\@end@env@string}
1868 %\changes{3.02}{2011/12/10}{new}
1869 % Define a macro that contains the end environment marker:
1870 % \begin{macrocode}
1871 \def\@end@env@string{\end}
1872 \@onelevel@sanitize\@end@env@string
1873 % \end{macrocode}
1874 %\end{macro}
1875 %
1876 %\begin{macro}{\def@replace@markers}
1877 %\changes{3.02}{2011/12/10}{new}
1878 % Things start to get a bit complicated here. The substitution
1879 % macros need the markers as part of their definition. The easiest
1880 % way to do this is to define a command (using \cs{edef}) that defines the
1881 % substitution macros. The markers and replacements are the only
1882 % things to be expanded.
1883 % \begin{macrocode}
1884 \edef\def@replace@markers{%
1885 % \end{macrocode}
1886 % First define the command that replaces the newline marker:
1887 % \begin{macrocode}
1888 \noexpand\def\noexpand\do@replace@charM##1\@char@M##2\noexpand\end@replace@marker{%
1889 \noexpand\expandafter\noexpand\toks@\noexpand\expandafter{\noexpand\replace@text}%
1890 \noexpand\ifx\noexpand\relax##2\noexpand\relax
1891 \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1}%
1892 \noexpand\let\noexpand\do@replacenext\noexpand\replace@mark@noop
1893 \noexpand\else
1894 \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1^^J}%
1895 \noexpand\let\noexpand\do@replacenext\noexpand\do@replace@charM
1896 \noexpand\fi
1897 \noexpand\do@replacenext##2\noexpand\end@replace@marker
1898 }%
1899 % \end{macrocode}
1900 % Next define the command that replaces the begin environment marker:
1901 % \begin{macrocode}
1902 \noexpand\def\noexpand\doreplace@begverb##1\@beg@env@string##2\noexpand\end@replace@marker{%
1903 \noexpand\expandafter\noexpand\toks@\noexpand\expandafter{\noexpand\replace@text}%
1904 \noexpand\ifx\noexpand\relax##2\noexpand\relax
1905 \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1}%
1906 \noexpand\let\noexpand\do@replacenext\noexpand\replace@mark@noop
1907 \noexpand\else
1908 \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@
1909 ##1\expandafter\@gobble\string\\begin}%
1910 \noexpand\let\noexpand\do@replacenext\noexpand\doreplace@begverb
1911 \noexpand\fi
1912 \noexpand\do@replacenext##2\noexpand\end@replace@marker
1913 }%
1914 % \end{macrocode}
1915 % Next define the command that replaces the end environment marker:
1916 % \begin{macrocode}
1917 \noexpand\def\noexpand\doreplace@endverb##1\@end@env@string##2\noexpand\end@replace@marker{%
1918 \noexpand\expandafter\noexpand\toks@\noexpand\expandafter{\noexpand\replace@text}%
1919 \noexpand\ifx\noexpand\relax##2\noexpand\relax
1920 \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1}%
1921 \noexpand\let\noexpand\do@replacenext\noexpand\replace@mark@noop
1922 \noexpand\else
1923 \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@
1924 ##1\expandafter\@gobble\string\\end}%
1925 \noexpand\let\noexpand\do@replacenext\noexpand\doreplace@endverb
1926 \noexpand\fi
1927 \noexpand\do@replacenext##2\noexpand\end@replace@marker
1928 }%
1929 % \end{macrocode}
1930 % Finally, define a higher-level command that calls all three of the above:
1931 % \begin{macrocode}
1932 \noexpand\def\noexpand\replace@markers##1{%
1933 \noexpand\def\noexpand\replace@text{}%
1934 \noexpand\expandafter\noexpand\do@replace@charM##1\@char@M\relax\noexpand\end@replace@marker
1935 \noexpand\let##1\noexpand\replace@text
1936 \noexpand\def\noexpand\replace@text{}%
1937 \noexpand\expandafter\noexpand\doreplace@begverb##1\@beg@env@string\relax\noexpand\end@replace@marker
1938 \noexpand\let##1\noexpand\replace@text
1939 \noexpand\def\noexpand\replace@text{}%
1940 \noexpand\expandafter\noexpand\doreplace@endverb##1\@end@env@string\relax\noexpand\end@replace@marker
1941 \noexpand\let##1\noexpand\replace@text
1942 }
1943 }
1944 % \end{macrocode}
1945 % Define the substitution loop terminator:
1946 % \begin{macrocode}
1947 \def\replace@mark@noop#1\end@replace@marker{}
1948 % \end{macrocode}
1949 % Now define all the marker substitution macros:
1950 % \begin{macrocode}
1951 \def@replace@markers
1952 % \end{macrocode}
1953 %\end{macro}
1954 %
1955 %\begin{environment}{defproblem}
1956 %\changes{3.01}{2011/08/22}{added second optional argument}
1957 %\changes{3.02}{2011/12/11}{added key-val optional argument}
1958 %\begin{definition}
1959 %"\begin{defproblem}"\oarg{n}\oarg{default args}\marg{label}\oarg{key-val options}
1960 %\end{definition}
1961 % Define a new problem identified by \meta{label} with
1962 % \meta{n} arguments to add to the current data base. Note that
1963 % since the contents of the environment are passed to a command,
1964 % the contents can't contain any verbatim text.
1965 % \begin{macrocode}
1966 \newenvironment{defproblem}[1][0]{%
1967 \edef\@prob@currentargN{\number0#1}%
1968 \@defproblem@beginenv
1969 }{}
1970 % \end{macrocode}
1971 % Gather second optional argument and mandatory argument:
1972 % \begin{macrocode}
1973 \newcommand*{\@defproblem@beginenv}[2][]{%
1974 \def\@prob@currentdefaultargs{#1}%
1975 \def\@prob@currentlabel{#2}%
1976 \@defproblem@beginenv@
1977 }
1978 % \end{macrocode}
1979 % Get final optional argument and process
1980 % \begin{macrocode}
1981 \newcommand*{\@defproblem@beginenv@}[1][]{%
1982 \setkeys{probsoln}{#1}%
1983 \long@collect@body\prob@do@defproblem
1984 }
1985 % \end{macrocode}
1986 %\end{environment}
1987 %\begin{macro}{\prob@do@newproblem}
1988 %\begin{definition}
1989 %\cs{prob@do@newproblem}\marg{definition}
1990 %\end{definition}
1991 % Defines a new problem given by \meta{definition}, where
1992 % the number of arguments is given by \cs{@prob@currentargN},
1993 % the label is given by \cs{@prob@currentlabel} and the data base
1994 % is given by \cs{prob@currentdb}.
1995 % \begin{macrocode}
1996 \newcommand{\prob@do@newproblem}[1]{%
1997 \if@prob@fragile
1998 \probsoln@process@fragile{#1}%
1999 \protected@edef\do@def@new@problem{%
2000 \noexpand\prob@newproblem
2001 {\@prob@currentargN}%
2002 {\prob@currentdb}%
2003 {\@prob@currentlabel}%
2004 {%
2005 \noexpand\probsoln@write@tmp{\@prob@tmp@problem}%
2006 \noexpand\probsoln@read@tmp
2007 }%
2008 {\@prob@currentdefaultargs}%
2009 }%
2010 \else
2011 \toks@{#1}%
2012 \protected@edef\do@def@new@problem{%
2013 \noexpand\prob@newproblem
2014 {\@prob@currentargN}%
2015 {\prob@currentdb}%
2016 {\@prob@currentlabel}%
2017 {\the\toks@}%
2018 {\@prob@currentdefaultargs}%
2019 }%
2020 \fi
2021 \do@def@new@problem
2022 }
2023 % \end{macrocode}
2024 %\end{macro}
2025 %
2026 %\begin{macro}{\prob@do@defproblem}
2027 % The default action of the \env{defproblem} environment is to
2028 % define a new problem.
2029 % \begin{macrocode}
2030 \let\prob@do@defproblem=\prob@do@newproblem
2031 % \end{macrocode}
2032 %\end{macro}
2033 %
2034 %\begin{environment}{onlysolution}
2035 % Define an environment that only displays its contents if the
2036 % solutions should be displayed.
2037 % \begin{macrocode}
2038 \newenvironment{onlysolution}[1][]{%
2039 \setkeys{probsoln}{#1}%
2040 \long@collect@body\do@onlysolution
2041 }{}
2042 % \end{macrocode}
2043 %\end{environment}
2044 %\begin{macro}{\do@onlysolution}
2045 %\changes{3.02}{2011/12/10}{added \cs{probsoln@do@body}}
2046 % \begin{macrocode}
2047 \newcommand{\do@onlysolution}[1]{%
2048 \ifshowanswers
2049 \probsoln@do@body{#1}%
2050 \fi
2051 % \end{macrocode}
2052 % Add to the list of solutions
2053 % \begin{macrocode}
2054 \@ifundefined{@prob@currentlabel}
2055 {}%
2056 {%
2057 \expandafter
2058 \psn@add@unique@label
2059 \csname prob@db@\prob@currentdb @solutions\endcsname{%
2060 \@prob@currentlabel
2061 }%
2062 }%
2063 }
2064 % \end{macrocode}
2065 %\end{macro}
2066 %
2067 %\begin{macro}{\psn@add@unique@label}
2068 %\changes{3.01}{2011/08/22}{new}
2069 %\begin{definition}
2070 %\cs{psn@add@unique@label}\marg{list cs}\marg{label}
2071 %\end{definition}
2072 % Globally adds label to list if not already in list.
2073 % \begin{macrocode}
2074 \newcommand*{\psn@add@unique@label}[2]{%
2075 \ifx#1\@empty
2076 \xdef#1{#2}%
2077 \else
2078 \edef\@tmp@label{#2}%
2079 \expandafter\DTLifinlist\expandafter{\@tmp@label}{#1}%
2080 {}% ignore
2081 {\xdef#1{#1,\@tmp@label}}%
2082 \fi
2083 }
2084 % \end{macrocode}
2085 %\end{macro}
2086 %
2087 %\begin{macro}{\DTLifinlist}
2088 % This is defined in \sty{datatool}, but there's no sense loading
2089 % the entire package just for this, so define if not already
2090 % defined.
2091 %\changes{3.01}{2011/08/22}{new}
2092 % \begin{macrocode}
2093 \providecommand{\DTLifinlist}[4]{%
2094 \def\@dtl@doifinlist##1,#1,##2\end@dtl@doifinlist{%
2095 \def\@before{##1}%
2096 \def\@after{##2}%
2097 }%
2098 \expandafter\@dtl@doifinlist\expandafter,#2,#1,\@nil
2099 \end@dtl@doifinlist
2100 \ifx\@after\@nnil
2101 % not found
2102 #4%
2103 \else
2104 % found
2105 #3%
2106 \fi
2107 }
2108 % \end{macrocode}
2109 %\end{macro}
2110 %
2111 %\begin{environment}{onlyproblem}
2112 % Define an environment that only displays its contents if the
2113 % solutions should not be displayed.
2114 %\changes{3.02}{2011/12/10}{added optional argument.}
2115 % \begin{macrocode}
2116 \newenvironment{onlyproblem}[1][]{%
2117 \setkeys{probsoln}{#1}%
2118 \long@collect@body\do@onlyproblem
2119 }{}
2120 % \end{macrocode}
2121 %\end{environment}
2122 %\begin{macro}{\do@onlyproblem}
2123 %\changes{3.02}{2011/12/10}{added \cs{probsoln@do@body}}
2124 % \begin{macrocode}
2125 \newcommand{\do@onlyproblem}[1]{%
2126 \ifshowanswers
2127 \else
2128 \probsoln@do@body{#1}%
2129 \fi
2130 }
2131 % \end{macrocode}
2132 %\end{macro}
2133 %
2134 %\begin{macro}{\probsoln@do@body}
2135 % Either does argument or sanitizes, writes and reads.
2136 %\changes{3.02}{2011/12/10}{new}
2137 % \begin{macrocode}
2138 \newcommand{\probsoln@do@body}[1]{%
2139 \if@prob@fragile
2140 \probsoln@process@fragile{#1}%
2141 \probsoln@write@tmp{\@prob@tmp@problem}%
2142 \probsoln@read@tmp
2143 \else
2144 #1%
2145 \fi
2146 }
2147 % \end{macrocode}
2148 %\end{macro}
2149 %
2150 %\begin{macro}{\probsoln@process@fragile}
2151 % Sanitizes and replaces markers. Result stored in
2152 % \cs{@prob@tmp@problem}
2153 %\changes{3.02}{2011/12/10}{new}
2154 % \begin{macrocode}
2155 \newcommand{\probsoln@process@fragile}[1]{%
2156 \def\@prob@tmp@problem{#1}%
2157 \@onelevel@sanitize\@prob@tmp@problem
2158 \replace@markers\@prob@tmp@problem
2159 }
2160 % \end{macrocode}
2161 %\end{macro}
2162 %
2163 %\begin{macro}{\probsoln@write@tmp}
2164 %\changes{3.02}{2011/12/10}{new}
2165 % Writes argument to temporary file (including opening and closing
2166 % file.)
2167 % \begin{macrocode}
2168 \newcommand{\probsoln@write@tmp}[1]{%
2169 \immediate\openout\probsoln@write=\ProbSolnFragileFile.\ProbSolnFragileExt
2170 \immediate\write\probsoln@write{#1}%
2171 \immediate\closeout\probsoln@write
2172 }
2173 % \end{macrocode}
2174 %\end{macro}
2175 %
2176 %\begin{macro}{\probsoln@read@tmp}
2177 %\changes{3.02}{2011/12/10}{new}
2178 % Inputs temporary file.
2179 % \begin{macrocode}
2180 \newcommand{\probsoln@read@tmp}{%
2181 \input{\ProbSolnFragileFile.\ProbSolnFragileExt}%
2182 }
2183 % \end{macrocode}
2184 %\end{macro}
2185 %
2186 %\subsection{Using Problems}
2187 %\begin{macro}{\useproblem}
2188 %\begin{definition}
2189 %\cs{useproblem}\oarg{db name}\marg{label}\marg{arg1}\ldots
2190 %\marg{argN}
2191 %\end{definition}
2192 % Use problem identified by \meta{label} in data base
2193 % \meta{db name} where \meta{arg1}\ldots\meta{argN} are the
2194 % arguments to pass to the problem, if the problem was defined
2195 % to take arguments.
2196 %\changes{3.01}{2011/08/22}{changed \cs{@next} to \cs{@useprob@next}}
2197 % \begin{macrocode}
2198 \newcommand{\useproblem}[2][default]{%
2199 \def\@prob@currentlabel{#2}%
2200 \def\prob@currentdb{#1}%
2201 \prob@showdraftlabel{#1}{#2}%
2202 \@add@used@problem{#2}{#1}%
2203 \let\@useprob@next=\relax
2204 \@ifundefined{prob@data@#1@#2}%
2205 {%
2206 \PackageError{probsoln}%
2207 {Problem `#2' is not defined in data set `#1'}{}%
2208 }%
2209 {%
2210 \def\@useprob@next{\csname prob@data@#1@#2\endcsname}%
2211 }%
2212 \@useprob@next
2213 }
2214 % \end{macrocode}
2215 %\end{macro}
2216 %\subsection{Loading Problems From Another File}
2217 %\begin{macro}{\loadallproblems}
2218 %\begin{definition}
2219 %\cs{loadallproblems}\oarg{db name}\marg{filename}
2220 %\end{definition}
2221 % Loads all the problems defined in \meta{filename} and adds
2222 % them to data base \meta{db name}. (\cs{par} is temporarily
2223 % disabled to allow for blank lines between problems.)
2224 % \begin{macrocode}
2225 \newcommand*{\loadallproblems}[2][default]{%
2226 \bgroup
2227 \let\par\relax
2228 \edef\prob@currentdb{#1}%
2229 \input{#2}%
2230 \egroup
2231 }
2232 % \end{macrocode}
2233 %\end{macro}
2234 %\begin{macro}{\prob@do@selectedproblem}
2235 % Only define problem if the label is listed in
2236 % \cs{prob@selectedlabels}. (The current label is
2237 % given by \cs{@prob@currentlabel}.)
2238 %\changes{3.01}{2011/08/22}{replaced \cs{IFSubStringInString} with
2239 %\cs{DTLifinlist}}
2240 % \begin{macrocode}
2241 \newcommand{\prob@do@selectedproblem}[1]{%
2242 \expandafter\DTLifinlist\expandafter{\@prob@currentlabel}{\prob@selectedlabels}%
2243 {%
2244 \prob@do@newproblem{#1}%
2245 }%
2246 {}%
2247 }
2248 % \end{macrocode}
2249 %\end{macro}
2250 %
2251 %\begin{macro}{\loadselectedproblems}
2252 %\begin{definition}
2253 %\cs{loadselectedproblems}\oarg{db name}\marg{list}\marg{filename}
2254 %\end{definition}
2255 % Loads only those problems whose labels are listed in
2256 % \meta{list}.
2257 % \begin{macrocode}
2258 \newcommand{\loadselectedproblems}[3][default]{%
2259 \bgroup
2260 \let\par\relax
2261 \edef\prob@currentdb{#1}%
2262 \edef\prob@selectedlabels{#2}%
2263 \let\prob@do@defproblem=\prob@do@selectedproblem
2264 \input{#3}%
2265 \egroup
2266 }
2267 % \end{macrocode}
2268 %\end{macro}
2269 %
2270 %\begin{macro}{\prob@do@exceptedproblem}
2271 % Only define problem if the label isn't listed in
2272 % \cs{prob@selectedlabels}. (The current label is
2273 % given by \cs{@prob@currentlabel}.)
2274 % \begin{macrocode}
2275 \newcommand{\prob@do@exceptedproblem}[1]{%
2276 \expandafter\DTLifinlist\expandafter{\@prob@currentlabel}{\prob@selectedlabels}%
2277 {}%
2278 {%
2279 \prob@do@newproblem{#1}%
2280 }%
2281 }
2282 % \end{macrocode}
2283 %\end{macro}
2284 %
2285 %\begin{macro}{\loadexceptproblems}
2286 %\begin{definition}
2287 %\cs{loadexceptproblems}\oarg{db name}\marg{list}\marg{filename}
2288 %\end{definition}
2289 % Loads only those problems whose labels are not listed in
2290 % \meta{list}.
2291 %\changes{3.01}{2011/08/22}{new}
2292 % \begin{macrocode}
2293 \newcommand{\loadexceptproblems}[3][default]{%
2294 \bgroup
2295 \let\par\relax
2296 \edef\prob@currentdb{#1}%
2297 \edef\prob@selectedlabels{#2}%
2298 \let\prob@do@defproblem=\prob@do@exceptedproblem
2299 \input{#3}%
2300 \egroup
2301 }
2302 % \end{macrocode}
2303 %\end{macro}
2304 %
2305 %\begin{macro}{\prob@add@currentlabel}
2306 % Adds the current label to \cs{prob@selectedlabels} (ignores
2307 % argument.)
2308 % \begin{macrocode}
2309 \newcommand{\prob@add@currentlabel}[1]{%
2310 \ifx\prob@selectedlabels\@empty
2311 \xdef\prob@selectedlabels{\@prob@currentlabel}%
2312 \else
2313 \xdef\prob@selectedlabels{\prob@selectedlabels,\@prob@currentlabel}%
2314 \fi
2315 }
2316 % \end{macrocode}
2317 %\end{macro}
2318 %\begin{macro}{\iffirstpass}
2319 % Determines if this is the first pass of the filename when loading
2320 % a data base.
2321 % \begin{macrocode}
2322 \newif\iffirstpass
2323 \firstpasstrue
2324 % \end{macrocode}
2325 %\end{macro}
2326 %\begin{macro}{\loadrandomproblems}
2327 %\begin{definition}
2328 %\cs{loadrandomproblems}\oarg{db name}\marg{n}\marg{filename}
2329 %\end{definition}
2330 % Loads \meta{n} randomly selected problems from \meta{filename}.
2331 %\changes{3.03}{2011/01/16}{fetch exclusion list}
2332 % \begin{macrocode}
2333 \newcommand{\loadrandomproblems}[3][default]{%
2334 \@fetch@excluded@list{\@excl@list}{#1}%
2335 \@loadrandomproblems{#1}{#2}{#3}{\@excl@list}%
2336 }
2337 % \end{macrocode}
2338 %\end{macro}
2339 %\begin{macro}{\loadrandomexcept}
2340 %\begin{definition}
2341 %\cs{loadrandomexcept}\oarg{db
2342 %name}\marg{n}\marg{filename}\marg{exception list}
2343 %\end{definition}
2344 % Loads \meta{n} randomly selected problems from \meta{filename},
2345 % excluding labels listed in \meta{exception list}.
2346 % \begin{macrocode}
2347 \newcommand{\loadrandomexcept}[4][default]{%
2348 \@loadrandomproblems{#1}{#2}{#3}{#4}%
2349 }
2350 % \end{macrocode}
2351 %\end{macro}
2352 %\begin{macro}{\@loadrandomproblems}
2353 % Internal workings of \cs{loadrandomproblems} and
2354 % \cs{loadrandomexcept}.
2355 % Needs to input \meta{filename} twice: the first time just gathers
2356 % all the labels, the second time only loads the selected problems.
2357 % \begin{macrocode}
2358 \newcommand{\@loadrandomproblems}[4]{%
2359 \bgroup
2360 \let\par\relax
2361 \def\prob@db@reserved{}%
2362 \def\prob@currentdb{reserved}%
2363 \edef\prob@selectedlabels{}%
2364 % \end{macrocode}
2365 % Collect labels:
2366 % \begin{macrocode}
2367 \let\prob@do@defproblem=\prob@add@currentlabel
2368 \firstpasstrue
2369 % \end{macrocode}
2370 %\changes{3.05}{2017/07/10}{allow comma-separated list of file names}
2371 %Allow a comma-separated list of file names.
2372 % \begin{macrocode}
2373 \@for\@thisfile:=#3\do{\input{\@thisfile}}%
2374 % \end{macrocode}
2375 % Shuffle labels.
2376 % \begin{macrocode}
2377 \@probselN=0\relax
2378 \@for\@thislabel:=\prob@selectedlabels\do{%
2379 % \end{macrocode}
2380 %\changes{3.03}{2011/01/16}{ensure arguments expanded in
2381 %\cs{DTLifinlist}}
2382 % \begin{macrocode}
2383 \edef\@if@in@list{\noexpand\DTLifinlist{\@thislabel}{#4}}%
2384 \@if@in@list
2385 {}%
2386 {%
2387 \advance\@probselN by 1\relax
2388 \expandafter
2389 \edef\csname @prob@tmp@\romannumeral\@probselN\endcsname{%
2390 \@thislabel}%
2391 }%
2392 }%
2393 % \end{macrocode}
2394 %\changes{3.03}{2012/01/16}{added exclusion list to warning messages}
2395 % \begin{macrocode}
2396 \edef\@do@excludedlist{#4}%
2397 \ifdefempty{\@do@excludedlist}%
2398 {}%
2399 {\def\@do@excludedlist{(Excluded list: #4.) }}%
2400 % \end{macrocode}
2401 %\changes{3.03}{2012/01/16}{added check for 0 available problems}
2402 % \begin{macrocode}
2403 \ifnum\@probselN=0\relax
2404 \PackageWarning{probsoln}{You have requested
2405 \number#2\space\space problem(s) but there are no available
2406 problems in `#3'. \@do@excludedlist No problems will be selected}%
2407 \else
2408 \shuffle{@prob@tmp@}{\@probselN}%
2409 \ifnum\@probselN<#2\relax
2410 % \end{macrocode}
2411 %\changes{3.01}{2011/08/22}{changed error to warning}
2412 % \begin{macrocode}
2413 \ifnum\@probselN=1\relax
2414 \PackageWarning{probsoln}{You have requested
2415 \number#2\space\space problem(s) but there is only
2416 1 problem available in `#3'. \@do@excludedlist
2417 Only 1 problem will be selected}%
2418 \else
2419 \PackageWarning{probsoln}{You have requested
2420 \number#2\space\space problem(s) but there are only
2421 \number\@probselN\space problems available in `#3'.
2422 \@do@excludedlist
2423 Only \number\@probselN\space problems will be selected}%
2424 \fi
2425 \else
2426 \@probselN=#2\relax
2427 \fi
2428 \fi
2429 % \end{macrocode}
2430 % Store only the first \meta{n} of the shuffled labels.
2431 % \begin{macrocode}
2432 \@probN=0\relax
2433 \def\prob@selectedlabels{}%
2434 \whiledo{\@probN<\@probselN}{%
2435 \advance\@probN by 1\relax
2436 \ifx\prob@selectedlabels\@empty
2437 \edef\prob@selectedlabels{%
2438 \csname @prob@tmp@\romannumeral\@probN\endcsname}%
2439 \else
2440 \edef\prob@selectedlabels{%
2441 \prob@selectedlabels,%
2442 \csname @prob@tmp@\romannumeral\@probN\endcsname}%
2443 \fi
2444 }%
2445 % \end{macrocode}
2446 % Only load selected labels.
2447 % \begin{macrocode}
2448 \let\prob@do@defproblem=\prob@do@selectedproblem
2449 \firstpassfalse
2450 \@for\@thisfile:=#3\do{\input{\@thisfile}}%
2451 % \end{macrocode}
2452 % Move them from the reserved data base into the required data base
2453 % in the order specified by \cs{prob@selectedlabels}
2454 % \begin{macrocode}
2455 \@ifundefined{prob@db@#1}{\prob@newdb{#1}}{}%
2456 \@for\@thislabel:=\prob@selectedlabels\do{%
2457 \@moveproblem{\@thislabel}{reserved}{#1}%
2458 }%
2459 % \end{macrocode}
2460 % \cs{prob@selectedlabels} had to be globally defined. It's no
2461 % longer required to undefine it.
2462 % \begin{macrocode}
2463 \let\prob@selectedlabels=\undefined
2464 \egroup
2465 }
2466 % \end{macrocode}
2467 %\end{macro}
2468 %
2469 %\subsection{Iterating Through a Data Base}
2470 %\begin{macro}{\foreachproblem}
2471 %\begin{definition}
2472 %\cs{foreachproblem}\oarg{db name}\marg{body}
2473 %\end{definition}
2474 % Does \meta{body} for each problem defined in the data base
2475 % \meta{db name}. Within \meta{body}, the command \cs{thisproblem}
2476 % can be used to do the current problem and the command
2477 % \cs{thisproblemlabel} can be used to access the current label.
2478 % \begin{macrocode}
2479 \newcommand{\foreachproblem}[2][default]{%
2480 \@ifundefined{prob@db@#1}{%
2481 \PackageError{probsoln}{Data base `#1' is not defined}{}%
2482 }{%
2483 \expandafter\let\expandafter\@tmp\csname prob@db@#1\endcsname
2484 \@for\thisproblemlabel:=\@tmp\do{%
2485 \expandafter\ifnum
2486 \csname prob@argN@#1@\thisproblemlabel\endcsname>0\relax
2487 \@ifundefined{prob@args@#1@\thisproblemlabel}{%
2488 \expandafter\@prob@getargs
2489 \csname prob@argN@#1@\thisproblemlabel\endcsname
2490 {#1}{\thisproblemlabel}}{}%
2491 \expandafter\let\expandafter\thisproblemargs
2492 \csname prob@args@#1@\thisproblemlabel\endcsname
2493 \else
2494 \let\thisproblemargs\@empty
2495 \fi
2496 \expandafter\toks@\expandafter{\thisproblemargs}%
2497 \edef\thisproblem{\noexpand\useproblem[#1]{\thisproblemlabel}%
2498 \the\toks@}%
2499 #2%
2500 }%
2501 }%
2502 }
2503 % \end{macrocode}
2504 %\end{macro}
2505 %
2506 %\begin{macro}{\foreachsolution}
2507 %\begin{definition}
2508 %\cs{foreachsolution}\oarg{db name}\marg{body}
2509 %\end{definition}
2510 % Does \meta{body} for each problem defined in the data base
2511 % \meta{db name} that has a solution contained within
2512 % \env{onlysolution}. Within \meta{body}, the command \cs{thisproblem}
2513 % can be used to do the current problem and the command
2514 % \cs{thisproblemlabel} can be used to access the current label.
2515 % \begin{macrocode}
2516 \newcommand{\foreachsolution}[2][default]{%
2517 \@ifundefined{prob@db@#1}{%
2518 \PackageError{probsoln}{Data base `#1' is not defined}{}%
2519 }{%
2520 \expandafter\let\expandafter\@tmp\csname prob@db@#1@solutions\endcsname
2521 \@for\thisproblemlabel:=\@tmp\do{%
2522 \ifx\thisproblemlabel\@empty
2523 \else
2524 \expandafter\ifnum
2525 \csname prob@argN@#1@\thisproblemlabel\endcsname>0\relax
2526 \@ifundefined{prob@args@#1@\thisproblemlabel}{%
2527 \expandafter\@prob@getargs
2528 \csname prob@argN@#1@\thisproblemlabel\endcsname
2529 {#1}{\thisproblemlabel}}{}%
2530 \expandafter\let\expandafter\thisproblemargs
2531 \csname prob@args@#1@\thisproblemlabel\endcsname
2532 \else
2533 \let\thisproblemargs\@empty
2534 \fi
2535 \expandafter\toks@\expandafter{\thisproblemargs}%
2536 \edef\thisproblem{\noexpand\useproblem[#1]{\thisproblemlabel}%
2537 \the\toks@}%
2538 #2%
2539 \fi
2540 }%
2541 }%
2542 }
2543 % \end{macrocode}
2544 %\end{macro}
2545 %
2546 %\begin{macro}{\foreachdataset}
2547 %\begin{definition}
2548 %\cs{foreachdataset}\marg{cmd}\marg{body}
2549 %\end{definition}
2550 % Iterates through all defined data sets. Assigns \meta{cmd} to the
2551 % name of the data base.
2552 % \begin{macrocode}
2553 \newcommand{\foreachdataset}[2]{%
2554 \@for#1:=\prob@databases\do{#2}}
2555 % \end{macrocode}
2556 %\end{macro}
2557 %
2558 %\subsection{Random Numbers}
2559 % First define some registers for later use.
2560 % \begin{macrocode}
2561 \newcount\@probN \newcount\@probselN \newcount\@rndselctr
2562 \newcount\r@ndcur
2563 \newcount\@ps@randtmp
2564 \r@ndcur=1\relax
2565 % \end{macrocode}
2566 %\begin{macro}{\PSNrandseed}
2567 % Set the random generator seed
2568 % \begin{macrocode}
2569 \newcommand*{\PSNrandseed}[1]{%
2570 \ifnum#1=0\relax
2571 \PackageWarning{probsoln}{Can't have 0 as random seed, changing to 1}%
2572 \global\r@ndcur=1\relax
2573 \else
2574 \global\r@ndcur=#1\relax
2575 \fi
2576 \PackageInfo{probsoln}{Random Seed = \number\r@ndcur}%
2577 }
2578 % \end{macrocode}
2579 %\end{macro}
2580 %\begin{macro}{\PSNgetrandseed}
2581 % \begin{macrocode}
2582 \newcommand*{\PSNgetrandseed}[1]{#1=\r@ndcur\relax}
2583 % \end{macrocode}
2584 %\end{macro}
2585 %\begin{macro}{\PSNrand}
2586 % Generate a random integer.
2587 % \begin{macrocode}
2588 \newcommand*{\PSNrand}{%
2589 \@ps@randtmp=\r@ndcur
2590 \multiply\@ps@randtmp by 16811\relax
2591 \r@ndcur=\@ps@randtmp
2592 \global\divide\r@ndcur by 39989\relax
2593 \global\multiply\r@ndcur by 39989\relax
2594 \advance\@ps@randtmp by -\r@ndcur
2595 \global\r@ndcur = \@ps@randtmp
2596 \ifnum\r@ndcur=0\relax
2597 \global\r@ndcur=1\relax
2598 \fi
2599 }
2600 % \end{macrocode}
2601 %\end{macro}
2602 %\begin{macro}{\PSN@old@rand}
2603 % Random generator used in v3.0 and earlier
2604 % \begin{macrocode}
2605 \newcommand*{\PSN@old@rand}{%
2606 \@ps@randtmp=\r@ndcur
2607 \multiply\@ps@randtmp by 16807\relax
2608 \r@ndcur=\@ps@randtmp
2609 \global\divide\r@ndcur by 120001\relax
2610 \global\multiply\r@ndcur by 120001\relax
2611 \advance\@ps@randtmp by -\r@ndcur
2612 \global\r@ndcur = \@ps@randtmp
2613 \ifnum\r@ndcur=0\relax
2614 \global\r@ndcur=1\relax
2615 \fi
2616 }
2617 % \end{macrocode}
2618 %\end{macro}
2619 %\begin{macro}{\PSNuseoldrandom}
2620 % Use the old random number generator
2621 % \begin{macrocode}
2622 \newcommand*{\PSNuseoldrandom}{%
2623 \let\PSNrand\PSN@old@rand
2624 }
2625 % \end{macrocode}
2626 %\end{macro}
2627 %
2628 %\begin{macro}{\PSNrandom}
2629 %\begin{definition}
2630 %\cs{PSNrandom}\marg{count}\marg{n}
2631 %\end{definition}
2632 % stores a random number from 1 to \meta{n}
2633 % in the TeX count register \meta{count}
2634 % \begin{macrocode}
2635 \newcommand{\PSNrandom}[2]{%
2636 % \end{macrocode}
2637 % generate new random number.
2638 % \begin{macrocode}
2639 \PSNrand
2640 #1=\r@ndcur
2641 \@ps@randtmp=\r@ndcur
2642 % \end{macrocode}
2643 % now set \meta{count} to (\meta{count} mod \meta{n}) + 1
2644 % \begin{macrocode}
2645 \divide\@ps@randtmp by #2\relax
2646 \multiply\@ps@randtmp by #2\relax
2647 \advance#1 by -\@ps@randtmp
2648 \advance#1 by 1\relax
2649 }
2650 % \end{macrocode}
2651 %\end{macro}
2652 %\begin{macro}{\random}
2653 %\begin{definition}
2654 % \cs{random}\marg{counter}\marg{a}\marg{b}
2655 %\end{definition}
2656 % Generate a random number in the range $[a,b]$, and store this
2657 % number in the \LaTeX\ counter \meta{counter}.
2658 % \begin{macrocode}
2659 \newcommand{\random}[3]{%
2660 \ifnum#2=1\relax
2661 \PSNrandom{\value{#1}}{#3}%
2662 \else
2663 \@rndselctr=#3%
2664 \advance\@rndselctr by -#2\relax
2665 \advance\@rndselctr by 1\relax
2666 \PSNrandom{\value{#1}}{\@rndselctr}%
2667 \addtocounter{#1}{#2}%
2668 \addtocounter{#1}{-1}%
2669 \fi
2670 }
2671 % \end{macrocode}
2672 %\end{macro}
2673 %\begin{macro}{\shuffle}
2674 % Shuffle contents of pseudo-array. For example, suppose you have
2675 % the following definitions: "\def\fooi{A}", "\def\fooii{B}" and
2676 % "\def\fooiii{C}", then "\shuffle{foo}{3}" will shuffle the
2677 % definitions, so you may end up with, e.g.\ "\def\fooi{C}",
2678 % "\def\fooii{A}", "\def\fooiii{B}", or some other variation.
2679 % \begin{macrocode}
2680 \newcount\@shfctr \newcount\@shfA \newcount\@shfB
2681 \newcommand{\shuffle}[2]{%
2682 \@shfctr=1\relax
2683 \whiledo{\@shfctr < 101}%
2684 {%
2685 \PSNrandom{\@shfA}{#2}\PSNrandom{\@shfB}{#2}%
2686 \ifnum\@shfA=\@shfB
2687 \else
2688 \edef\@@tmpA{\csname#1\romannumeral\@shfA\endcsname}%
2689 \let\@tmpA=\@@tmpA
2690 \edef\@@tmpB{\csname#1\romannumeral\@shfB\endcsname}%
2691 \let\@tmpB=\@@tmpB
2692 \expandafter\xdef\csname#1\romannumeral\@shfA\endcsname{\@tmpB}%
2693 \expandafter\xdef\csname#1\romannumeral\@shfB\endcsname{\@tmpA}%
2694 \fi
2695 \advance\@shfctr by 1\relax
2696 }%
2697 }
2698 % \end{macrocode}
2699 %\end{macro}
2700 %\begin{macro}{\doforrandN}
2701 %\begin{definition}
2702 % \cs{doforrandN}\marg{n}\marg{cmd}\marg{list}\marg{text}.
2703 %\end{definition}
2704 % A bit like \cs{@for} but only for a random subset of the given
2705 % list.
2706 % For example, the following will load one problem each from two
2707 % out of the three listed files.
2708 %\begin{verbatim}
2709 %\doforrandN{2}{\tmp}{file1,file2,file3}{%
2710 %\loadrandomproblems{1}{\tmp}}
2711 %\end{verbatim}
2712 %
2713 %\changes{3.03}{}{added count register to keep track of level to
2714 %allow for nested loops}
2715 % \begin{macrocode}
2716 \newcount\@ps@forrand
2717 \newcount\@ps@forrand@level
2718 \newcommand{\doforrandN}[4]{%
2719 \global\advance\@ps@forrand@level by 1\relax
2720 {%
2721 \@ps@forrand=0\relax
2722 \@for#2:=#3\do{%
2723 \advance\@ps@forrand by 1\relax
2724 \expandafter
2725 \edef\csname @doforrandN@\number\@ps@forrand@level @\romannumeral\@ps@forrand\endcsname{#2}%
2726 }%
2727 \ifnum\@ps@forrand<#1\relax
2728 \PackageError{probsoln}{Can't randomly select \number#1\space item(s)}{You
2729 have requested \number#1\space item(s), but there
2730 are only \number\@ps@forrand\space item(s) in the list: #3}%
2731 \else
2732 \shuffle{@doforrandN@\number\@ps@forrand@level @}{\@ps@forrand}%
2733 \ifnum#1>0\relax
2734 \@ps@forrand=0\relax
2735 \loop
2736 \advance\@ps@forrand by 1\relax
2737 \edef#2{\csname @doforrandN@\number\@ps@forrand@level @\romannumeral\@ps@forrand\endcsname}%
2738 #4%
2739 \ifnum\@ps@forrand<#1\relax
2740 \repeat
2741 \fi
2742 \fi
2743 }%
2744 \global\advance\@ps@forrand@level by -1\relax
2745 }
2746 % \end{macrocode}
2747 %\end{macro}
2748 %\subsection{Compatibility With Older Versions}
2749 % These commands ensure that this version is compatible with
2750 % versions prior to v3.0
2751 %\begin{macro}{\newproblem}
2752 %\changes{3.01}{2011/08/22}{added second optional argument}
2753 % Defines a new problem.
2754 % \begin{macrocode}
2755 \newcommand*{\newproblem}{\@ifstar\@snewproblem\@newproblem}
2756 % \end{macrocode}
2757 %\end{macro}
2758 %\begin{macro}{\@snewproblem}
2759 % Store first optional argument
2760 % \begin{macrocode}
2761 \newcommand{\@snewproblem}[1][0]{%
2762 \def\@newprob@argN{#1}%
2763 \@s@newproblem
2764 }%
2765 % \end{macrocode}
2766 %\end{macro}
2767 %\begin{macro}{\@s@newproblem}
2768 % Define a new problem without a solution
2769 % \begin{macrocode}
2770 \newcommand{\@s@newproblem}[3][]{%
2771 \begin{defproblem}[\@newprob@argN][#1]{#2}%
2772 #3%
2773 \end{defproblem}%
2774 }
2775 % \end{macrocode}
2776 %\end{macro}
2777 %\begin{macro}{\@newproblem}
2778 % Store first optional argument
2779 % \begin{macrocode}
2780 \newcommand{\@newproblem}[1][0]{%
2781 \def\@newprob@argN{#1}%
2782 \@ns@newproblem
2783 }
2784 % \end{macrocode}
2785 %\end{macro}
2786 %\begin{macro}{\@ns@newproblsm}
2787 % Define problem with a solution:
2788 % \begin{macrocode}
2789 \newcommand{\@ns@newproblem}[4][]{%
2790 \begin{defproblem}[\@newprob@argN][#1]{#2}%
2791 #3%
2792 \begin{onlysolution}%
2793 \begin{solution}%
2794 #4%
2795 \end{solution}%
2796 \end{onlysolution}%
2797 \end{defproblem}%
2798 }
2799 % \end{macrocode}
2800 %\end{macro}
2801 %
2802 %\begin{macro}{\selectallproblems}
2803 % \begin{macrocode}
2804 \newcommand*{\selectallproblems}[1]{{\loadallproblems[#1]{#1}}%
2805 \foreachproblem[#1]{\PSNitem\thisproblem\endPSNitem}}
2806 % \end{macrocode}
2807 %\end{macro}
2808 %\begin{macro}{\selectrandomly}
2809 % \begin{macrocode}
2810 \newcommand*{\selectrandomly}[2]{%
2811 {\loadrandomproblems[#1]{#2}{#1}}%
2812 \foreachproblem[#1]{\PSNitem\thisproblem\endPSNitem}%
2813 }
2814 % \end{macrocode}
2815 %\end{macro}
2816 %\begin{environment}{PSNitem}
2817 % \begin{macrocode}
2818 \newenvironment{PSNitem}{\item}{}
2819 % \end{macrocode}
2820 %\end{environment}
2821 %
2822 %\subsection{Formatting Commands}
2823 % These commands are provided to format parts of the
2824 % problems/solutions.
2825 %\begin{environment}{solution}
2826 %\changes{3.01}{2011/08/22}{added space after solution tag}
2827 % \begin{macrocode}
2828 \@ifundefined{solution}{%
2829 \newenvironment{solution}{\par\noindent\textbf{\solutionname:}\space
2830 \ignorespaces}{}%
2831 }{}
2832 % \end{macrocode}
2833 %\end{environment}
2834 %\begin{macro}{\solutionname}
2835 % \begin{macrocode}
2836 \newcommand*{\solutionname}{Solution}
2837 % \end{macrocode}
2838 %\end{macro}
2839 % Define an in-line enumeration which uses the enumeration
2840 % environment's counters.
2841 %\begin{environment}{textenum}
2842 %\changes{2007 Oct 6}{2.05}{new}
2843 %\changes{2012/08/23}{3.04}{fixed bug that interfered with depth counter}
2844 % \begin{macrocode}
2845 \newenvironment{textenum}{%
2846 \ifnum\@enumdepth>\thr@@
2847 \@toodeep
2848 \else
2849 \advance\@enumdepth by 1\relax
2850 \edef\@enumctr{enum\romannumeral\the\@enumdepth}%
2851 \let\@item\@textitem
2852 \def\@itemlabel{%
2853 \refstepcounter{\@enumctr}%
2854 \csname label\@enumctr\endcsname
2855 }%
2856 \setcounter{\@enumctr}{0}%
2857 \fi
2858 \ignorespaces
2859 }%
2860 {%
2861 }
2862 % \end{macrocode}
2863 %\end{environment}
2864 %\begin{macro}{\@textitem}
2865 % In-line enumeration item
2866 % \begin{macrocode}
2867 \def\@textitem[#1]{#1\space\ignorespaces}
2868 % \end{macrocode}
2869 %\end{macro}
2870 %\begin{macro}{\correctitemformat}
2871 % \changes{2007 Oct 6}{2.05}{new}
2872 % Indicates how to format the item label for \cs{correctitem} when
2873 % the solutions are shown. The argument is the label. This
2874 % defaults to placing the argument in a box.
2875 % \begin{macrocode}
2876 \newcommand*{\correctitemformat}[1]{\fbox{#1}}
2877 % \end{macrocode}
2878 %\end{macro}
2879 %\begin{macro}{\incorrectitemformat}
2880 % \changes{2007 Oct 6}{2.05}{new}
2881 % Indicates how to format the item label for \cs{incorrectitem} when
2882 % the solutions are shown. The argument is the label. This
2883 % defaults to just the argument shifted
2884 % by \cs{fboxsep} + \cs{fboxrule} to ensure it aligns
2885 % with the default \cs{correctitemformat}.
2886 % \begin{macrocode}
2887 \newcommand*{\incorrectitemformat}[1]{%
2888 \hspace{\fboxsep}\hspace{\fboxrule}#1}
2889 % \end{macrocode}
2890 %\end{macro}
2891 %
2892 %\begin{macro}{\correctitem}
2893 % \changes{2007 Oct 6}{2.05}{new}
2894 %This can be used instead of \cs{item}. If the solutions are
2895 % not shown, it behaves like \cs{item}, otherwise, it's like
2896 % \cs{item}, but the label is formatted according to
2897 % \cs{correctitemformat}.
2898 % \begin{macrocode}
2899 \newcommand*{\correctitem}{\@inmatherr\correctitem
2900 \@ifnextchar[\@correctitem{\@noitemargtrue\@correctitem[\@itemlabel]}}
2901 % \end{macrocode}
2902 % \begin{macrocode}
2903 \def\@correctitem[#1]{%
2904 \ifshowanswers
2905 \@item[\correctitemformat{#1}]%
2906 \else
2907 \@item[#1]%
2908 \fi}
2909 % \end{macrocode}
2910 %\end{macro}
2911 %
2912 %\begin{macro}{\incorrectitem}
2913 % \changes{2007 Oct 6}{2.05}{new}
2914 %This can be used instead of \cs{item}. If the solutions are
2915 % not shown, it behaves like \cs{item}, otherwise, it's like
2916 % \cs{item}, but the label is formatted according to
2917 % \cs{incorrectitemformat}.
2918 % \begin{macrocode}
2919 \newcommand*{\incorrectitem}{\@inmatherr\incorrectitem
2920 \@ifnextchar[\@incorrectitem{\@noitemargtrue\@incorrectitem[\@itemlabel]}}
2921 % \end{macrocode}
2922 % \begin{macrocode}
2923 \def\@incorrectitem[#1]{%
2924 \ifshowanswers
2925 \@item[\incorrectitemformat{#1}]%
2926 \else
2927 \@item[#1]%
2928 \fi}
2929 % \end{macrocode}
2930 %\end{macro}
2931 %\iffalse
2932 % \begin{macrocode}
2933 %</probsoln.sty>
2934 % \end{macrocode}
2935 %\fi
2936 %\iffalse
2937 % \begin{macrocode}
2938 %<*sample-exclude.tex>
2939 % \end{macrocode}
2940 %\fi
2941 %\iffalse
2942 % \begin{macrocode}
2943 % This file is public domain
2944 \documentclass[a4paper]{article}
2945
2946 \usepackage{probsoln}
2947 %uncomment the following line to display the answers
2948 %\showanswers
2949
2950 % Set the academic year to be 2011/12 (if omitted assume current
2951 % academic year):
2952 \SetStartYear{2011}
2953
2954 % Set the seed for the random number generator:
2955 \PSNrandseed{\GetStartYear}
2956
2957 % Exclude problems that have been used this year or the previous
2958 % two academic years. (Creates a file called 'excluded.tex' to
2959 % store labels of used problems. Also creates a file called
2960 % \jobname.prb that stores labels of problems used in this
2961 % document, so they don't get excluded on subsequent runs.)
2962 \ExcludePreviousFile[3]{excluded}
2963
2964 % To clear the used problems file (\jobname.prb) of the labels
2965 % generated in the previous run, comment the above and uncomment
2966 % below:
2967 % \ClearUsedFile{excluded}
2968
2969 % Load 1 problem randomly selected from the given file
2970 % and store in database 'firstprinciples':
2971 \loadrandomproblems[firstprinciples]{1}{prob-1stprncp}
2972
2973 % Load 5 problems randomly selected from the given file
2974 % and store in database 'easy':
2975 \loadrandomproblems[easy]{5}{prob-easy}
2976
2977 % Load 2 problems randomly selected from the given file
2978 % and store in database 'implicit':
2979 \loadrandomproblems[implicit]{2}{prob-implicit}
2980
2981 % Load 2 problems randomly selected from the given file
2982 % and store in database 'probspaces':
2983 \loadrandomproblems[probspaces]{2}{prob-probspaces}
2984
2985 % Load 1 problem randomly selected from the given file
2986 % and store in database 'mchoice':
2987 \loadrandomproblems[mchoice]{1}{prob-mchoice}
2988
2989 % Load 1 problem randomly selected from the given file
2990 % and store in database 'nosoln':
2991 \loadrandomproblems[nosoln]{1}{prob-nosoln}
2992
2993
2994 \begin{document}
2995 \title{Sample Problem Sheet}
2996 \author{Nicola Talbot}
2997 \maketitle
2998
2999 \begin{enumerate}
3000 % Differentiation from first principles
3001 \foreachproblem[firstprinciples]{\item \thisproblem}
3002
3003 \item Differentiate the following functions:
3004 \begin{enumerate}
3005 \foreachproblem[easy]{\item \thisproblem}
3006 \end{enumerate}
3007
3008 % Implicit differentiation
3009 \foreachproblem[implicit]{\item \thisproblem}
3010
3011 % Finite probability spaces
3012 \foreachproblem[probspaces]{\item \thisproblem}
3013
3014 % Multiple choice question
3015 \foreachproblem[mchoice]{\item \thisproblem}
3016
3017 % select one essay style question that doesn't come with a solution
3018
3019 \foreachproblem[nosoln]{\item \thisproblem}
3020 \end{enumerate}
3021 \end{document}
3022 % \end{macrocode}
3023 %\fi
3024 %\iffalse
3025 % \begin{macrocode}
3026 %</sample-exclude.tex>
3027 % \end{macrocode}
3028 %\fi
3029 %\iffalse
3030 % \begin{macrocode}
3031 %<*sample.tex>
3032 % \end{macrocode}
3033 %\fi
3034 %\iffalse
3035 % \begin{macrocode}
3036 % This file is public domain
3037 \documentclass[a4paper]{article}
3038
3039 \usepackage{probsoln}
3040 % uncomment the following line to display the answers
3041 % \showanswers
3042
3043 % The following will generate a different set of
3044 % problems each year
3045 %\PSNrandseed{\year}
3046
3047 % Load 1 problem randomly selected from the given file
3048 % and store in database 'firstprinciples':
3049 \loadrandomproblems[firstprinciples]{1}{prob-1stprncp}
3050
3051 % Load 5 problems randomly selected from the given file
3052 % and store in database 'easy':
3053 \loadrandomproblems[easy]{5}{prob-easy}
3054
3055 % Load 2 problems randomly selected from the given file
3056 % and store in database 'implicit':
3057 \loadrandomproblems[implicit]{2}{prob-implicit}
3058
3059 % Load 2 problems randomly selected from the given file
3060 % and store in database 'probspaces':
3061 \loadrandomproblems[probspaces]{2}{prob-probspaces}
3062
3063 % Load 1 problem randomly selected from the given file
3064 % and store in database 'mchoice':
3065 \loadrandomproblems[mchoice]{1}{prob-mchoice}
3066
3067 % Load 1 problem randomly selected from the given file
3068 % and store in database 'nosoln':
3069 \loadrandomproblems[nosoln]{1}{prob-nosoln}
3070
3071
3072 \begin{document}
3073 \title{Sample Problem Sheet}
3074 \author{Nicola Talbot}
3075 \maketitle
3076
3077 \begin{enumerate}
3078 % Differentiation from first principles
3079 \foreachproblem[firstprinciples]{\item \thisproblem}
3080
3081 \item Differentiate the following functions:
3082 \begin{enumerate}
3083 \foreachproblem[easy]{\item \thisproblem}
3084 \end{enumerate}
3085
3086 % Implicit differentiation
3087 \foreachproblem[implicit]{\item \thisproblem}
3088
3089 % Finite probability spaces
3090 \foreachproblem[probspaces]{\item \thisproblem}
3091
3092 % Multiple choice question
3093 \foreachproblem[mchoice]{\item \thisproblem}
3094
3095 % select one essay style question that doesn't come with a solution
3096
3097 \foreachproblem[nosoln]{\item \thisproblem}
3098 \end{enumerate}
3099 \end{document}
3100 % \end{macrocode}
3101 %\fi
3102 %\iffalse
3103 % \begin{macrocode}
3104 %</sample.tex>
3105 % \end{macrocode}
3106 %\fi
3107 %\iffalse
3108 % \begin{macrocode}
3109 %<*sample2.tex>
3110 % \end{macrocode}
3111 %\fi
3112 %\iffalse
3113 % \begin{macrocode}
3114 % This file is public domain
3115 \documentclass[a4paper]{article}
3116
3117 \usepackage{probsoln}
3118 %uncomment the following line to display the answers
3119 \showanswers
3120
3121 % The following will generate a different set of
3122 % problems in subsequent years
3123 %\PSNrandseed{\year}
3124
3125 % The following will generate a different set of
3126 % problems on every run (where each run is at
3127 % least a minute apart from the previous one)
3128 \PSNrandseed{\time}
3129
3130 \begin{document}
3131 \title{Sample Problem Sheet}
3132 \author{Nicola Talbot}
3133 \maketitle
3134
3135 \begin{enumerate}
3136
3137 \newcounter{numproblems}
3138 % store a random number from 1 to 3 in the counter numproblems
3139 \random{numproblems}{1}{3}
3140
3141 % selects 1,2 or 3 problems from 1 of the given files:
3142 \doforrandN{1}{\file}{prob-1stprncp,prob-implicit}{\selectrandomly{\file}{\value{numproblems}}}
3143
3144 \item Differentiate the following functions:
3145 \input{prob-args}% load database
3146 \begin{enumerate}
3147 \newcounter{A}
3148 \newcounter{B}
3149 \newcounter{C}
3150 \random{A}{-4}{5}
3151 \random{B}{-3}{3}
3152 \random{C}{-1}{10}
3153 \item \useproblem{diff:quad}{\arabic{A}}{\arabic{B}}{\arabic{C}}
3154
3155 \random{A}{-5}{5}
3156 % make sure its not 0
3157 \whiledo{\value{A}=0}{\random{A}{-5}{5}}
3158 \item \useproblem{diff:sin}{\arabic{A}}
3159 \end{enumerate}
3160 \end{enumerate}
3161 \end{document}
3162 % \end{macrocode}
3163 %\fi
3164 %\iffalse
3165 % \begin{macrocode}
3166 %</sample2.tex>
3167 % \end{macrocode}
3168 %\fi
3169 %\iffalse
3170 % \begin{macrocode}
3171 %<*sample3.tex>
3172 % \end{macrocode}
3173 %\fi
3174 %\iffalse
3175 % \begin{macrocode}
3176 % This file is public domain
3177 \documentclass[a4paper]{article}
3178
3179 \usepackage{longtable}
3180 \usepackage{probsoln}
3181
3182 %\showanswers
3183
3184 \newcounter{problem}
3185 \renewcommand{\PSNitem}{\refstepcounter{problem}%
3186 \theproblem. }
3187 \renewcommand{\endPSNitem}{\\}
3188
3189 \renewenvironment{solution}{}{}
3190
3191 \newcommand{\selected}{\fbox{$\times$}}
3192 \newcommand{\notselected}{\fbox{\phantom{$\times$}}}
3193
3194 \begin{document}
3195 \begin{longtable}{lrrrl}
3196 \bfseries Question & \bfseries A & \bfseries B &
3197 \bfseries C & \ifshowanswers \bfseries Reason\fi\\
3198 \selectrandomly{prob-tabmchoice}{2}
3199 \end{longtable}
3200 \end{document}
3201 % \end{macrocode}
3202 %\fi
3203 %\iffalse
3204 % \begin{macrocode}
3205 %</sample3.tex>
3206 % \end{macrocode}
3207 %\fi
3208 %\iffalse
3209 % \begin{macrocode}
3210 %<*sample4.tex>
3211 % \end{macrocode}
3212 %\fi
3213 %\iffalse
3214 % \begin{macrocode}
3215 % This file is public domain
3216 \documentclass[a4paper]{article}
3217
3218 \usepackage{probsoln}
3219
3220 % Sample file illustrating use of \loadallproblems
3221 % All problems defined in specified file will be used
3222 % in order of definition
3223
3224 \loadallproblems{prob-easy}
3225 %\loadselectedproblems{diffeasy:gpowh,diffeasy:arccos}{prob-easy}
3226 %\loadexceptproblems{diffeasy:gpowh,diffeasy:arccos}{prob-easy}
3227
3228 \showanswers
3229
3230 \begin{document}
3231 Differentiate the following functions:
3232 \begin{enumerate}
3233 \foreachproblem{\item \thisproblem}
3234 \end{enumerate}
3235 \end{document}
3236 % \end{macrocode}
3237 %\fi
3238 %\iffalse
3239 % \begin{macrocode}
3240 %</sample4.tex>
3241 % \end{macrocode}
3242 %\fi
3243 %\iffalse
3244 % \begin{macrocode}
3245 %<*sample5.tex>
3246 % \end{macrocode}
3247 %\fi
3248 %\iffalse
3249 % \begin{macrocode}
3250 % This file is public domain
3251 \documentclass[a4paper]{report}
3252
3253 \usepackage{probsoln}
3254
3255 \begin{document}
3256 \hideanswers
3257
3258 \chapter{Easy Problems}
3259 \loadrandomproblems[easyproblems]{5}{prob-easy2}
3260
3261 \renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
3262 \begin{enumerate}
3263 \foreachproblem[easyproblems]{\item\label{prob:\thisproblemlabel}\thisproblem}
3264 \end{enumerate}
3265
3266 \chapter{Probability Spaces}
3267 \loadrandomproblems[probspaces]{2}{prob-probspaces2}
3268
3269 \renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
3270 \begin{enumerate}
3271 \foreachproblem[probspaces]{\item\label{prob:\thisproblemlabel}\thisproblem}
3272 \end{enumerate}
3273
3274 \appendix
3275 \chapter{Solutions}
3276 \showanswers
3277 \begin{enumerate}
3278 \foreachdataset{\thisdataset}{%
3279 \foreachproblem[\thisdataset]{\item[\ref{prob:\thisproblemlabel}]\thisproblem}
3280 }
3281 \end{enumerate}
3282
3283 \end{document}
3284 % \end{macrocode}
3285 %\fi
3286 %\iffalse
3287 % \begin{macrocode}
3288 %</sample5.tex>
3289 % \end{macrocode}
3290 %\fi
3291 %\iffalse
3292 % \begin{macrocode}
3293 %<*sample6.tex>
3294 % \end{macrocode}
3295 %\fi
3296 %\iffalse
3297 % \begin{macrocode}
3298 \documentclass{article}
3299
3300 \usepackage[usedefaultargs]{probsoln}
3301
3302 \loadallproblems{prob-args}
3303
3304 \begin{document}
3305 \begin{enumerate}
3306 \foreachproblem{\item \thisproblem}
3307 \end{enumerate}
3308 \end{document}
3309 % \end{macrocode}
3310 %\fi
3311 %\iffalse
3312 % \begin{macrocode}
3313 %</sample6.tex>
3314 % \end{macrocode}
3315 %\fi
3316 %\iffalse
3317 % \begin{macrocode}
3318 %<*sample7.tex>
3319 % \end{macrocode}
3320 %\fi
3321 %\iffalse
3322 % \begin{macrocode}
3323 \documentclass{article}
3324
3325 \usepackage{tikz}
3326 \usepackage{probsoln}
3327
3328 \loadallproblems{prob-mixed}
3329
3330 \begin{document}
3331 \section{Exercises}
3332 \begin{enumerate}
3333 \foreachproblem{\item\label{q-\thisproblemlabel} \thisproblem}
3334 \end{enumerate}
3335
3336 \section{Solutions}
3337 \showanswers
3338
3339 \begin{enumerate}
3340 \foreachsolution{\item[\ref{q-\thisproblemlabel}] \thisproblem}
3341 \end{enumerate}
3342 \end{document}
3343 % \end{macrocode}
3344 %\fi
3345 %\iffalse
3346 % \begin{macrocode}
3347 %</sample7.tex>
3348 % \end{macrocode}
3349 %\fi
3350 %\iffalse
3351 % \begin{macrocode}
3352 %<*sample8.tex>
3353 % \end{macrocode}
3354 %\fi
3355 %\iffalse
3356 % \begin{macrocode}
3357 \documentclass{article}
3358
3359 \usepackage{listings}
3360 \usepackage{probsoln}
3361
3362 \setkeys{probsoln}{fragile}
3363
3364 \loadallproblems{prob-verb}
3365
3366 \showanswers
3367 \begin{document}
3368 \begin{enumerate}
3369 \foreachproblem{\item \thisproblem}
3370 \end{enumerate}
3371 \end{document}
3372 % \end{macrocode}
3373 %\fi
3374 %\iffalse
3375 % \begin{macrocode}
3376 %</sample8.tex>
3377 % \end{macrocode}
3378 %\fi
3379 %\iffalse
3380 % \begin{macrocode}
3381 %<*sample9.tex>
3382 % \end{macrocode}
3383 %\fi
3384 %\iffalse
3385 % \begin{macrocode}
3386 % This file is public domain
3387 \documentclass[a4paper]{article}
3388
3389 \usepackage{probsoln}
3390 % uncomment the following line to display the answers
3391 % \showanswers
3392
3393 % Load 10 problems randomly selected from the given files
3394 \loadrandomproblems{10}{prob-1stprncp,prob-easy,prob-implicit,prob-probspaces}
3395
3396 \begin{document}
3397 \title{Sample Problem Sheet}
3398 \author{Nicola Talbot}
3399 \maketitle
3400
3401 \begin{enumerate}
3402 \foreachproblem{\item \thisproblem}
3403 \end{enumerate}
3404 \end{document}
3405 % \end{macrocode}
3406 %\fi
3407 %\iffalse
3408 % \begin{macrocode}
3409 %</sample9.tex>
3410 % \end{macrocode}
3411 %\fi
3412 %\iffalse
3413 % \begin{macrocode}
3414 %<*prob-1stprncp.tex>
3415 % \end{macrocode}
3416 %\fi
3417 %\iffalse
3418 % \begin{macrocode}
3419 % This file is public domain
3420 %
3421 % These problems all involve differentiating from 1st principles
3422
3423 \newproblem{dfp:xcube}{%
3424 Differentiate $f(x) = x^3$ with respect to $x$ by first principles.}{%
3425 \begin{eqnarray*}
3426 \frac{dy}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{f(x+\Delta x) - f(x)}{\Delta x}\\
3427 & = & \lim_{\Delta x\rightarrow 0}\frac{(x+\Delta x)^3-x^3}{\Delta x}\\
3428 & = & \lim_{\Delta x\rightarrow 0}\frac{(x+\Delta x)(x^2+2x\Delta x+(\Delta x)^2)-x^3}{\Delta x}\\
3429 & = & \lim_{\Delta x\rightarrow 0}\frac{x^3+3x^2\Delta x+3x(\Delta x)^2+(\Delta x)^3-x^3}{\Delta x}\\
3430 & = & \lim_{\Delta x\rightarrow 0}\frac{3x^2\Delta x+3x(\Delta x)^2+(\Delta x)^3}{\Delta x}\\
3431 & = & \lim_{\Delta x\rightarrow 0}3x^2+3x\Delta x + (\Delta x)^2\\
3432 & = & 3x^2
3433 \end{eqnarray*}}
3434
3435 \newproblem{dfp:Ioverxsq}{%
3436 Differentiate $\displaystyle f(x) = \frac{1}{x^2}$ with respect to $x$ by first principles.}{%
3437 \begin{eqnarray*}
3438 \frac{df}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{\frac{1}{(x+\Delta x)^2}-\frac{1}{x^2}}{\Delta x}\\
3439 & = & \lim_{\Delta x\rightarrow 0}\frac{\frac{x^2-(x+\Delta x)^2}{x^2(x+\Delta x)^2}}{\Delta x}\\
3440 & = & \lim_{\Delta x\rightarrow 0}\frac{x^2-(x^2+2x\Delta x+(\Delta x)^2)}{x^2\Delta x(x+\Delta x)^2}\\
3441 & = & \lim_{\Delta x\rightarrow 0}\frac{-2x\Delta x-(\Delta x)^2}{x^2\Delta x(x+\Delta x)^2}\\
3442 & = & \lim_{\Delta x\rightarrow 0}\frac{-2x-\Delta x}{x^2(x+\Delta x)^2}\\
3443 & = & \frac{-2x}{x^2x^2}\\
3444 & = & -\frac{2}{x^3}
3445 \end{eqnarray*}}
3446
3447 \newproblem{dfp:sqrtx}{%
3448 Differentiate from first principles $f(x) = \surd x$}{%
3449 \begin{eqnarray*}
3450 \frac{df}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{\sqrt{x+\Delta x}-\surd x}{\Delta x}\\
3451 & = & \lim_{\Delta x\rightarrow 0}\frac{(\sqrt{x+\Delta x}-\surd x)(\sqrt{x+\delta x}+\surd x)}{\Delta x(\sqrt{x+\Delta x}+\surd x)}\\
3452 & = & \lim_{\Delta x\rightarrow 0}\frac{x+\Delta x - x}{\Delta x(\sqrt{x+\Delta x}+\surd x)}\\
3453 & = & \lim_{\Delta x\rightarrow 0}\frac{\Delta x}{\Delta x(\sqrt{x+\Delta x}+\Delta x)}\\
3454 & = & \lim_{\Delta x\rightarrow 0}\frac{1}{\sqrt{x+\Delta x}+\surd x}\\
3455 & = & \frac{1}{2\surd x}
3456 \end{eqnarray*}}
3457
3458 \newproblem{dfp:cons}{%
3459 Differentiate from first principles $f(x) = c$ where $c$ is a constant.}{%
3460 \begin{eqnarray*}
3461 \frac{df}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{c-c}{\Delta x}\\
3462 & = & \lim_{\Delta x\rightarrow 0}0\\
3463 & = & 0
3464 \end{eqnarray*}}
3465
3466 \newproblem{dfp:cosx}{%
3467 Given
3468 \begin{eqnarray*}
3469 \lim_{x \rightarrow 0} \frac{\cos x - 1}{x} & = & 0\\
3470 \lim_{x \rightarrow 0} \frac{\sin x}{x} & = & 1
3471 \end{eqnarray*}
3472 differentiate from first principles $f(x) = \cos x$.}{%
3473 \begin{eqnarray*}
3474 \frac{df}{dx} & = & \lim_{\Delta x \rightarrow 0}\frac{f(x + \Delta x) - f(x)}{\Delta x}\\
3475 & = & \lim_{\Delta x \rightarrow 0} \frac{\cos(x + \Delta x) - \cos(x)}{\Delta x}\\
3476 & = & \lim_{\Delta x \rightarrow 0} \frac{\cos x\cos\Delta x - \sin x\sin\Delta x - \cos x}{\Delta x}\\
3477 & = & \lim_{\Delta x \rightarrow 0} \frac{\cos x(\cos\Delta x - 1) - \sin x\sin\Delta x}{\Delta x}\\
3478 & = & \cos x\lim_{\Delta x \rightarrow 0}\frac{\cos\Delta x - 1}{\Delta x}
3479 - \sin x\lim_{\Delta x \rightarrow 0}\frac{\sin\Delta x}{\Delta x}\\
3480 & = & -1 \qquad\mbox{(using given results)}
3481 \end{eqnarray*}}
3482 % \end{macrocode}
3483 %\fi
3484 %\iffalse
3485 % \begin{macrocode}
3486 %</prob-1stprncp.tex>
3487 % \end{macrocode}
3488 %\fi
3489 %\iffalse
3490 % \begin{macrocode}
3491 %<*prob-args.tex>
3492 % \end{macrocode}
3493 %\fi
3494 %\iffalse
3495 % \begin{macrocode}
3496 % This file is public domain
3497 %
3498 % These problems require arguments
3499 %
3500 % Arguments: #1->a_2, #2->a_1 and #3->a_0
3501 % (Arguments must be integers)
3502 \newcount\ctr
3503 \newproblem[3][{1}{2}{3}]{diff:quad}{%
3504 \(f(x) =
3505 \ifnum#1=0
3506 \else
3507 \ifnum#1=1\else#1\fi x^2
3508 \fi
3509 \ifnum#2=0
3510 \else
3511 \ifnum#2>0 \ifnum#1=0 \else + \fi \fi
3512 \ifnum#2=1\else#2\fi x
3513 \fi
3514 \ifnum#3=0
3515 \else
3516 \ifnum#3>0 \ifnum#2=0 \ifnum#1=0 \else + \fi \else + \fi\fi
3517 #3
3518 \fi\)
3519 }{%
3520 \(f'(x) =
3521 \ifnum#1=0
3522 \else
3523 \ctr=2
3524 \multiply\ctr by #1
3525 \the\ctr x
3526 \fi
3527 \ifnum#2=0
3528 \else
3529 \ifnum#2>0 \ifnum#1=0 \else + \fi \fi
3530 #2
3531 \fi
3532 % print 0 if both #1 and #2 are 0
3533 \ifnum#1=0 \ifnum#2=0 0 \fi\fi
3534 \)
3535 }
3536
3537 \begin{defproblem}[1][{6}]{diff:sin}
3538 \begin{onlyproblem}
3539 \(f(x) = \sin(#1x)\)
3540 \end{onlyproblem}
3541 \begin{onlysolution}
3542 \(f'(x) = #1\cos(#1x)\)
3543 \end{onlysolution}
3544 \end{defproblem}
3545 % \end{macrocode}
3546 %\fi
3547 %\iffalse
3548 % \begin{macrocode}
3549 %</prob-args.tex>
3550 % \end{macrocode}
3551 %\fi
3552 %\iffalse
3553 % \begin{macrocode}
3554 %<*prob-easy.tex>
3555 % \end{macrocode}
3556 %\fi
3557 %\iffalse
3558 % \begin{macrocode}
3559 % This file is public domain
3560 %
3561 % These are all easy differentiation problems
3562
3563 \newproblem{diffeasy:gpowh}{%
3564 \(f(x) = g(x)^{h(x)}.\)}{%
3565 \begin{eqnarray*}
3566 f(x) & = & e^{\ln g(x)^{h(x)}}\\
3567 & = & e^{h(x)\ln g(x)}\\
3568 f'(x) & = & e^{h(x)\ln g(x)}(h'(x)\ln g(x) + h(x)\frac{g'(x)}{g(x)})\\
3569 & = & g(x)^{h(x)}(h'(x)\ln g(x) + \frac{h(x)g'(x)}{g(x)})
3570 \end{eqnarray*}}
3571
3572 \newproblem{diffeasy:arcsin}{%
3573 \(y = \arcsin(x)\)}{%
3574 \[\sin(y) = x\]
3575 diff. w.r.t. $x$:
3576 \begin{eqnarray*}
3577 \cos y \frac{dy}{dx} & = & 1\\
3578 \frac{dy}{dx} & = & \frac{1}{\cos y}\\
3579 & = & \frac{1}{\sqrt{1 - \sin^2y}}\\
3580 & = & \frac{1}{\sqrt{1-x^2}}.
3581 \end{eqnarray*}}
3582
3583 \newproblem{diffeasy:arccos}{%
3584 $y = \arccos x$.}{%
3585 \(\cos y = x\)
3586 diff. w.r.t. $x$:
3587 \begin{eqnarray*}
3588 -\sin y \frac{dy}{dx} & = & 1\\
3589 \frac{dy}{dx} & = & \frac{-1}{\sin y}\\
3590 & = & \frac{-1}{\sqrt{1-\cos^2y}}\\
3591 & = & \frac{-1}{\sqrt{1-x^2}}
3592 \end{eqnarray*}}
3593
3594 \newproblem{diffeasy:tan}{%
3595 \(y = \tan x\)}{%
3596 \begin{eqnarray*}
3597 y & = & \tan x\\
3598 & = & \frac{\sin x}{\cos x}\\
3599 \frac{dy}{dx} & = & \frac{\cos x}{\cos x} + \sin x\times\frac{-1}{\cos^2x}\times -\sin x\\
3600 & = & 1 + \tan^2x\\
3601 & = & \sec^2x.
3602 \end{eqnarray*}}
3603
3604 \newproblem{diffeasy:arctan}{%
3605 \(y = \arctan x = \tan^{-1}x\)}{%
3606 \[\tan y = x\]
3607 diff w.r.t. $x$:
3608 \begin{eqnarray*}
3609 \sec^2y\frac{dy}{dx} & = & 1\\
3610 \frac{dy}{dx} & = & \frac{1}{\sec^2y}\\
3611 & = & \frac{1}{1+\tan^2y}\\
3612 & = & \frac{1}{1+x^2}
3613 \end{eqnarray*}}
3614
3615 \newproblem{diffeasy:cot}{%
3616 \(y = (\tan x)^{-1} = \cot x\)}{%
3617 \begin{eqnarray*}
3618 \frac{dy}{dx} & = & -(\tan x)^{-2}\sec^2x\\
3619 & = & -\frac{\cos^2x}{\sin^2x}\cdot\frac{1}{\cos^2x}\\
3620 & = & \frac{-1}{\sin^2x}\\
3621 & = & -\csc^2x.
3622 \end{eqnarray*}}
3623
3624 \newproblem{diffeasy:cosxsqsinx}{%
3625 $y = \cos(x^2)\sin x$.}{%
3626 \[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]}
3627
3628 \newproblem{diffeasy:xlnx}{%
3629 $y = (x+1)\ln(x+1)$.}{%
3630 \begin{eqnarray*}
3631 \frac{dy}{dx} & = & \ln(x+1) + \frac{x+1}{x+1}\\
3632 & = & 1 + \ln(x+1).
3633 \end{eqnarray*}}
3634
3635 \newproblem{diffeasy:glng}{%
3636 $f(x) = g(x)\ln(g(x))$.}{%
3637 \begin{eqnarray*}
3638 f'(x) & = & g'(x)\ln(g(x)) + \frac{g(x)}{g(x)}g'(x)\\
3639 & = & g'(x)(1+\ln(g(x))).
3640 \end{eqnarray*}}
3641
3642 \newproblem{diffeasy:sinx/x}{%
3643 $y = \frac{\sin x}{x}$.}{%
3644 \[\frac{dy}{dx} = \frac{\cos x}{x} - \frac{\sin x}{x^2}\]}
3645
3646 \newproblem{diffeasy:exp4x}{%
3647 $y = \exp(4x)$
3648 }%
3649 {%
3650 \[\frac{dy}{dx} = 4\exp(4x)\]
3651 }
3652
3653 \newproblem{diffeasy:exp3x+2}{%
3654 $y = \exp(3x+2)$
3655 }%
3656 {%
3657 \[\frac{dy}{dx} = 3\exp(3x+2)\]
3658 }
3659
3660 \newproblem{diffeasy:cubic}{%
3661 $y=x^3 + 4x^2 - x + 3$
3662 }%
3663 {%
3664 \[\frac{dy}{dx} = 3x^2 + 8x - 1\]
3665 }
3666
3667 \newproblem{diffeasy:quad}{%
3668 $y=2x^3 + 6x -1$
3669 }%
3670 {%
3671 \[\frac{dy}{dx} = 6x + 6 = 6(x+1)\]
3672 }
3673 % \end{macrocode}
3674 %\fi
3675 %\iffalse
3676 % \begin{macrocode}
3677 %</prob-easy.tex>
3678 % \end{macrocode}
3679 %\fi
3680 %\iffalse
3681 % \begin{macrocode}
3682 %<*prob-easy2.tex>
3683 % \end{macrocode}
3684 %\fi
3685 %\iffalse
3686 % \begin{macrocode}
3687 % This file is public domain
3688 %
3689 % These are all easy differentiation problems
3690
3691 \begin{defproblem}{diffeasy:gpowh}%
3692 \begin{onlyproblem}%
3693 \(f(x) = g(x)^{h(x)}.\)%
3694 \end{onlyproblem}%
3695 \begin{onlysolution}%
3696 \begin{eqnarray*}
3697 f(x) & = & e^{\ln g(x)^{h(x)}}\\
3698 & = & e^{h(x)\ln g(x)}\\
3699 f'(x) & = & e^{h(x)\ln g(x)}(h'(x)\ln g(x) + h(x)\frac{g'(x)}{g(x)})\\
3700 & = & g(x)^{h(x)}(h'(x)\ln g(x) + \frac{h(x)g'(x)}{g(x)})
3701 \end{eqnarray*}%
3702 \end{onlysolution}%
3703 \end{defproblem}
3704
3705 \begin{defproblem}{diffeasy:arcsin}%
3706 \begin{onlyproblem}%
3707 \(y = \arcsin(x)\)%
3708 \end{onlyproblem}%
3709 \begin{onlysolution}%
3710 \[\sin(y) = x\]
3711 diff. w.r.t. $x$:
3712 \begin{eqnarray*}
3713 \cos y \frac{dy}{dx} & = & 1\\
3714 \frac{dy}{dx} & = & \frac{1}{\cos y}\\
3715 & = & \frac{1}{\sqrt{1 - \sin^2y}}\\
3716 & = & \frac{1}{\sqrt{1-x^2}}.
3717 \end{eqnarray*}
3718 \end{onlysolution}%
3719 \end{defproblem}
3720
3721 \begin{defproblem}{diffeasy:arccos}%
3722 \begin{onlyproblem}%
3723 $y = \arccos x$.%
3724 \end{onlyproblem}%
3725 \begin{onlysolution}%
3726 \(\cos y = x\)
3727 diff. w.r.t. $x$:
3728 \begin{eqnarray*}
3729 -\sin y \frac{dy}{dx} & = & 1\\
3730 \frac{dy}{dx} & = & \frac{-1}{\sin y}\\
3731 & = & \frac{-1}{\sqrt{1-\cos^2y}}\\
3732 & = & \frac{-1}{\sqrt{1-x^2}}
3733 \end{eqnarray*}
3734 \end{onlysolution}%
3735 \end{defproblem}
3736
3737 \begin{defproblem}{diffeasy:tan}%
3738 \begin{onlyproblem}%
3739 \(y = \tan x\)%
3740 \end{onlyproblem}
3741 \begin{onlysolution}%
3742 \begin{eqnarray*}
3743 y & = & \tan x\\
3744 & = & \frac{\sin x}{\cos x}\\
3745 \frac{dy}{dx} & = & \frac{\cos x}{\cos x} + \sin x\times\frac{-1}{\cos^2x}\times -\sin x\\
3746 & = & 1 + \tan^2x\\
3747 & = & \sec^2x.
3748 \end{eqnarray*}
3749 \end{onlysolution}%
3750 \end{defproblem}
3751
3752 \begin{defproblem}{diffeasy:arctan}%
3753 \begin{onlyproblem}%
3754 \(y = \arctan x = \tan^{-1}x\)%
3755 \end{onlyproblem}%
3756 \begin{onlysolution}%
3757 \[\tan y = x\]
3758 diff w.r.t. $x$:
3759 \begin{eqnarray*}
3760 \sec^2y\frac{dy}{dx} & = & 1\\
3761 \frac{dy}{dx} & = & \frac{1}{\sec^2y}\\
3762 & = & \frac{1}{1+\tan^2y}\\
3763 & = & \frac{1}{1+x^2}
3764 \end{eqnarray*}
3765 \end{onlysolution}%
3766 \end{defproblem}
3767
3768 \begin{defproblem}{diffeasy:cot}%
3769 \begin{onlyproblem}%
3770 \(y = (\tan x)^{-1} = \cot x\)%
3771 \end{onlyproblem}
3772 \begin{onlysolution}%
3773 \begin{eqnarray*}
3774 \frac{dy}{dx} & = & -(\tan x)^{-2}\sec^2x\\
3775 & = & -\frac{\cos^2x}{\sin^2x}\cdot\frac{1}{\cos^2x}\\
3776 & = & \frac{-1}{\sin^2x}\\
3777 & = & -\csc^2x.
3778 \end{eqnarray*}
3779 \end{onlysolution}%
3780 \end{defproblem}
3781
3782 \begin{defproblem}{diffeasy:cosxsqsinx}%
3783 \begin{onlyproblem}%
3784 $y = \cos(x^2)\sin x$.%
3785 \end{onlyproblem}%
3786 \begin{onlysolution}%
3787 \[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]
3788 \end{onlysolution}%
3789 \end{defproblem}
3790
3791 \begin{defproblem}{diffeasy:xlnx}%
3792 \begin{onlyproblem}%
3793 $y = (x+1)\ln(x+1)$.
3794 \end{onlyproblem}%
3795 \begin{onlysolution}%
3796 \begin{eqnarray*}
3797 \frac{dy}{dx} & = & \ln(x+1) + \frac{x+1}{x+1}\\
3798 & = & 1 + \ln(x+1).
3799 \end{eqnarray*}
3800 \end{onlysolution}
3801 \end{defproblem}
3802
3803 \begin{defproblem}{diffeasy:glng}%
3804 \begin{onlyproblem}%
3805 $f(x) = g(x)\ln(g(x))$.
3806 \end{onlyproblem}
3807 \begin{onlysolution}%
3808 \begin{eqnarray*}
3809 f'(x) & = & g'(x)\ln(g(x)) + \frac{g(x)}{g(x)}g'(x)\\
3810 & = & g'(x)(1+\ln(g(x))).
3811 \end{eqnarray*}
3812 \end{onlysolution}%
3813 \end{defproblem}
3814
3815 \begin{defproblem}{diffeasy:sinx/x}
3816 \begin{onlyproblem}%
3817 $y = \frac{\sin x}{x}$.
3818 \end{onlyproblem}
3819 \begin{onlysolution}%
3820 \[\frac{dy}{dx} = \frac{\cos x}{x} - \frac{\sin x}{x^2}\]
3821 \end{onlysolution}%
3822 \end{defproblem}
3823
3824 % \end{macrocode}
3825 %\fi
3826 %\iffalse
3827 % \begin{macrocode}
3828 %</prob-easy2.tex>
3829 % \end{macrocode}
3830 %\fi
3831 %\iffalse
3832 % \begin{macrocode}
3833 %<*prob-implicit.tex>
3834 % \end{macrocode}
3835 %\fi
3836 %\iffalse
3837 % \begin{macrocode}
3838 % This file is public domain
3839 %
3840 % These are all implicit differentiation problems
3841
3842 \newproblem{imd:circ}{%
3843 Find the gradient of the unit circle ($x^2 + y^2 = 1$).}{%
3844 Differentiating with respect to $x$ gives:
3845 \begin{eqnarray*}
3846 2x + 2y\frac{dy}{dx} & = & 0\\
3847 \frac{dy}{dx} & = & \frac{-2x}{2y}\\
3848 & = & \frac{-x}{\sqrt{1-x^2}}.
3849 \end{eqnarray*}}
3850
3851 \newproblem{imd:ellipse}{%
3852 Find the gradient of the ellipse given by $4x^2 + 3y^2 = 25$.}{%
3853 Differentiating with respect to $x$ gives:
3854 \begin{eqnarray*}
3855 8x + 6y\frac{dy}{dx} & = & 0\\
3856 \frac{dy}{dx} & = & \frac{-8x}{6y}\\
3857 & = & \frac{-4x}{\sqrt{25-4x^2}}.
3858 \end{eqnarray*}}
3859
3860 \newproblem{imd:ysq:xcuov2mx}{%
3861 Find $\frac{dy}{dx}$, given
3862 \begin{displaymath}
3863 y^2 = \frac{x^3}{2-x}
3864 \end{displaymath}}{%
3865 Differentiating both sides w.r.t.\ $x$:
3866 \begin{eqnarray*}
3867 2y\frac{dy}{dx} & = & \frac{(2-x)3x^2 - x^3(-1)}{(2-x)^2}\\
3868 & = & \frac{3x^2(2-x) + x^3}{(2-x)^2}\\
3869 & = & \frac{6x^2 - 3x^3 + x^3}{(2-x)^2}\\
3870 & = & \frac{6x^2-2x^3}{(2-x)^2}\\
3871 & = & 2x^2\frac{3-x}{(2-x)^2}
3872 \end{eqnarray*}
3873 Therefore
3874 \begin{displaymath}
3875 y\frac{dy}{dx} = x^2\frac{3-x}{(2-x)^2}
3876 \end{displaymath}}
3877
3878 \newproblem{imd:exy:IIxay}{%
3879 Differentiate w.r.t.\ $x$:
3880 \begin{displaymath}
3881 e^{xy} = 2x + y
3882 \end{displaymath}}{%
3883 Differentiating both sides w.r.t.\ $x$:
3884 \begin{eqnarray*}
3885 e^{xy}(1y + x\frac{dy}{dx}) & = & 2 + \frac{dy}{dx}\\
3886 xe^{xy}\frac{dy}{dx} - \frac{dy}{dx} & = & 2 - ye^{xy} \\
3887 \frac{dy}{dx}(xe^{xy}-1) & = & 2 - ye^{xy}\\
3888 \frac{dy}{dx} & = & \frac{2-ye^{xy}}{xe^{xy}-1}
3889 \end{eqnarray*}}
3890 % \end{macrocode}
3891 %\fi
3892 %\iffalse
3893 % \begin{macrocode}
3894 %</prob-implicit.tex>
3895 % \end{macrocode}
3896 %\fi
3897 %\iffalse
3898 % \begin{macrocode}
3899 %<*prob-mchoice.tex>
3900 % \end{macrocode}
3901 %\fi
3902 %\iffalse
3903 % \begin{macrocode}
3904 % This file is public domain
3905 %
3906 % These problems are for multiple choice questions
3907 \newproblem{mc:prod}{%
3908 Which of the following is the derivative of $x\sin(x)$?
3909 (Circle the correct answer.)
3910 \ifthenelse{\boolean{showanswers}}{}%
3911 {\begin{description}
3912 \item[A] $\sin(x)$
3913 \item[B] $x\cos(x)$
3914 \item[C] $\sin(x) + x\cos(x)$
3915 \end{description}
3916 }}{%
3917 \begin{description}
3918 \item[A] $\sin(x)$
3919 \item[B] $x\cos(x)$
3920 \item[\textcircled{C}] $\sin(x) + x\cos(x)$ (product rule).
3921 \end{description}
3922 }
3923
3924 \newproblem{mc:quot}{%
3925 Which of the following is the derivative of $\frac{\sin(x)}{x}$?
3926 (Circle the correct answer.)
3927 \ifthenelse{\boolean{showanswers}}{}%
3928 {\begin{description}
3929 \item[A] $\sin(x)$
3930 \item[B] $\cos(x)$
3931 \item[C] $\frac{\cos(x)x-\sin(x)}{x^2}$
3932 \end{description}
3933 }}{%
3934 \begin{description}
3935 \item[A] $\sin(x)$
3936 \item[B] $\cos(x)$
3937 \item[\textcircled{C}] $\frac{\cos(x)x-\sin(x)}{x^2}$
3938 (quotient rule)
3939 \end{description}
3940 }
3941 % \end{macrocode}
3942 %\fi
3943 %\iffalse
3944 % \begin{macrocode}
3945 %</prob-mchoice.tex>
3946 % \end{macrocode}
3947 %\fi
3948 %\iffalse
3949 % \begin{macrocode}
3950 %<*prob-mixed.tex>
3951 % \end{macrocode}
3952 %\fi
3953 %\iffalse
3954 % \begin{macrocode}
3955 % This file is public domain
3956 %
3957 % These problems are a mixture of essay-style and questions with
3958 % answers. One of these problems requires the tikz package
3959
3960 \newproblem*{oop}{Describe what is meant by object-oriented
3961 programming.}
3962
3963 \begin{defproblem}{inheritance}
3964 Describe what is meant by the term \emph{inheritance} in
3965 object-oriented programming. Use examples.
3966 \end{defproblem}
3967
3968 \begin{defproblem}{weightedcoin}%
3969 \begin{onlyproblem}
3970 A coin is weighted so that heads is four times as likely
3971 as tails. Find the probability that:
3972 \begin{textenum}
3973 \item tails appears,
3974 \item heads appears
3975 \end{textenum}%
3976 \end{onlyproblem}%
3977 \begin{onlysolution}
3978 Let $p=P(T)$, then $P(H)=4p$. We require $P(H)+P(T)=1$,
3979 so $4p+p=1$, hence $p=\frac{1}{5}$. Therefore:
3980 \begin{textenum}
3981 \item $P(T)=\frac{1}{5}$,
3982 \item $P(H)=\frac{4}{5}$
3983 \end{textenum}
3984 \end{onlysolution}
3985 \end{defproblem}
3986
3987 \begin{defproblem}{validprobspaces}
3988 \begin{onlyproblem}%
3989 Under which of the following functions does
3990 $S=\{a_1,a_2\}$ become a probability space?
3991 \par
3992 \begin{textenum}
3993 \begin{tabular}{ll}
3994 \item $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
3995 &
3996 \item\label{validprobspacescorrect1} $P(a_1)=\frac{3}{4}$,
3997 $P(a_2)=\frac{1}{4}$
3998 \\
3999 \item\label{validprobspacescorrect2} $P(a_1)=1$, $P(a_2)=0$
4000 &
4001 \item $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
4002 \end{tabular}
4003 \end{textenum}
4004 \end{onlyproblem}%
4005 \begin{onlysolution}%
4006 \ref{validprobspacescorrect1} and \ref{validprobspacescorrect2}%
4007 \end{onlysolution}
4008 \end{defproblem}
4009
4010 \begin{defproblem}{digraph}
4011 \begin{onlyproblem}\label{ex:digraph}
4012 Identify, if any, the sinks and sources of the digraph shown in Figure~\ref{fig:digraph}.
4013
4014 \begin{figure}[tbh]
4015 \centering
4016 \begin{tikzpicture}[every node/.style={draw,circle}]
4017 \path (0,0) node (A) {$A$}
4018 (1,0) node (B) {$B$}
4019 (0,1) node (C) {$C$};
4020 \draw[->] (A) -- (B);
4021 \draw[->] (B) -- (C);
4022 \draw[->] (A) -- (C);
4023 \end{tikzpicture}
4024 \par
4025 \caption{Digraph for Question~\ref{ex:digraph}}
4026 \label{fig:digraph}
4027 \end{figure}
4028 \end{onlyproblem}
4029 \begin{onlysolution}
4030 $A$ is a souce and $C$ is a sink.
4031 \end{onlysolution}
4032 \end{defproblem}
4033 % \end{macrocode}
4034 %\fi
4035 %\iffalse
4036 % \begin{macrocode}
4037 %</prob-mixed.tex>
4038 % \end{macrocode}
4039 %\fi
4040 %\iffalse
4041 % \begin{macrocode}
4042 %<*prob-newdata.tex>
4043 % \end{macrocode}
4044 %\fi
4045 %\iffalse
4046 % \begin{macrocode}
4047 % This file is public domain
4048 \begin{defproblem}{sample}
4049 \begin{onlyproblem}
4050 Differentiate $y=\sin x$
4051 \end{onlyproblem}
4052 \begin{onlysolution}
4053 $y'=\cos x$
4054 \end{onlysolution}
4055 \end{defproblem}
4056
4057 \begin{defproblem}[1]{sample2}
4058 \begin{onlyproblem}
4059 Differentiate $y = \sin(#1x)$
4060 \end{onlyproblem}
4061 \begin{onlysolution}
4062 $y'=#1\cos #1x$
4063 \end{onlysolution}
4064 \end{defproblem}
4065
4066 \begin{defproblem}{sample3}
4067 \begin{onlyproblem}
4068 Differentiate $y = x^2$.
4069 \end{onlyproblem}
4070 \begin{onlysolution}
4071 $y' = 2x$
4072 \end{onlysolution}
4073 \end{defproblem}
4074 % \end{macrocode}
4075 %\fi
4076 %\iffalse
4077 % \begin{macrocode}
4078 %</prob-newdata.tex>
4079 % \end{macrocode}
4080 %\fi
4081 %\iffalse
4082 % \begin{macrocode}
4083 %<*prob-nosoln.tex>
4084 % \end{macrocode}
4085 %\fi
4086 %\iffalse
4087 % \begin{macrocode}
4088 % This file is public domain
4089 %
4090 % these problems don't have solutions
4091
4092 \newproblem*{oop}{Describe what is meant by object-oriented
4093 programming.}
4094
4095 \newproblem*{inheritance}{Describe what is meant by the term
4096 \emph{inheritance} in object-oriented programming. Use examples.}
4097 % \end{macrocode}
4098 %\fi
4099 %\iffalse
4100 % \begin{macrocode}
4101 %</prob-nosoln.tex>
4102 % \end{macrocode}
4103 %\fi
4104 %\iffalse
4105 % \begin{macrocode}
4106 %<*prob-probspaces.tex>
4107 % \end{macrocode}
4108 %\fi
4109 %\iffalse
4110 % \begin{macrocode}
4111 % This file is public domain
4112 %
4113 % Finite probability spaces
4114 \newproblem{weightedcoin}{%
4115 A coin is weighted so that heads is four times as likely
4116 as tails. Find the probability that:
4117 \begin{textenum}
4118 \item tails appears,
4119 \item heads appears
4120 \end{textenum}}{%
4121 Let $p=P(T)$, then $P(H)=4p$. We require $P(H)+P(T)=1$,
4122 so $4p+p=1$, hence $p=\frac{1}{5}$. Therefore:
4123 \begin{textenum}
4124 \item $P(T)=\frac{1}{5}$,
4125 \item $P(H)=\frac{4}{5}$
4126 \end{textenum}}
4127
4128 \newproblem*{validprobspaces}{%
4129 Under which of the following functions does
4130 $S=\{a_1,a_2\}$ become a probability space?
4131 \par
4132 \begin{textenum}
4133 \begin{tabular}{ll}
4134 \incorrectitem $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
4135 &
4136 \correctitem $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$
4137 \\
4138 \correctitem $P(a_1)=1$, $P(a_2)=0$
4139 &
4140 \incorrectitem $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
4141 \end{tabular}
4142 \end{textenum}
4143 }
4144 % \end{macrocode}
4145 %\fi
4146 %\iffalse
4147 % \begin{macrocode}
4148 %</prob-probspaces.tex>
4149 % \end{macrocode}
4150 %\fi
4151 %\iffalse
4152 % \begin{macrocode}
4153 %<*prob-probspaces2.tex>
4154 % \end{macrocode}
4155 %\fi
4156 %\iffalse
4157 % \begin{macrocode}
4158 % This file is public domain
4159 %
4160 % Finite probability spaces
4161 \begin{defproblem}{weightedcoin}
4162 \begin{onlyproblem}%
4163 A coin is weighted so that heads is four times as likely
4164 as tails. Find the probability that:
4165 \begin{textenum}
4166 \item tails appears,
4167 \item heads appears
4168 \end{textenum}
4169 \end{onlyproblem}
4170 \begin{onlysolution}%
4171 Let $p=P(T)$, then $P(H)=4p$. We require $P(H)+P(T)=1$,
4172 so $4p+p=1$, hence $p=\frac{1}{5}$. Therefore:
4173 \begin{textenum}
4174 \item $P(T)=\frac{1}{5}$,
4175 \item $P(H)=\frac{4}{5}$
4176 \end{textenum}
4177 \end{onlysolution}
4178 \end{defproblem}
4179
4180 \begin{defproblem}{validprobspaces}
4181 \begin{onlyproblem}%
4182 Under which of the following functions does
4183 $S=\{a_1,a_2\}$ become a probability space?
4184 \par
4185 \begin{textenum}
4186 \begin{tabular}{ll}
4187 \item $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
4188 &
4189 \item\label{validprobspacescorrect1} $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$
4190 \\
4191 \item\label{validprobspacescorrect2} $P(a_1)=1$, $P(a_2)=0$
4192 &
4193 \item $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
4194 \end{tabular}
4195 \end{textenum}
4196 \end{onlyproblem}%
4197 \begin{onlysolution}%
4198 \ref{validprobspacescorrect1} and \ref{validprobspacescorrect2}%
4199 \end{onlysolution}
4200 \end{defproblem}
4201 % \end{macrocode}
4202 %\fi
4203 %\iffalse
4204 % \begin{macrocode}
4205 %</prob-probspaces2.tex>
4206 % \end{macrocode}
4207 %\fi
4208 %\iffalse
4209 % \begin{macrocode}
4210 %<*prob-tabmchoice.tex>
4211 % \end{macrocode}
4212 %\fi
4213 %\iffalse
4214 % \begin{macrocode}
4215 % This file is public domain
4216 %
4217 % These problems are designed to be placed in a
4218 % tabular environment
4219 %
4220 \newproblem{tab:1}{%
4221 What is $(3+2)\times5$? &
4222 25 \ifshowanswers\selected\else\notselected\fi &
4223 13 \notselected &
4224 10 \notselected &
4225 }{Brackets come first}%
4226
4227 \newproblem{tab:2}{%
4228 What is $-1+2\times3$? &
4229 3 \notselected &
4230 -7 \notselected &
4231 5 \ifshowanswers\selected\else\notselected\fi &
4232 }{Multiplication comes first}%
4233 % \end{macrocode}
4234 %\fi
4235 %\iffalse
4236 % \begin{macrocode}
4237 %</prob-tabmchoice.tex>
4238 % \end{macrocode}
4239 %\fi
4240 %\iffalse
4241 % \begin{macrocode}
4242 %<*prob-verb.tex>
4243 % \end{macrocode}
4244 %\fi
4245 %\iffalse
4246 % \begin{macrocode}
4247 \begin{defproblem}{code-helloworld}
4248 This problem has a code fragment.
4249 \begin{onlyproblem}
4250 \lstset{language=Java}
4251 \begin{lstlisting}
4252 public class HelloWorld
4253 {
4254 public static void main(String[] args)
4255 {
4256 System.out.println("Hello World!");
4257 }
4258 }
4259 \end{lstlisting}
4260 \end{onlyproblem}
4261 \begin{onlysolution}
4262 \lstset{language=Java}
4263 \begin{lstlisting}
4264 public class HelloWorld
4265 {
4266 public static void main(String[] args)
4267 {
4268 System.out.println("Hello "
4269 + (args.length==0 ? "anon" : args[0])+"!");
4270 }
4271 }
4272 \end{lstlisting}
4273 \end{onlysolution}
4274 \end{defproblem}
4275 % \end{macrocode}
4276 %\fi
4277 %\iffalse
4278 % \begin{macrocode}
4279 %</prob-verb.tex>
4280 % \end{macrocode}
4281 %\fi
4282 %\Finale
4283 \endinput