Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. puts "\n"
  2. puts "Welcome to my election program. What a way to make a living!"
  3. puts "\n"
  4.  
  5. # Specification (optional enhancement): handle write-in votes
  6.  
  7. puts "The official candidates are Dolly, Jane, and Lily. You may also enter a write-in candidate of your choice."
  8. puts "\n"
  9. puts "Please enter one candidate name per line and press return."
  10. puts "\n"
  11.  
  12. votes = Array.new
  13. i = 1
  14. while i <= 10
  15. print "Vote ##{i}: "
  16. votes << gets.chomp.upcase.strip
  17. i += 1
  18. end
  19. puts "\n"
  20.  
  21. puts "Counting votes, beep boo beep beep ..."
  22. puts "\n"
  23. puts "The votes are counted! The results are"
  24. puts "\n"
  25.  
  26. vote_counts_h = Hash.new(0)
  27. votes.each do |candidate| vote_counts_h [candidate] += 1
  28. end
  29.  
  30. vote_counts_a = vote_counts_h.sort_by { | v, k | k }
  31. vote_counts_a.reverse!
  32.  
  33. # Specification (optional enhancement): Handle grammar of vote summary saying vote or votes appropriately
  34. # Programmer note: Adding block to provide different output depending on
  35. # whether candidate receive one vote or more than one vote.
  36.  
  37. vote_counts_a.each do |candidate, votes_received|
  38. if votes_received == 1
  39. puts candidate.capitalize + ": " + votes_received.to_s + " vote"
  40. else
  41. puts candidate.capitalize + ": " + votes_received.to_s + " votes"
  42. end
  43. end
  44.  
  45. # Adding nifty code uncovered for Walk-A-Thon program that will create two variables
  46. # from my vote_counts hash: one with the highest value (max_votes) and one with the key or keys
  47. # associated with the highest value (max_candidate).
  48. # The code below is much shorter than my initial code for this project, plus it makes handling ties easy.
  49.  
  50. max_votes = vote_counts_h.values.max
  51. max_candidate = vote_counts_h.select { |k, v| v == max_votes}.keys
  52.  
  53. # Creating another variable to store the number of winners (winner_count):
  54. # 1 for a single winner, or 2 or more in case of a tie.
  55. # A variable isn't required here -- I could do it with some nested code
  56. # -- but creating a variable makes the program easier to understand.
  57.  
  58. winner_count = max_candidate.count
  59. puts "\n"
  60.  
  61. # Specification (optional enhancement): Handle ties for a winner appropriately
  62. # Adding code to provide different outputs for a single winner and a tie.
  63. # (Easy since I already did this in the Walk-A-Thon tracker program.)
  64.  
  65. if winner_count == 1
  66. puts "And the winner is ... #{max_candidate[0].capitalize} with #{max_votes} votes!"
  67. else
  68. puts "And the winner is ... it's a tie! #{max_candidate[0..-2].join(', ').capitalize} and #{max_candidate[-1].capitalize} received #{max_votes} votes each."
  69. end
  70.  
  71. puts "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement