remove tabs
This commit is contained in:
parent
08f493ae79
commit
80bdd86297
|
@ -3,26 +3,26 @@
|
||||||
|
|
||||||
|
|
||||||
def gcd(a, b):
|
def gcd(a, b):
|
||||||
"""Compute gcd(a,b)
|
"""Compute gcd(a,b)
|
||||||
|
|
||||||
:param a: first number
|
:param a: first number
|
||||||
:param b: second number
|
:param b: second number
|
||||||
:returns: the gcd
|
:returns: the gcd
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if a > b:
|
if a > b:
|
||||||
c = a % b
|
c = a % b
|
||||||
else:
|
else:
|
||||||
c = b % a
|
c = b % a
|
||||||
|
|
||||||
if c == 0:
|
if c == 0:
|
||||||
return min(a,b)
|
return min(a,b)
|
||||||
elif a == 1:
|
elif a == 1:
|
||||||
return b
|
return b
|
||||||
elif b == 1:
|
elif b == 1:
|
||||||
return a
|
return a
|
||||||
else:
|
else:
|
||||||
return gcd(min(a,b), c)
|
return gcd(min(a,b), c)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
440
calculus.py
440
calculus.py
|
@ -7,315 +7,315 @@ from generic import Stack
|
||||||
|
|
||||||
|
|
||||||
def infixToPostfix(infixExp):
|
def infixToPostfix(infixExp):
|
||||||
"""Transform an infix expression into postfix expression
|
"""Transform an infix expression into postfix expression
|
||||||
|
|
||||||
:param infixExp: an infix expression (caracters must be separate by a space)
|
:param infixExp: an infix expression (caracters must be separate by a space)
|
||||||
:returns: the corresponding postfix expression
|
:returns: the corresponding postfix expression
|
||||||
|
|
||||||
:Example:
|
:Example:
|
||||||
|
|
||||||
>>> infixToPostfix("1 + 2")
|
>>> infixToPostfix("1 + 2")
|
||||||
'1 2 +'
|
'1 2 +'
|
||||||
|
|
||||||
>>> infixToPostfix("1 * 2 + 3")
|
>>> infixToPostfix("1 * 2 + 3")
|
||||||
'1 2 * 3 +'
|
'1 2 * 3 +'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
priority = {"*" : 3, "/": 3, "+": 2, "-":2, "(": 1}
|
priority = {"*" : 3, "/": 3, "+": 2, "-":2, "(": 1}
|
||||||
|
|
||||||
opStack = Stack()
|
opStack = Stack()
|
||||||
postfixList = []
|
postfixList = []
|
||||||
|
|
||||||
tokenList = infixExp.split(" ")
|
tokenList = infixExp.split(" ")
|
||||||
|
|
||||||
for token in tokenList:
|
for token in tokenList:
|
||||||
if token == "(":
|
if token == "(":
|
||||||
opStack.push(token)
|
opStack.push(token)
|
||||||
elif token == ")":
|
elif token == ")":
|
||||||
topToken = opStack.pop()
|
topToken = opStack.pop()
|
||||||
while topToken != "(":
|
while topToken != "(":
|
||||||
postfixList.append(topToken)
|
postfixList.append(topToken)
|
||||||
topToken = opStack.pop()
|
topToken = opStack.pop()
|
||||||
elif token in "+-*/":
|
elif token in "+-*/":
|
||||||
while (not opStack.isEmpty()) and (priority[opStack.peek()] >= priority[token]):
|
while (not opStack.isEmpty()) and (priority[opStack.peek()] >= priority[token]):
|
||||||
postfixList.append(opStack.pop())
|
postfixList.append(opStack.pop())
|
||||||
opStack.push(token)
|
opStack.push(token)
|
||||||
else:
|
else:
|
||||||
postfixList.append(token)
|
postfixList.append(token)
|
||||||
|
|
||||||
while not opStack.isEmpty():
|
while not opStack.isEmpty():
|
||||||
postfixList.append(opStack.pop())
|
postfixList.append(opStack.pop())
|
||||||
|
|
||||||
return " ".join(postfixList)
|
return " ".join(postfixList)
|
||||||
|
|
||||||
def computePostfix(postfixExp):
|
def computePostfix(postfixExp):
|
||||||
"""Compute a postfix expression
|
"""Compute a postfix expression
|
||||||
|
|
||||||
:param postfixExp: a postfix expression
|
:param postfixExp: a postfix expression
|
||||||
:returns: the result of the expression
|
:returns: the result of the expression
|
||||||
|
|
||||||
"""
|
"""
|
||||||
print(postfixToInfix(postfixExp))
|
print(postfixToInfix(postfixExp))
|
||||||
# where to save numbers or
|
# where to save numbers or
|
||||||
operandeStack = Stack()
|
operandeStack = Stack()
|
||||||
|
|
||||||
tokenList = postfixExp.split(" ")
|
tokenList = postfixExp.split(" ")
|
||||||
|
|
||||||
for (i,token) in enumerate(tokenList):
|
for (i,token) in enumerate(tokenList):
|
||||||
if token in "+-*/":
|
if token in "+-*/":
|
||||||
op2 = operandeStack.pop()
|
op2 = operandeStack.pop()
|
||||||
op1 = operandeStack.pop()
|
op1 = operandeStack.pop()
|
||||||
res = doMath(token, op1, op2)
|
res = doMath(token, op1, op2)
|
||||||
operandeStack.push(res)
|
operandeStack.push(res)
|
||||||
|
|
||||||
#print("Operation: {op1} {op} {op2}".format(op1 = op1, op = token, op2 = op2))
|
#print("Operation: {op1} {op} {op2}".format(op1 = op1, op = token, op2 = op2))
|
||||||
#print(operandeStack)
|
#print(operandeStack)
|
||||||
#print(tokenList[i+1:])
|
#print(tokenList[i+1:])
|
||||||
newPostfix = " ".join(operandeStack + tokenList[i+1:])
|
newPostfix = " ".join(operandeStack + tokenList[i+1:])
|
||||||
print(postfixToInfix(newPostfix))
|
print(postfixToInfix(newPostfix))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
operandeStack.push(token)
|
operandeStack.push(token)
|
||||||
|
|
||||||
return operandeStack.pop()
|
return operandeStack.pop()
|
||||||
|
|
||||||
def computePostfixBis(postfixExp):
|
def computePostfixBis(postfixExp):
|
||||||
"""Compute a postfix expression like a good student
|
"""Compute a postfix expression like a good student
|
||||||
|
|
||||||
:param postfixExp: a postfix expression
|
:param postfixExp: a postfix expression
|
||||||
:returns: the result of the expression
|
:returns: the result of the expression
|
||||||
|
|
||||||
"""
|
"""
|
||||||
print(postfixToInfix(postfixExp))
|
print(postfixToInfix(postfixExp))
|
||||||
# where to save numbers or
|
# where to save numbers or
|
||||||
operandeStack = Stack()
|
operandeStack = Stack()
|
||||||
|
|
||||||
tokenList = postfixExp.split(" ")
|
tokenList = postfixExp.split(" ")
|
||||||
|
|
||||||
# On fait le calcul jusqu'à n'avoir plus qu'un élément
|
# On fait le calcul jusqu'à n'avoir plus qu'un élément
|
||||||
while len(tokenList) > 1:
|
while len(tokenList) > 1:
|
||||||
tmpTokenList = []
|
tmpTokenList = []
|
||||||
# on va chercher les motifs du genre A B + pour les calculer
|
# on va chercher les motifs du genre A B + pour les calculer
|
||||||
while len(tokenList) > 2:
|
while len(tokenList) > 2:
|
||||||
if isNumber(tokenList[0]) and isNumber(tokenList[1]) and tokenList[2] in "+-*/":
|
if isNumber(tokenList[0]) and isNumber(tokenList[1]) and tokenList[2] in "+-*/":
|
||||||
# S'il y a une opération à faire
|
# S'il y a une opération à faire
|
||||||
op1 = tokenList[0]
|
op1 = tokenList[0]
|
||||||
op2 = tokenList[1]
|
op2 = tokenList[1]
|
||||||
token = tokenList[2]
|
token = tokenList[2]
|
||||||
res = doMath(token, op1, op2)
|
res = doMath(token, op1, op2)
|
||||||
|
|
||||||
tmpTokenList.append(res)
|
tmpTokenList.append(res)
|
||||||
|
|
||||||
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
||||||
del tokenList[0:3]
|
del tokenList[0:3]
|
||||||
else:
|
else:
|
||||||
tmpTokenList.append(tokenList[0])
|
tmpTokenList.append(tokenList[0])
|
||||||
|
|
||||||
del tokenList[0]
|
del tokenList[0]
|
||||||
tmpTokenList += tokenList
|
tmpTokenList += tokenList
|
||||||
|
|
||||||
tokenList = tmpTokenList.copy()
|
tokenList = tmpTokenList.copy()
|
||||||
print(postfixToInfix(" ".join(tokenList)))
|
print(postfixToInfix(" ".join(tokenList)))
|
||||||
|
|
||||||
return tokenList[0]
|
return tokenList[0]
|
||||||
|
|
||||||
def isNumber(exp):
|
def isNumber(exp):
|
||||||
"""Check if the expression can be a number
|
"""Check if the expression can be a number
|
||||||
|
|
||||||
:param exp: an expression
|
:param exp: an expression
|
||||||
:returns: True if the expression can be a number and false otherwise
|
:returns: True if the expression can be a number and false otherwise
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tokenList = exp.split(" ")
|
tokenList = exp.split(" ")
|
||||||
if len(tokenList) != 1:
|
if len(tokenList) != 1:
|
||||||
# si l'expression est trop longue pour être un chiffre
|
# si l'expression est trop longue pour être un chiffre
|
||||||
return 0
|
return 0
|
||||||
return exp.isdigit() or (exp[0] == "-" and exp[1:].isdigit())
|
return exp.isdigit() or (exp[0] == "-" and exp[1:].isdigit())
|
||||||
|
|
||||||
|
|
||||||
def doMath(op, op1, op2):
|
def doMath(op, op1, op2):
|
||||||
"""Compute "op1 op op2"
|
"""Compute "op1 op op2"
|
||||||
|
|
||||||
:param op: operator
|
:param op: operator
|
||||||
:param op1: first operande
|
:param op1: first operande
|
||||||
:param op2: second operande
|
:param op2: second operande
|
||||||
:returns: string representing the result
|
:returns: string representing the result
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return str(eval(op1 + op + op2))
|
return str(eval(op1 + op + op2))
|
||||||
|
|
||||||
|
|
||||||
def postfixToInfix(postfixExp):
|
def postfixToInfix(postfixExp):
|
||||||
"""Transforme postfix expression into infix expression
|
"""Transforme postfix expression into infix expression
|
||||||
|
|
||||||
:param postfixExp: a postfix expression
|
:param postfixExp: a postfix expression
|
||||||
:returns: the corresponding infix expression
|
:returns: the corresponding infix expression
|
||||||
|
|
||||||
"""
|
"""
|
||||||
operandeStack = Stack()
|
operandeStack = Stack()
|
||||||
|
|
||||||
tokenList = postfixExp.split(" ")
|
tokenList = postfixExp.split(" ")
|
||||||
|
|
||||||
for (i,token) in enumerate(tokenList):
|
for (i,token) in enumerate(tokenList):
|
||||||
if token in "+-*/":
|
if token in "+-*/":
|
||||||
op2 = operandeStack.pop()
|
op2 = operandeStack.pop()
|
||||||
if needPar(op2, token, "after"):
|
if needPar(op2, token, "after"):
|
||||||
op2 = "( " + op2 + " )"
|
op2 = "( " + op2 + " )"
|
||||||
op1 = operandeStack.pop()
|
op1 = operandeStack.pop()
|
||||||
if needPar(op1, token, "before"):
|
if needPar(op1, token, "before"):
|
||||||
op1 = "( " + op1 + " )"
|
op1 = "( " + op1 + " )"
|
||||||
res = "{op1} {op} {op2}".format(op1 = op1, op = token, op2 = op2)
|
res = "{op1} {op} {op2}".format(op1 = op1, op = token, op2 = op2)
|
||||||
|
|
||||||
operandeStack.push(res)
|
operandeStack.push(res)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
operandeStack.push(token)
|
operandeStack.push(token)
|
||||||
|
|
||||||
return operandeStack.pop()
|
return operandeStack.pop()
|
||||||
|
|
||||||
def needPar(operande, operator, posi = "after"):
|
def needPar(operande, operator, posi = "after"):
|
||||||
"""Says whether or not the operande needs parenthesis
|
"""Says whether or not the operande needs parenthesis
|
||||||
|
|
||||||
:param operande: the operande
|
:param operande: the operande
|
||||||
:param operator: the operator
|
:param operator: the operator
|
||||||
:param posi: "after"(default) if the operande will be after the operator, "before" othewise
|
:param posi: "after"(default) if the operande will be after the operator, "before" othewise
|
||||||
:returns: bollean
|
:returns: bollean
|
||||||
"""
|
"""
|
||||||
priority = {"*" : 3, "/": 3, "+": 2, "-":2}
|
priority = {"*" : 3, "/": 3, "+": 2, "-":2}
|
||||||
|
|
||||||
if isNumber(operande) and "-" in operande:
|
if isNumber(operande) and "-" in operande:
|
||||||
return 1
|
return 1
|
||||||
elif not isNumber(operande):
|
elif not isNumber(operande):
|
||||||
# Si c'est une grande expression ou un chiffre négatif
|
# Si c'est une grande expression ou un chiffre négatif
|
||||||
stand_alone = get_main_op(operande)
|
stand_alone = get_main_op(operande)
|
||||||
# Si la priorité de l'operande est plus faible que celle de l'opérateur
|
# Si la priorité de l'operande est plus faible que celle de l'opérateur
|
||||||
minor_priority = priority[get_main_op(operande)] < priority[operator]
|
minor_priority = priority[get_main_op(operande)] < priority[operator]
|
||||||
# Si l'opérateur est -/ pour after ou juste / pour before
|
# Si l'opérateur est -/ pour after ou juste / pour before
|
||||||
special = (operator in "-/" and posi == "after") or (operator in "/" and posi == "before")
|
special = (operator in "-/" and posi == "after") or (operator in "/" and posi == "before")
|
||||||
|
|
||||||
return stand_alone and (minor_priority or special)
|
return stand_alone and (minor_priority or special)
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def get_main_op(exp):
|
def get_main_op(exp):
|
||||||
"""Getting the main operation of the expression
|
"""Getting the main operation of the expression
|
||||||
|
|
||||||
:param exp: the expression
|
:param exp: the expression
|
||||||
:returns: the main operation (+, -, * or /) or 0 if the expression is only one element
|
:returns: the main operation (+, -, * or /) or 0 if the expression is only one element
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
priority = {"*" : 3, "/": 3, "+": 2, "-":2}
|
priority = {"*" : 3, "/": 3, "+": 2, "-":2}
|
||||||
|
|
||||||
parStack = Stack()
|
parStack = Stack()
|
||||||
tokenList = exp.split(" ")
|
tokenList = exp.split(" ")
|
||||||
|
|
||||||
if len(tokenList) == 1:
|
if len(tokenList) == 1:
|
||||||
# Si l'expression n'est qu'un élément
|
# Si l'expression n'est qu'un élément
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
main_op = []
|
main_op = []
|
||||||
|
|
||||||
for token in tokenList:
|
for token in tokenList:
|
||||||
if token == "(":
|
if token == "(":
|
||||||
parStack.push(token)
|
parStack.push(token)
|
||||||
elif token == ")":
|
elif token == ")":
|
||||||
parStack.pop()
|
parStack.pop()
|
||||||
elif token in "+-*/" and parStack.isEmpty():
|
elif token in "+-*/" and parStack.isEmpty():
|
||||||
main_op.append(token)
|
main_op.append(token)
|
||||||
|
|
||||||
return min(main_op, key = lambda s: priority[s])
|
return min(main_op, key = lambda s: priority[s])
|
||||||
|
|
||||||
|
|
||||||
def expand_list(list_list):
|
def expand_list(list_list):
|
||||||
"""Expand list of list
|
"""Expand list of list
|
||||||
|
|
||||||
:param list: the list to expande
|
:param list: the list to expande
|
||||||
:returns: list of expanded lists
|
:returns: list of expanded lists
|
||||||
|
|
||||||
:Example:
|
:Example:
|
||||||
|
|
||||||
>>> expand_list([1,2,[3,4],5,[6,7,8]])
|
>>> expand_list([1,2,[3,4],5,[6,7,8]])
|
||||||
[[1, 2, 3, 5, 6], [1, 2, 4, 5, 7], [1, 2, 4, 5, 8]]
|
[[1, 2, 3, 5, 6], [1, 2, 4, 5, 7], [1, 2, 4, 5, 8]]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
list_in_list = [i for i in list_list if type(i) == list].copy()
|
list_in_list = [i for i in list_list if type(i) == list].copy()
|
||||||
nbr_ans_list = max([len(i) for i in list_in_list])
|
nbr_ans_list = max([len(i) for i in list_in_list])
|
||||||
|
|
||||||
ans = [list_list.copy() for i in range(nbr_ans_list)]
|
ans = [list_list.copy() for i in range(nbr_ans_list)]
|
||||||
for (i,l) in enumerate(ans):
|
for (i,l) in enumerate(ans):
|
||||||
for (j,e) in enumerate(l):
|
for (j,e) in enumerate(l):
|
||||||
if type(e) == list:
|
if type(e) == list:
|
||||||
ans[i][j] = e[min(i,len(e)-1)]
|
ans[i][j] = e[min(i,len(e)-1)]
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def debug_var(name, var):
|
def debug_var(name, var):
|
||||||
"""print the name of the variable and the value
|
"""print the name of the variable and the value
|
||||||
|
|
||||||
:param name: the name of the variable
|
:param name: the name of the variable
|
||||||
:param var: the variable we want information
|
:param var: the variable we want information
|
||||||
"""
|
"""
|
||||||
print(name, ": ", var)
|
print(name, ": ", var)
|
||||||
|
|
||||||
|
|
||||||
def test(exp):
|
def test(exp):
|
||||||
"""Make various test on an expression
|
"""Make various test on an expression
|
||||||
|
|
||||||
"""
|
"""
|
||||||
print("-------------")
|
print("-------------")
|
||||||
print("Expression ",exp)
|
print("Expression ",exp)
|
||||||
postfix = infixToPostfix(exp)
|
postfix = infixToPostfix(exp)
|
||||||
#print("Postfix " , postfix)
|
#print("Postfix " , postfix)
|
||||||
#print(computePostfix(postfix))
|
#print(computePostfix(postfix))
|
||||||
#print("Bis")
|
#print("Bis")
|
||||||
print(computePostfixBis(postfix))
|
print(computePostfixBis(postfix))
|
||||||
#print(postfixToInfix(postfix))
|
#print(postfixToInfix(postfix))
|
||||||
#print(get_main_op(exp))
|
#print(get_main_op(exp))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
#exp = "1 + 3 * 5"
|
#exp = "1 + 3 * 5"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
|
|
||||||
#exp = "2 * 3 * 3 * 5"
|
#exp = "2 * 3 * 3 * 5"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
|
|
||||||
#exp = "2 * 3 + 3 * 5"
|
#exp = "2 * 3 + 3 * 5"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
|
|
||||||
#exp = "2 * ( 3 + 4 ) + 3 * 5"
|
#exp = "2 * ( 3 + 4 ) + 3 * 5"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
|
#exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
|
#exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
|
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "2 + 5 * ( 3 - 4 )"
|
#exp = "2 + 5 * ( 3 - 4 )"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "( 2 + 5 ) * ( 3 - 4 )"
|
#exp = "( 2 + 5 ) * ( 3 - 4 )"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "( 2 + 5 ) * ( 3 * 4 )"
|
#exp = "( 2 + 5 ) * ( 3 * 4 )"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
#
|
#
|
||||||
#exp = "( 2 + 5 ) / ( 3 * 4 )"
|
#exp = "( 2 + 5 ) / ( 3 * 4 )"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
|
|
||||||
#print(expand_list([1,2,['a','b','c'], 3, ['d','e']]))
|
#print(expand_list([1,2,['a','b','c'], 3, ['d','e']]))
|
||||||
|
|
||||||
## Ce denier pose un soucis. Pour le faire marcher il faudrai implémenter le calcul avec les fractions
|
## Ce denier pose un soucis. Pour le faire marcher il faudrai implémenter le calcul avec les fractions
|
||||||
#exp = "( 2 + 5 ) / 3 * 4"
|
#exp = "( 2 + 5 ) / 3 * 4"
|
||||||
#test(exp)
|
#test(exp)
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
271
fraction.py
271
fraction.py
|
@ -5,184 +5,185 @@ from calculus import isNumber
|
||||||
from arithmetic import gcd
|
from arithmetic import gcd
|
||||||
|
|
||||||
class Fraction(object):
|
class Fraction(object):
|
||||||
"""Fractions!"""
|
"""Fractions!"""
|
||||||
|
|
||||||
def __init__(self, num, denom = 1):
|
def __init__(self, num, denom = 1):
|
||||||
"""To initiate a fraction we need a numerator and a denominator
|
"""To initiate a fraction we need a numerator and a denominator
|
||||||
|
|
||||||
:param num: the numerator
|
:param num: the numerator
|
||||||
:param denom: the denominator
|
:param denom: the denominator
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self._num = num
|
self._num = num
|
||||||
self._denom = denom
|
self._denom = denom
|
||||||
|
|
||||||
def simplify(self):
|
def simplify(self):
|
||||||
"""Simplify the fraction
|
"""Simplify the fraction
|
||||||
|
|
||||||
:returns: steps to simplify the fraction or the fraction if there is nothing to do
|
:returns: steps to simplify the fraction or the fraction if there is nothing to do
|
||||||
"""
|
"""
|
||||||
steps = [str(self)]
|
steps = [str(self)]
|
||||||
|
|
||||||
if self._denom < 0:
|
if self._denom < 0:
|
||||||
self._num = - self._num
|
self._num = - self._num
|
||||||
self._denom = - self._denom
|
self._denom = - self._denom
|
||||||
steps += [str(self)]
|
steps += [str(self)]
|
||||||
|
|
||||||
gcd_ = gcd(abs(self._num), abs(self._denom))
|
gcd_ = gcd(abs(self._num), abs(self._denom))
|
||||||
if self._num == self._denom:
|
if self._num == self._denom:
|
||||||
self._num = 1
|
self._num = 1
|
||||||
self._denom = 1
|
self._denom = 1
|
||||||
steps += [str(self)]
|
steps += [str(self)]
|
||||||
elif gcd_ != 1:
|
|
||||||
|
|
||||||
self._num, self._denom = self._num // gcd_ , self._denom // gcd_
|
elif gcd_ != 1:
|
||||||
steps += ["( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = self._num, reste2 = self._denom, gcd = gcd_)]
|
self._num, self._denom = self._num // gcd_ , self._denom // gcd_
|
||||||
steps += [str(self)]
|
steps += ["( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = self._num, reste2 = self._denom, gcd = gcd_)]
|
||||||
return steps
|
steps += [str(self)]
|
||||||
|
|
||||||
def __str__(self):
|
return steps
|
||||||
if self._denom == 1:
|
|
||||||
return str(self._num)
|
|
||||||
else:
|
|
||||||
return str(self._num) + " / " + str(self._denom)
|
|
||||||
|
|
||||||
def __add__(self, other):
|
def __str__(self):
|
||||||
if type(other) == Fraction:
|
if self._denom == 1:
|
||||||
#cool
|
return str(self._num)
|
||||||
number = other
|
else:
|
||||||
else:
|
return str(self._num) + " / " + str(self._denom)
|
||||||
number = Fraction(other)
|
|
||||||
|
|
||||||
steps = ["{frac1} + {frac2}".format(frac1 = self, frac2 = number)]
|
def __add__(self, other):
|
||||||
|
if type(other) == Fraction:
|
||||||
|
#cool
|
||||||
|
number = other
|
||||||
|
else:
|
||||||
|
number = Fraction(other)
|
||||||
|
|
||||||
if self._denom == number._denom:
|
steps = ["{frac1} + {frac2}".format(frac1 = self, frac2 = number)]
|
||||||
com_denom = self._denom
|
|
||||||
num1 = self._num
|
|
||||||
num2 = number._num
|
|
||||||
|
|
||||||
else:
|
if self._denom == number._denom:
|
||||||
gcd_denom = gcd(self._denom, number._denom)
|
com_denom = self._denom
|
||||||
coef1 = number._denom // gcd_denom
|
num1 = self._num
|
||||||
coef2 = self._denom // gcd_denom
|
num2 = number._num
|
||||||
|
|
||||||
steps += ["( {num1} * {coef1} ) / ( {den1} * {coef1} ) + ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2)]
|
else:
|
||||||
|
gcd_denom = gcd(self._denom, number._denom)
|
||||||
|
coef1 = number._denom // gcd_denom
|
||||||
|
coef2 = self._denom // gcd_denom
|
||||||
|
|
||||||
com_denom = self._denom * coef1
|
steps += ["( {num1} * {coef1} ) / ( {den1} * {coef1} ) + ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2)]
|
||||||
num1 = self._num * coef1
|
|
||||||
num2 = number._num * coef2
|
|
||||||
|
|
||||||
steps += ["( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)]
|
com_denom = self._denom * coef1
|
||||||
|
num1 = self._num * coef1
|
||||||
|
num2 = number._num * coef2
|
||||||
|
|
||||||
num = num1 + num2
|
steps += ["( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)]
|
||||||
|
|
||||||
ans_frac = Fraction(num, com_denom)
|
num = num1 + num2
|
||||||
steps += ans_frac.simplify()
|
|
||||||
|
|
||||||
return steps
|
ans_frac = Fraction(num, com_denom)
|
||||||
|
steps += ans_frac.simplify()
|
||||||
|
|
||||||
def __sub__(self, other):
|
return steps
|
||||||
if type(other) == Fraction:
|
|
||||||
#cool
|
|
||||||
number = other
|
|
||||||
else:
|
|
||||||
number = Fraction(other)
|
|
||||||
|
|
||||||
steps = ["{frac1} - {frac2}".format(frac1 = self, frac2 = number)]
|
def __sub__(self, other):
|
||||||
|
if type(other) == Fraction:
|
||||||
|
#cool
|
||||||
|
number = other
|
||||||
|
else:
|
||||||
|
number = Fraction(other)
|
||||||
|
|
||||||
if self._denom == number._denom:
|
steps = ["{frac1} - {frac2}".format(frac1 = self, frac2 = number)]
|
||||||
com_denom = self._denom
|
|
||||||
num1 = self._num
|
|
||||||
num2 = number._num
|
|
||||||
|
|
||||||
else:
|
if self._denom == number._denom:
|
||||||
gcd_denom = gcd(self._denom, number._denom)
|
com_denom = self._denom
|
||||||
coef1 = number._denom // gcd_denom
|
num1 = self._num
|
||||||
coef2 = self._denom // gcd_denom
|
num2 = number._num
|
||||||
|
|
||||||
steps += ["( {num1} * {coef1} ) / ( {den1} * {coef1} ) - ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2)]
|
else:
|
||||||
|
gcd_denom = gcd(self._denom, number._denom)
|
||||||
|
coef1 = number._denom // gcd_denom
|
||||||
|
coef2 = self._denom // gcd_denom
|
||||||
|
|
||||||
com_denom = self._denom * coef1
|
steps += ["( {num1} * {coef1} ) / ( {den1} * {coef1} ) - ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2)]
|
||||||
num1 = self._num * coef1
|
|
||||||
num2 = number._num * coef2
|
|
||||||
|
|
||||||
steps += ["( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)]
|
com_denom = self._denom * coef1
|
||||||
|
num1 = self._num * coef1
|
||||||
|
num2 = number._num * coef2
|
||||||
|
|
||||||
num = num1 - num2
|
steps += ["( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)]
|
||||||
|
|
||||||
ans_frac = Fraction(num, com_denom)
|
num = num1 - num2
|
||||||
steps += ans_frac.simplify()
|
|
||||||
|
|
||||||
return steps
|
ans_frac = Fraction(num, com_denom)
|
||||||
|
steps += ans_frac.simplify()
|
||||||
|
|
||||||
def __mul__(self, other):
|
return steps
|
||||||
if type(other) == Fraction:
|
|
||||||
#cool
|
|
||||||
number = other
|
|
||||||
else:
|
|
||||||
number = Fraction(other)
|
|
||||||
|
|
||||||
steps = ["( {frac1} ) * ( {frac2} )".format(frac1 = self, frac2 = number)]
|
def __mul__(self, other):
|
||||||
steps += ["( {num1} * {num2} ) / ( {denom1} * {denom2} )".format(num1 = self._num, num2 = number._num, denom1 = self._denom, denom2 = number._denom)]
|
if type(other) == Fraction:
|
||||||
|
#cool
|
||||||
|
number = other
|
||||||
|
else:
|
||||||
|
number = Fraction(other)
|
||||||
|
|
||||||
num = self._num * number._num
|
steps = ["( {frac1} ) * ( {frac2} )".format(frac1 = self, frac2 = number)]
|
||||||
denom = self._denom * number._denom
|
steps += ["( {num1} * {num2} ) / ( {denom1} * {denom2} )".format(num1 = self._num, num2 = number._num, denom1 = self._denom, denom2 = number._denom)]
|
||||||
|
|
||||||
ans_frac = Fraction(num, denom)
|
num = self._num * number._num
|
||||||
steps += ans_frac.simplify()
|
denom = self._denom * number._denom
|
||||||
|
|
||||||
return steps
|
ans_frac = Fraction(num, denom)
|
||||||
|
steps += ans_frac.simplify()
|
||||||
|
|
||||||
def __truediv__(self, other):
|
return steps
|
||||||
if type(other) == Fraction:
|
|
||||||
#cool
|
|
||||||
number = other
|
|
||||||
else:
|
|
||||||
number = Fraction(other)
|
|
||||||
|
|
||||||
steps = ["( {frac1} ) / ( {frac2} )".format(frac1 = self, frac2 = number)]
|
def __truediv__(self, other):
|
||||||
number = Fraction(number._denom, number._num)
|
if type(other) == Fraction:
|
||||||
steps += self * number
|
#cool
|
||||||
|
number = other
|
||||||
|
else:
|
||||||
|
number = Fraction(other)
|
||||||
|
|
||||||
return steps
|
steps = ["( {frac1} ) / ( {frac2} )".format(frac1 = self, frac2 = number)]
|
||||||
|
number = Fraction(number._denom, number._num)
|
||||||
|
steps += self * number
|
||||||
|
|
||||||
|
return steps
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
f = Fraction(34, 12)
|
f = Fraction(34, 12)
|
||||||
g = Fraction(1,5)
|
g = Fraction(1,5)
|
||||||
h = Fraction(-1,5)
|
h = Fraction(-1,5)
|
||||||
t = Fraction(-4,5)
|
t = Fraction(-4,5)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (f - 1):
|
for i in (f - 1):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (f + 1):
|
for i in (f + 1):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (f + g):
|
for i in (f + g):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (f - g):
|
for i in (f - g):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (f * g):
|
for i in (f * g):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (h + t):
|
for i in (h + t):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (h - t):
|
for i in (h - t):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (h * t):
|
for i in (h * t):
|
||||||
print(i)
|
print(i)
|
||||||
print("---------")
|
print("---------")
|
||||||
for i in (h / t):
|
for i in (h / t):
|
||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
#print(f.simplify())
|
#print(f.simplify())
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
|
74
generic.py
74
generic.py
|
@ -3,59 +3,59 @@
|
||||||
|
|
||||||
|
|
||||||
class Stack(object):
|
class Stack(object):
|
||||||
"""Docstring for Stack """
|
"""Docstring for Stack """
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""@todo: to be defined1 """
|
"""@todo: to be defined1 """
|
||||||
self.items = []
|
self.items = []
|
||||||
|
|
||||||
def pushFromList(self, list):
|
def pushFromList(self, list):
|
||||||
"""Push the list in the stack
|
"""Push the list in the stack
|
||||||
|
|
||||||
:param list: a list
|
:param list: a list
|
||||||
"""
|
"""
|
||||||
for i in list[::-1]:
|
for i in list[::-1]:
|
||||||
self.push(i)
|
self.push(i)
|
||||||
|
|
||||||
def isEmpty(self):
|
def isEmpty(self):
|
||||||
""" Says if the stack is empty
|
""" Says if the stack is empty
|
||||||
:returns: @todo
|
:returns: @todo
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return self.items == []
|
return self.items == []
|
||||||
|
|
||||||
def push(self, item):
|
def push(self, item):
|
||||||
"""Push an item in the stack
|
"""Push an item in the stack
|
||||||
|
|
||||||
:param item: @todo
|
:param item: @todo
|
||||||
:returns: @todo
|
:returns: @todo
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.items.append(item)
|
self.items.append(item)
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
"""Getting the last item and remove it
|
"""Getting the last item and remove it
|
||||||
:returns: last item
|
:returns: last item
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return self.items.pop()
|
return self.items.pop()
|
||||||
|
|
||||||
def peek(self, posi = 0):
|
def peek(self, posi = 0):
|
||||||
"""Getting the last item
|
"""Getting the last item
|
||||||
:param posi: which item to peek 0 (last) 1 (the onebefore the last)...
|
:param posi: which item to peek 0 (last) 1 (the onebefore the last)...
|
||||||
:returns: the item
|
:returns: the item
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return self.items[-1 - posi]
|
return self.items[-1 - posi]
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.items)
|
return len(self.items)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.items) + " -> "
|
return str(self.items) + " -> "
|
||||||
|
|
||||||
def __add__(self, addList):
|
def __add__(self, addList):
|
||||||
return self.items + addList
|
return self.items + addList
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
|
Loading…
Reference in New Issue