Teaching Programming

Stefan Behnel stefan_ml at behnel.de
Tue May 4 12:30:46 EDT 2010


superpollo, 04.05.2010 17:55:
> since i have some kind of computer literacy (as opposed to most of my
> colleagues), some years ago i was kindly asked to try and solve a
> "simple" particular problem, that is to write a program that generates
> math exercises (q+a) from an example taken from the textbook. for
> instance, this:
>
> %%TITLE:Sample worksheet
> %%
> %%SCHEMA:\lim_{x \to <A>}
> %%SCHEMA:\frac
> %%SCHEMA:{x^3-<A-B>x^2-<AB>x}
> %%SCHEMA:{x^3-<A>x^2+<C>x-<AC>}\\
> %%
> %%ANS:FRAC
> %%ANSNUM:<A^2+AB>
> %%ANSDEN:<A^2+C>
> %%
> %%AMIN:1
> %%AINC:1
> %%AMAX:2
> %%BMIN:3
> %%BINC:1
> %%BMAX:4
> %%CMIN:2
> %%CINC:1
> %%CMAX:3
>
> should generate this latex source document:
>
> \documentclass[a4paper,10pt,twocolumn,fleqn]{article}
> \title{Sample worksheet}
> \pagestyle{empty}
> \usepackage[italian]{babel}
> \usepackage{amsmath}
> \usepackage{amssymb}
> \usepackage{cancel}
> \usepackage{mathrsfs}
> \usepackage[dvips]{graphicx}
> \usepackage{eurosym}
> \usepackage{pstricks}
> \usepackage{pst-eucl}
> \usepackage{pst-poly}
> \usepackage{pst-plot}
> \frenchspacing
> \begin{document}
> \section*{\center{\framebox{Sample worksheet}}}
> \noindent
> \begin{enumerate}
> \item
> \begin{multline*}
> \lim_{x \to 1}
> \frac
> {x^3+3x^2-4x}
> {x^3-x^2+2x-2}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 2}
> \frac
> {x^3+x^2-6x}
> {x^3-2x^2+2x-4}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 2}
> \frac
> {x^3+2x^2-8x}
> {x^3-2x^2+2x-4}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 1}
> \frac
> {x^3+2x^2-3x}
> {x^3-x^2+2x-2}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 1}
> \frac
> {x^3+2x^2-3x}
> {x^3-x^2+3x-3}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 1}
> \frac
> {x^3+3x^2-4x}
> {x^3-x^2+3x-3}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 2}
> \frac
> {x^3+x^2-6x}
> {x^3-2x^2+3x-6}\\
> \end{multline*}
> \item
> \begin{multline*}
> \lim_{x \to 2}
> \frac
> {x^3+2x^2-8x}
> {x^3-2x^2+3x-6}\\
> \end{multline*}
> \end{enumerate}
> \subsection*{\center{Answers}}
> \begin{enumerate}
> \item
> \begin{displaymath}
> \frac{5}{3}
> \end{displaymath}
> \item
> \begin{displaymath}
> \frac{5}{3}
 > [...]

I'm not exactly sure I understand the mapping between the two formats, but 
it seems to me that you'll need a proper math expression parser (with a 
strong emphasis on *parser*) for this. Math expressions are not exactly 
trivial (recursion, prefix/infix/postfix notations, functions), which is 
why 'real' programmers don't write parsers for them but download tested, 
working code for that from somewhere.


> another thing is that for some features i intended to include i found
> convenient to use python (since it's the language i feel i am more at
> ease with), so i had to cope with template lines like this:
>
> %%SCHEMA:<PYCODE1:import math ; print int(math.sqrt($A**2-2.*$A*$B))>

You should assure that the math module is always imported, and otherwise 
restrict the expressiveness to expressions (terms that result in a value), 
not arbitrary statements (executable commands that do things without 
returning anything).


> to make a long story short: the whole program is now 4775 lines of bash
> code,

Argh!


> *but* it was and it is a wonderful experience to learn to program

It does sound like you almost completely skipped over two very important 
programming lessons, though: reading code is harder than writing it (i.e. 
maintenance cost matters), and the best code is the code that you don't 
need to write (and debug and maintain).

Stefan




More information about the Python-list mailing list