-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.py
More file actions
81 lines (69 loc) · 2.89 KB
/
printer.py
File metadata and controls
81 lines (69 loc) · 2.89 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class TermPrinter:
@staticmethod
def print(any_term, with_sign=False, sign_space=1):
if type(any_term) in [float, int]:
if any_term == 0:
return ""
else:
return TermPrinter.get_printable_coefficient(any_term)
sign = ""
if with_sign:
if any_term.get_coefficient() < 0:
sign = (" " * sign_space) + "-" + (" " * sign_space)
elif any_term.get_coefficient() > 0:
sign = (" " * sign_space) + "+" + (" " * sign_space)
any_term = abs(any_term) # The sign will be add already
if hasattr(any_term, "terms"):
return sign + TermPrinter.__print_multiple_alpha_term(abs(any_term))
elif hasattr(any_term, "is_equal_zero"):
return sign + TermPrinter.__print_alpha_term(abs(any_term))
else:
raise ValueError("The any_term parameter must be an actionable value.")
@staticmethod
def __print_alpha_term(term):
if term.get_coefficient() == 0:
return ""
elif term.get_exponent() == 0:
return term.get_printable_coefficient()
elif term.get_exponent != 0:
if abs(term.get_coefficient()) == 1:
return term.get_alpha() + TermPrinter.get_printable_exponent(term.get_exponent())
else:
return (term.get_printable_coefficient()
+ term.get_alpha()
+ TermPrinter.get_printable_exponent(term.get_exponent()))
@staticmethod
def __print_multiple_alpha_term(multiple_term):
if multiple_term.get_coefficient() == 0:
return ""
else:
coefficient = multiple_term.get_coefficient()
alp_exp = ""
for term in multiple_term.seperated_terms:
term.set_coefficient(1)
obj = TermPrinter.__print_alpha_term(term)
if not obj.isnumeric():
alp_exp += obj
if len(alp_exp) == 0:
return TermPrinter.get_printable_coefficient(coefficient)
else:
pr_coe = ""
if coefficient != 1:
pr_coe = TermPrinter.get_printable_coefficient(coefficient)
return pr_coe + alp_exp
@staticmethod
def get_printable_coefficient(_coe):
return str(int(_coe)) if float(_coe).is_integer() else str(_coe)
@staticmethod
def get_printable_exponent(_exp, is_regular=True):
if _exp in [0, 1] and is_regular:
return ""
minus = "⁻"
if _exp == -1 and is_regular:
return minus
exponents = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹']
abs_exponent = abs(_exp)
result = minus if _exp < 0 else ""
for i in str(abs_exponent):
result += exponents[int(i)]
return result