In the the Perl Monks guide to the Monastery we are adviced to post here when we have "automated a part of your life that wouldn't have been possible without the power of Perl" or are "using Perl to do something unique and humorous", so here I go.

Last night when I got home from work, I started to help one of my sons to do his homeworks. In Sweden, every boy and girl at his age are supposed to learn how to multiply and to learn the answers of all equations where 1 to 10 are multiplied by 1 to 10. We call this to learn the multiplication tables.

We usually do this by me asking him "Whats 7 times 5?" and he aswers "35" and so on until we've done all the numbers. My son doesn't like to do this kind of repeating and unstimulating tasks (somtimes I wish he was less like his father :-) so he gets bored after a short while. After some more questions we are in a full scale war, me giving him all the "It's important to do your homework" talks and he giving me all the stubborn "I don't want to!" answers.

This isn't the way neither he nor me wants to spend our evenings. I rather spend them defeating him in some Nintendo game and he rather spend them defeating me. So we needed a solution that made him do his multiplications fast, without complain so we could have some time left for a Nintendo joust.

I thought that I might use my sons love for compuers to solve this, so last evening I did a 5 minute perl hack to let him do the multiplications on his own on his computer. It worked perfectly - no arguing - no irritation - just his fingers punching the answers to all the questions and me just looking over his shoulder.

Nothing fancy, nothing super cool - but kind of fun and something that automated a boring part of our lives. (Prompts translated to English here)
#!/usr/bin/perl -w use strict; use vars qw($answer $correct $num @arr $errs); $errs=0; # Build questions foreach my $i (1..10) { foreach my $j (1..10) { push (@arr,"$i * $j"); } } # Loop block { # Exit when all Q's are spliced away last unless defined $arr[0]; # Get a random question (This will leave # 10 times 10 as the last question asked. # Use @arr instead of $#arr if you don't # like that behaviour. Please refer to # chipmunk's post for an explanation of # why.) $num=int(rand $#arr); # Find the answer $correct=eval($arr[$num]); # Prompt for answer print "What is $arr[$num] ? \n"; # Answering block { $answer=<STDIN>; chomp $answer; redo unless $answer=~m/^\d+$/; } # Right or wrong ? if ($answer == $correct) { print "Good, that's correct.\n"; splice(@arr,$num,1); } else { print "No, that's not correct, $arr[$num] = $correct\n"; $errs++; } # Tell how much it's left print "".($#arr+1)." questions left.\n\n"; # And again... redo; } print "All done.\nYou made $errs misstakes.\n";


/brother t0mas

In reply to Create math homework problems by t0mas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.