Created
October 18, 2013 16:31
-
-
Save iamricard/7044134 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial_mutante (base, jump = 1): | |
errors = { | |
'101': 'El valor debe ser positivo o cero.', | |
'102': 'El valor no puede ser superior a 10.', | |
'103': 'El valor debe ser entero.', | |
'201': 'El salto debe ser positivo.', | |
'202': 'El salto no puede ser superior a 5.', | |
'555': 'Error desconocido.', | |
} | |
# Lo ideal seria crear una clase per error, suposo | |
try: | |
t = float(base) | |
except: | |
raise Exception, errors['555'] | |
decimal = float(base) - int(float(base)) | |
if decimal: | |
raise Exception, errors['103'] | |
base = int(float(base)) | |
if (base > 10): | |
raise Exception, errors['102'] | |
if (base < 0): | |
raise Exception, errors['101'] | |
try: | |
jump = int(jump) | |
if (jump <= 0): | |
raise Exception, errors['201'] | |
except: | |
raise Exception, errors['555'] | |
if (jump > 5): | |
raise Exception, errors['202'] | |
try: | |
# Este segundo parametro tendra como valor por defecto 1. Si el | |
# valor del segundo parametro es superior o igual al valor del | |
# primer parametro, la funcion devolvera 1 | |
if (jump >= base): | |
return 1 | |
# El factorial_modificado(0) == factorial_modificado(1) == 1 | |
if not base or base == 1: | |
return 1 | |
start = base - jump | |
while (start > 0): | |
base = base * start | |
start -= jump | |
return base | |
except: | |
raise Exception, errors['555'] | |
arg1 = raw_input('Base:\n>') | |
arg2 = raw_input('Jump:\n>') | |
print(factorial_mutante(arg1, arg2)) | |
# >4 | |
# >2 | |
# 8 | |
# 12 | |
# 4 | |
# Traceback (most recent call last): | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 60, in <module> | |
# print(factorial_mutante(arg1, arg2)) | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 24, in factorial_mutante | |
# raise Exception, errors['102'] | |
# Exception: El valor no puede ser superior a 10. | |
# >9.3 | |
# >4 | |
# Traceback (most recent call last): | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 58, in <module> | |
# print(factorial_mutante(arg1, arg2)) | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 17, in factorial_mutante | |
# raise Exception, errors['103'] | |
# Exception: El valor debe ser entero. | |
# >a | |
# >5 | |
# Traceback (most recent call last): | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 58, in <module> | |
# print(factorial_mutante(arg1, arg2)) | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 14, in factorial_mutante | |
# raise Exception, errors['555'] | |
# Exception: Error desconocido. | |
# >8 | |
# >6 | |
# Traceback (most recent call last): | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 58, in <module> | |
# print(factorial_mutante(arg1, arg2)) | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 35, in factorial_mutante | |
# raise Exception, errors['202'] | |
# Exception: El salto no puede ser superior a 5. | |
# >-2 | |
# >4 | |
# Traceback (most recent call last): | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 58, in <module> | |
# print(factorial_mutante(arg1, arg2)) | |
# File "UF1-NF1-A03-Ricard-Sole.py", line 25, in factorial_mutante | |
# raise Exception, errors['101'] | |
# Exception: El valor debe ser positivo o cero. | |
# >4 | |
# >4 | |
# 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment