Created
March 11, 2013 13:06
-
-
Save joshbode/5134079 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # simple script to practice mental arithmetic | |
| import random | |
| import operator | |
| import argparse | |
| import re | |
| answer_re = re.compile(r'^[-+]?\d+$') | |
| ops = ( | |
| ('+', operator.add), | |
| ('-', operator.sub), | |
| ('x', operator.mul), | |
| ) | |
| def sample(x): | |
| """Sample generator.""" | |
| while True: | |
| yield random.sample(x, 1)[0] | |
| def main(): | |
| """""" | |
| # set up argument parser | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--series', help="Series to drill.", type=int, default=None) | |
| parser.add_argument('--min', help="Minimum multiplier.", type=int, default=2) | |
| parser.add_argument('--max', help="Maximum multiplier.", type=int, default=12) | |
| args = parser.parse_args() | |
| errors = 0 | |
| try: | |
| n = 1 | |
| for label, op in sample(ops): | |
| attempt = 1 | |
| x = random.randint(args.min, args.max) | |
| y = args.series if args.series else random.randint(args.min, args.max) | |
| # flip arguments if drilling a particular series | |
| if args.series and random.random() < 0.5: | |
| x, y = y, x | |
| while True: | |
| answer = raw_input( | |
| "{0}/{1}: {2:>2} {3} {4:>2} = ".format(n, attempt, x, label, y) | |
| ) | |
| # check answer | |
| if answer_re.match(answer) and int(answer) == op(x, y): | |
| break | |
| else: | |
| errors += 1 | |
| attempt += 1 | |
| n += 1 | |
| except KeyboardInterrupt: | |
| print "\n\nErrors: {0}".format(errors) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment