Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

99 linhas
2.4 KiB

  1. """
  2. Analytical Skills
  3. Opgave: recursie
  4. (c) 2019 Hogeschool Utrecht
  5. Tijmen Muller (tijmen.muller@hu.nl)
  6. Let op! Je mag voor deze opgave geen extra modules importeren met 'import'.
  7. """
  8. def faculteit(n):
  9. # Base case
  10. if n == 0:
  11. return 1
  12. # Recursie
  13. else:
  14. return faculteit(0)
  15. def exponent(n):
  16. # Base case
  17. # Recursie
  18. return n
  19. def som(lst):
  20. return 1
  21. def palindroom(woord):
  22. return False
  23. """==============================================[ HU TESTRAAMWERK ]====================================================
  24. Onderstaand staan de tests voor je code -- hieronder mag je niets wijzigen!
  25. Je kunt je code testen door deze file te runnen of met behulp van pytest.
  26. """
  27. import math, random
  28. def test_faculteit():
  29. for x in range(6):
  30. assert faculteit(x) == math.factorial(x), \
  31. f"Fout: faculteit({x}) geeft {faculteit(x)} in plaats van {math.factorial(x)}"
  32. def test_exponent():
  33. for x in range(10):
  34. assert exponent(x) == 2**x, \
  35. f"Fout: exponent({x}) geeft {exponent(x)} in plaats van {2**x}"
  36. def test_som():
  37. for i in range(6):
  38. lst_test = random.sample(range(-10, 11), i)
  39. assert som(lst_test) == sum(lst_test), \
  40. f"Fout: som({lst_test}) geeft {som(lst_test)} in plaats van {sum(lst_test)}"
  41. def test_palindroom():
  42. testcases = [
  43. ("", True),
  44. ("radar", True),
  45. ("maandnaam", True),
  46. ("pollepel", False),
  47. ("Maandnaam", False)
  48. ]
  49. for testcase, res in testcases:
  50. assert palindroom(testcase) is res, f"Fout: palindroom({testcase}) geeft {palindroom(testcase)} in plaats van {res}"
  51. if __name__ == '__main__':
  52. try:
  53. print("\x1b[0;32m")
  54. test_faculteit()
  55. print("Je functie faculteit() doorstaat de tests!")
  56. test_exponent()
  57. print("Je functie exponent() doorstaat de tests!")
  58. test_som()
  59. print("Je functie som() doorstaat de tests!")
  60. test_palindroom()
  61. print("Je functie palindroom() doorstaat de tests!")
  62. print("\x1b[0;30m")
  63. x = input("Geef een woord: ")
  64. print("'" + x + "' is " + ("" if palindroom(x) else "g") + "een palindroom!")
  65. except AssertionError as ae:
  66. print("\x1b[0;31m")
  67. print(str(ae))