am_dot_com

CN 2022-03-16

Mar 16th, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. https://azure.microsoft.com/en-us/free/
  2. https://portal.azure.com/#home
  3.  
  4. https://cloud.google.com/free
  5.  
  6. import sys
  7.  
  8. def factorialRecursivo(pN:int):
  9. if(pN<0):
  10. return False
  11. else:
  12. if(pN==0):
  13. return 1
  14. else:
  15. return pN*factorialRecursivo(pN-1)
  16. #if-else
  17. #if-else
  18. #def factorialRecursivo
  19.  
  20. *********************
  21.  
  22.  
  23. from my_code import factorialIterativo, factorialRecursivo
  24.  
  25. iTestWithThisOne = 33
  26.  
  27. rR = factorialRecursivo(iTestWithThisOne)
  28. rI = factorialIterativo(iTestWithThisOne)
  29. try:
  30. assert(rR==rI) #if NOT True, causes a runtime Exception
  31. print ("Passed the tests")
  32. except Exception as e:
  33. print ("Failed the tests")
  34. #try except
  35.  
  36. def factorialIterativo(pN:int):
  37. if(pN<0):
  38. return False
  39. else:
  40. #1*2*3*4*...*pN
  41. iResultado:int=1
  42. for iFactor in range(1, pN+1):
  43. iResultado*=iFactor
  44. #for
  45. return iResultado
  46. #if-else
  47. #def factorialIterativo
  48.  
  49. """
  50. python my_code.py 3
  51. sys.argv = [0=>"my_code.py" , 1=>3]
  52. """
  53. """
  54. É importante distinguir entre processamento Python
  55. por importação e por invocação CLI
  56. Quando a invocação é pela linha de comandos (CLI)
  57. o nome do módulo será __main__
  58. Podemos explorar isso.
  59. """
  60.  
  61. bCLICall = __name__=="__main__"
  62.  
  63. if (bCLICall):
  64. TAMANHO_ESPERADO = 2
  65. bOK = len(sys.argv) == TAMANHO_ESPERADO
  66. if (bOK):
  67. argumentoUtilizar = int(sys.argv[1])
  68. rRecursivo = factorialRecursivo(argumentoUtilizar)
  69. rIterativo = factorialIterativo(argumentoUtilizar)
  70.  
  71. strMsg:str=f"O fatorial recursivo de {argumentoUtilizar} é {rRecursivo}\n"
  72. print(strMsg)
  73. strMsg=f"O fatorial iterativo de {argumentoUtilizar} é {rIterativo}\n"
  74. print(strMsg)
  75. else:
  76. print ("Pf, forneça um inteiro.")
  77. #if
Add Comment
Please, Sign In to add comment