[Tutor] recursion
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Mon Aug 15 20:14:12 EDT 2022
Very true, Alan. It is tempting to teach something cool even before you
taught the basics.
I once tried to explain Random Forest algorithms to someone only to find out
they could not see what trees were yet!
(The above sounds like a joke but isn't. There really is a set of tools
available in python modules that use a forest of random trees that each try
to solve a problem and sort of patches together a reasonable way to solve
the problem using the forest of such trees. You do not need to understand
the individual decision trees to get the concept but it helps.)
[[REST CAN BE SKIPPED.]]
[[ACTUALLY THE ABOVE CAN BE SKIPPED TOO.]]
I once, seemingly many lives ago, taught Geometry (and English) at a private
school while still in College and as one of my majors was Mathematics I
decided to try a neat and elegant proof of the age-old theorem that an
isosceles triangle has the same angle in the vertices across from the two
sides of equal length. The fairly standard proof involves dropping an angle
bisector from the other of the three vertices to make effectively two
smaller triangles then use already taught techniques to prove the two
triangles are similar by SAS. In the process, you might label the initial
triangle as having vertices of A, B, C and add a D for the new point along
AB where you dropped a new line. You can then clearly mark which side and
which angle are equal and using the common third side, have a proof people
can visualize.
But is that an elegant proof? I mean it means getting your hands dirty by
creating a new line and maybe you have to prove the angle bisector stays in
bounds and who knows what else?
So I took these 10th graders and showed them an ELEGANT proof from a College
Geometry course I had taken. The entire proof involves taking the triangle
with vertices of A/B/C and leaving B alone but placing a B' alongside A and
an A' alongside B. You then tell them that the single triangle they are
seeing on the blackboard should be imagined as being two SUPERIMPOSED
triangles that are sort of a mirror image reflection over a vertical axis of
each other. One is triangle ABC and the other is A'BC' (change B to B' if it
makes you happier) and you now can show that side AB in one triangle is the
same as ... in the other and so on. Meaning since opposite sides are equal,
it is congruent to itself rotated or reflected as the sides are the same and
the angle is itself. So the two are congruent triangles and corresponding
angles are thus equal.
Now if you followed that (and nobody in that class did) then this is a
fairly simple and elegant proof. Luckily, I realized in time that this was
not working and switched to the more standard proof and learned a valuable
lesson about teaching to the level of the person rather than my level.
Oddly, teaching English was easier as it was not my native language (yet, it
is now, albeit quite a few others come close) and so my explanations and
choices were based at a level they could understand as I had myself had to
struggle to learn it.
[[SAFE TO READ BELOW.]]
For some people, recursion and other CS topics such as functional
programming or whatever you mean by Object Oriented Programming are a bit
like that. It requires twisting your mind around other approaches to problem
solving. I was trying to vaguely explain decorators to my wife (not a CS
person) and she was stuck a tad earlier with concepts of what it means to
feed a function to a function.
[[NEW TOPIC.]]
Which reminds me, if anyone is still here. There was a discussion about
measuring the time it takes to import various items into python and it was
done by analyzing the text of the file.
I wondered if a decorator might not be an interesting choice. I mean if your
interpreter reads "import foo" and if it then calls
importlib.import_module() or __import__() or some other such function, can
you perhaps intercept such calls with a decorator around them that does the
timing and reports it? If so, adding a temporary line or two of code above
your imports would turn the feature on and when satisfied, you comment it
out or remove it or perhaps have code restoring the original function.
This may not satisfy quite the same thing but is it in the ballpark and
doable?
Now obviously if the calls are explicit into the module, perhaps not. But
worth a try?
-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Alan Gauld via Tutor
Sent: Monday, August 15, 2022 7:24 PM
To: tutor at python.org
Subject: Re: [Tutor] recursion
On 15/08/2022 18:19, Mats Wichmann wrote:
>> There are things like traversing trees which are harder to do without
>> recursion but Fibonacci is absolutely trivial to do
The other common recursion function is factorial() and that too is nearly
trivial to do non-recursively. But traversing trees etc and other real-world
uses of recursion are a lot harder to teach! (You need to teach trees for a
start!)
I suspect the problem is that recursio is introduced too early in the
curriculum, but teachers like it because it's cool from an academic
standpoint and can help teach a regular approach to designing programs.
(see the online book "How to Design Programs" for an example of the
approach...http://htdp.org)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list