Teaching Programming

superpollo utente at esempio.net
Tue May 4 11:55:20 EDT 2010


superpollo ha scritto:
> Stefan Behnel ha scritto:
>> superpollo, 04.05.2010 14:46:
>>> my template system wants
>>> the input to generate the code to stay on a single line ( don't ask 
>>> :-( )
>>
>> I hope you don't mind if I still ask. What are you generating and for 
>> what templating system?
> 
> ok, since you asked for it, prepare yourself for a bit of a horror story 
> ;-)
> 
> i will answer in my next post

ok here it is.

i am not a programmer or it-guy in the first place; my job is as a high 
school teacher in a field only remotely connected with computers (math 
and physics).

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}
\end{displaymath}
\item
\begin{displaymath}
2
\end{displaymath}
\item
\begin{displaymath}
\frac{4}{3}
\end{displaymath}
\item
\begin{displaymath}
1
\end{displaymath}
\item
\begin{displaymath}
\frac{5}{4}
\end{displaymath}
\item
\begin{displaymath}
\frac{10}{7}
\end{displaymath}
\item
\begin{displaymath}
\frac{12}{7}
\end{displaymath}
\end{enumerate}
\end{document}

which in turn can be used to generate the following pdf file:

http://www.datafilehost.com/download-cc88a19e.html

fine huh? now, for the horror part.

when i began putting down some code i thought that maybe it would take a 
couple of evenings to put up a working prototype, and i was w-r-o-n-g:

1) as i said i am not a professional
2) i decided to use bash shell as a core language for the 
parser/generator ( *sigh* )
3) feautiritis soon crept in

if some of you knows a bit about math and especially algebra, you will 
understand that some of the biggest problems in generating standard math 
notation in to cope with the various layers of traditions and innuendos: 
for example i could generate the string "2x" which is fine; but what 
about "1x" or "-1x" or "0x"? and what about plus signs at the start of 
an expression (normally omitted)? when does a subexpression start? etc 
... there are plenty of icompatible rules and as much as exceptions ...

as you can see i had many problems to solve and i am not trained to cope 
with such issues from a programmatic standpoint.

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))>

or even worse:

%%ANSFRCEMBPYC:<PYCODE3:print "\\\\mathrm{p.~st.~}" , $C , 
"~\\\\longmapsto~" , $C**2*(3*$A+3*$B+$C) , "~\\\\mathrm{(max~loc.)}">\\

to make a long story short: the whole program is now 4775 lines of bash 
code, written by an unqualified amateur under time pressure; sometimes i 
have to hand-modify it to get certain outputs as i expect them to be; 
this in turn breaks other subsystems.

i am ashamed to post the code, and it is a true design nightmare. also 
it is offtopic (being bash).

*but* it was and it is a wonderful experience to learn to program, to 
acknowledge weaknesses end strongnesses of various approaches, to better 
understand the structure of math notation, problem solving end teaching 
techniques.

it is still work in progress though, and i hope i will never see the day 
i have to refurbish it. i hope -- instead -- that the next time i try to 
attack the task i will have a better knowledge thanks to errors 
committed, to avoid many of the pitfalls i stumbled upon.

for now, it works.

*whew* i hope i was not boring, and i sincerely hope that some of you 
could see the depth of the problem i tried to solve (perhaps 
usuccesfully), end give me some professional advice as what to do for 
the next incarnation of my awful program. any concrete suggestion will 
be *very* appreciated.

thank to all

bye



More information about the Python-list mailing list