Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://azure.microsoft.com/en-us/free/
- https://portal.azure.com/#home
- https://cloud.google.com/free
- import sys
- def factorialRecursivo(pN:int):
- if(pN<0):
- return False
- else:
- if(pN==0):
- return 1
- else:
- return pN*factorialRecursivo(pN-1)
- #if-else
- #if-else
- #def factorialRecursivo
- *********************
- from my_code import factorialIterativo, factorialRecursivo
- iTestWithThisOne = 33
- rR = factorialRecursivo(iTestWithThisOne)
- rI = factorialIterativo(iTestWithThisOne)
- try:
- assert(rR==rI) #if NOT True, causes a runtime Exception
- print ("Passed the tests")
- except Exception as e:
- print ("Failed the tests")
- #try except
- def factorialIterativo(pN:int):
- if(pN<0):
- return False
- else:
- #1*2*3*4*...*pN
- iResultado:int=1
- for iFactor in range(1, pN+1):
- iResultado*=iFactor
- #for
- return iResultado
- #if-else
- #def factorialIterativo
- """
- python my_code.py 3
- sys.argv = [0=>"my_code.py" , 1=>3]
- """
- """
- É importante distinguir entre processamento Python
- por importação e por invocação CLI
- Quando a invocação é pela linha de comandos (CLI)
- o nome do módulo será __main__
- Podemos explorar isso.
- """
- bCLICall = __name__=="__main__"
- if (bCLICall):
- TAMANHO_ESPERADO = 2
- bOK = len(sys.argv) == TAMANHO_ESPERADO
- if (bOK):
- argumentoUtilizar = int(sys.argv[1])
- rRecursivo = factorialRecursivo(argumentoUtilizar)
- rIterativo = factorialIterativo(argumentoUtilizar)
- strMsg:str=f"O fatorial recursivo de {argumentoUtilizar} é {rRecursivo}\n"
- print(strMsg)
- strMsg=f"O fatorial iterativo de {argumentoUtilizar} é {rIterativo}\n"
- print(strMsg)
- else:
- print ("Pf, forneça um inteiro.")
- #if
Add Comment
Please, Sign In to add comment