Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. from word import Word
  2.  
  3. def insert_words(file_variable, hash_set):
  4. """
  5. -------------------------------------------------------
  6. Retrieves every Word in file_variable and inserts into
  7. a HashSet.
  8. -------------------------------------------------------
  9. Preconditions:
  10. file_variable - the already open file containing data to evaluate (file)
  11. hash_set - the HashSet to insert the words into (HashSet)
  12. Postconditions:
  13. Each Word object in hash_set contains the number of comparisons
  14. required to insert that Word object from file_variable into hash_set.
  15. -------------------------------------------------------
  16. """
  17.  
  18. words = file_variable.read().lower().replace('\n', ' ').split(" ")
  19. for i in words:
  20. if i.isalpha():
  21. hash_set.insert(Word(i))
  22.  
  23. def comparison_total(hash_set):
  24. """
  25. -------------------------------------------------------
  26. Sums the comparison values of all Word objects in hash_set.
  27. -------------------------------------------------------
  28. Preconditions:
  29. hash_set - a hash set of Word objects (HashSet)
  30. Postconditions:
  31. returns
  32. total - the total of all comparison fields in the HashSet
  33. Word objects (int)
  34. max_word - the word having the most comparisons (Word)
  35. -------------------------------------------------------
  36. """
  37.  
  38. total = 0
  39. max_word = None
  40.  
  41. for i in hash_set:
  42. total += i.comparisons
  43. if not max_word or i.comparisons > max_word.comparisons:
  44. max_word = i
  45.  
  46. return total, max_word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement