Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:
I teach a general programming class, and I'm looking for interesting tasks to teach programming concepts.
One example is "produce English date suffixes from numbers", i.e.
where "suffix()" is a sub that the students have to come up with.for(1 .. 31){ print $_ . suffix($_) . "/n"; }
I like this task because
- it's relatively simple, although
- there's a gotcha because all numbers ending in "1" take "st" except 11, and the same for 2/12 and 3/13
- it kind of involves math but mostly just logical thinking
- TMTOWTDI -- I can think of about ten ways to do it, from the straightforward but clunky:
to the golf-y@suffixes = ( undef, 'st','nd','rd' ... etc); return $suffixes[$_[0]];$_[0]=~/(\b|[^1])(\d)$/;qw(0 st nd rd)[$2]or'th' - the content is familiar and practical, not abstract -- you might actually want someday to produce output like "last edited: 1st of April".
So, have monks got any examples of similar exercises which would be suitable for learners?
($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print
Back to
Seekers of Perl Wisdom