Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Create math homework problems

by t0mas (Priest)
on Jan 09, 2001 at 14:07 UTC ( [id://50652]=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Create math homework problems
by chipmunk (Parson) on Jan 09, 2001 at 21:29 UTC
    This may have been intentional, but I noted a gotcha in this line:     $num=int(rand $#arr); That will never pick the last index in @arr (as long as there is more than one element in @arr); 10 * 10 will always be the last question asked.

    A quick example: With four elements in @arr, $#arr is 3. rand(3) will return a number >= 0 and < 3. When you truncate the result with int(), you get 0, 1, or 2.

    I would have written that line as:     $num=int(rand @arr);

      I know that it leaves 10 times 10 as the last question and I thought leaving it as a 'grande finale' was kind of nice. I first considered some algorithm that favoured the lower numbers and there by increased the difficulty at the end of the program (my son thinks larger numbers are more difficult). But this was just a 5 minute hack, so i didn't put that much effort into it.

      I'll change the comment in the code to explain this behaviour. Thanks for your explanation.

      /brother t0mas

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://50652]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found