blob: cc74b5f166cef7ec3a242f74e0df1000278482a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# -*- coding: utf-8 -*-
"""
Contain the logic of fractions and comparations of fractions.
"""
import random
DENOMINATOR_MIN = 4
DENOMINATOR_MAX = 10
class Fraction(object):
def __init__(self, numerator=1, denominator=1):
self.numerator = numerator
self.denominator = denominator
class FractionLogic(object):
def __init__(self):
self.fraction = Fraction()
def generate(self):
"""Generate new fraction"""
if DENOMINATOR_MIN < 1:
raise Exception("DENOMINATOR_MIN need be greather than 0")
self.fraction.denominator = random.randrange(DENOMINATOR_MIN, DENOMINATOR_MAX)
self.fraction.numerator = random.randrange(0, self.fraction.denominator+1)
def get_current(self):
"""Return the current fraction, raise an exception if generate_fraction
hasn't called before"""
if self.fraction.denominator is None:
raise Exception("generate_fraction must be called before get_current_fraction")
return (self.fraction.numerator, self.fraction.denominator)
def is_equal(self, fraction):
"""Check if fraction is equal that the internal"""
if not(type(fraction) is Fraction and self.fraction.denominator is not None):
raise Exception("fraction must be a tuple of length 2")
if self.fraction.denominator is None:
raise Exception("generate_fraction must be called before is_equal")
return fraction.numerator * self.fraction.denominator == fraction.denominator * self.fraction.numerator
def __repr__(self):
if self.fraction.denominator is None:
return "<FractionLogic(Undefined)>"
return "<FractionLogic(%i,%i)>"%(self.fraction.numerator,self.fraction.denominator)
|