-
-
Save sporsh/1759793 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "presentation" | |
p_file = ARGV[0] || "presentations.txt" | |
v_file = ARGV[1] || "votes.txt" | |
def load_presentations(filename) | |
result = {} | |
open(filename) { |file| | |
while entry = file.gets | |
m_obj = /^(\d+):([^:]+):([^:]+)/.match(entry) | |
_, id, title, presenter = *m_obj | |
id = id.to_i | |
result[id] = Presentation.new(title,presenter) | |
end | |
} | |
result | |
end | |
def load_votes(filename, presentations) | |
open(filename) { |file| | |
while entry = file.gets | |
m_obj = /^(\d+)\s+(\d+)$/.match(entry) | |
_, id, score = *m_obj | |
id = id.to_i | |
score = score.to_i | |
presentations[id].add_score(score) | |
end | |
} | |
end | |
presentations = load_presentations(p_file) | |
load_votes(v_file, presentations) | |
def print_presentation_scores(heading, presentations) | |
puts '='*80 | |
puts heading | |
puts '-'*80 | |
sorted_p = presentations.sort_by { |p| p.average_score } | |
sorted_p.each { |p| printf("%.2f: %s\n", p.average_score, p.title) } | |
puts '' | |
end | |
highscore = presentations.values.select { |p| p.average_score >= 4 } | |
print_presentation_scores("Average score of 4 or more:", highscore) | |
lowscore = presentations.values.select { |p| p.average_score <= 2 } | |
print_presentation_scores("Average score of 2 or less:", lowscore) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment