You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 line
3.3 KiB

  1. """
  2. Analytical Skills
  3. Opgave: selection sort
  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 swap(lst, index1, index2):
  9. """ Verwisselt de waardes op positie index1 en index2 in lijst lst """
  10. lst[index1] = lst[index2]
  11. def find_index_of_minimum(lst, start_index=0):
  12. """ Vindt de locatie van het minimum in lijst lst vanaf een gegeven start_index """
  13. minimum = lst[start_index]
  14. index_of_minimum = start_index
  15. # Doorloop de lijst lst vanaf start_index en
  16. # update minimum en index_of_minimum waar nodig.
  17. return index_of_minimum
  18. def selection_sort(lst):
  19. """ Sorteer lijst lst 'in place' door middel van het selection sort algoritme """
  20. # Implementeer selection sort met behulp van
  21. # swap() en find_index_of_minimum()
  22. pass
  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 random
  28. def test_swap():
  29. lst_test = [4, 9, 7]
  30. swap(lst_test, 0, 1)
  31. assert lst_test == [9, 4, 7], "Fout: swap([4, 9, 7], 0, 1) geeft {} in plaats van {}".format(lst_test, [9, 4, 7])
  32. lst_test = [4, 9, 7]
  33. swap(lst_test, 1, 2)
  34. assert lst_test == [4, 7, 9], "Fout: swap([4, 9, 7], 1, 2) geeft {} in plaats van {}".format(lst_test, [4, 7, 9])
  35. lst_test = [4, 9, 7]
  36. swap(lst_test, 0, 2)
  37. assert lst_test == [7, 9, 4], "Fout: swap([4, 9, 7], 0, 2) geeft {} in plaats van {}".format(lst_test, [7, 9, 4])
  38. def test_find_index_of_minimum():
  39. lst_test = [18, 6, 21, 44, 9, 14]
  40. assert find_index_of_minimum(lst_test, 0) == 1, "Fout: find_index_of_minimum({}, 0) geeft {} in plaats van 1".format(lst_test, find_index_of_minimum(lst_test, 0))
  41. assert find_index_of_minimum(lst_test, 2) == 4, "Fout: find_index_of_minimum({}, 2) geeft {} in plaats van 4".format(lst_test, find_index_of_minimum(lst_test, 2))
  42. assert find_index_of_minimum(lst_test, 3) == 4, "Fout: find_index_of_minimum({}, 3) geeft {} in plaats van 4".format(lst_test, find_index_of_minimum(lst_test, 3))
  43. def test_selection_sort():
  44. lst_test = random.sample(range(-99, 100), 6)
  45. lst_copy = lst_test.copy()
  46. selection_sort(lst_test)
  47. assert lst_test == sorted(lst_copy), "Fout: selection_sort({}) geeft {} in plaats van {}".format(lst_copy, lst_test, sorted(lst_copy))
  48. if __name__ == '__main__':
  49. try:
  50. print("\x1b[0;32m")
  51. test_swap()
  52. print("Je functie swap() werkt goed!")
  53. test_find_index_of_minimum()
  54. print("Je functie find_index_of_minimum() werkt goed!")
  55. test_selection_sort()
  56. print("Je selection sort algoritme werkt goed!\n\nKnap gedaan!\n")
  57. print("\x1b[0;30m")
  58. aantal = int(input("Hoeveel getallen zal ik sorteren? "))
  59. lst = list(range(aantal))
  60. random.shuffle(lst)
  61. print("De lijst: \n" + str(lst))
  62. selection_sort(lst)
  63. print("is na sortering: \n" + str(lst))
  64. except AssertionError as ae:
  65. print("\x1b[0;31m")
  66. print(str(ae))