blob: 96dc5064516b6dc441abd335e401da5d1406b6f8 (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# -*- 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):
self.numerator = None
self.denominator = None
def __eq__(self,other_fraction):
if (self.numerator * other_fraction.denominator == self.denominator * other_fraction.numerator) and type(other_fraction) is Fraction:
return True
else:
return False
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 get_current_cake(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)
def is_equal(self, fraction):
"""DEPRECATED: 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)
|