Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. """Module for detecting voter fraud using binary search."""
  2. from classes import VoterList
  3. from tools import make_voter_list
  4.  
  5.  
  6. def fraud_detect_binary(first_booth_voters, second_booth_voters):
  7. """This function takes VoterLists from two voting booths as input.
  8. You can assume that the second VoterList is in lexicogrpahic order, that is,
  9. the second list is already sorted alphabetically.
  10.  
  11. The function returns a VoterList and an integer.
  12. The VoterList contains the voters who cast a vote in both booths (ie, frauds),
  13. The integer is the number of VoterName comparisons the function made.
  14. """
  15. results = []
  16. total_comparisons = 0
  17.  
  18. #supplementary video
  19. for Voter in first_booth_voters:
  20. first = 0
  21. last = len(second_booth_voters) - 1
  22. fraud = False
  23. index = (first + last) // 2
  24.  
  25. if len(results) == len(first_booth_voters):
  26. total_comparisons += 1
  27. break
  28. else:
  29. while first < last and not fraud:
  30. total_comparisons += 2
  31. if Voter < first_booth_voters[index]:
  32. last = index - 1
  33. else:
  34. first = index + 1
  35.  
  36. if Voter == first_booth_voters[index]:
  37. results.append(Voter)
  38. fraud = True
  39.  
  40. return results, comparisons
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement