Skip to content

Instantly share code, notes, and snippets.

@greenkey
Last active March 2, 2016 10:58
Show Gist options
  • Save greenkey/5261cc6ccc4699e95841 to your computer and use it in GitHub Desktop.
Save greenkey/5261cc6ccc4699e95841 to your computer and use it in GitHub Desktop.
To help my daughters learn fractions, I made an exercise generator. Now they hate me.
#!/usr/bin/python3
from random import randrange, random
from fractions import Fraction
from functools import reduce
from operator import mul
def pf(f):
'''Pretty print the fraction'''
return "{}/{}".format(f.numerator,f.denominator)
while True:
fractions = [Fraction( randrange(1,30), randrange(1,30) ) for i in range(randrange(2,6))]
final = reduce(mul, fractions)
if final.denominator <= 30:
print("{} = {}".format(" x ".join([pf(f) for f in fractions]),pf(final)))
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment