Created
February 7, 2012 13:51
-
-
Save jimweirich/1759774 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 4
This file contains hidden or 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
class Presentation | |
attr_reader :title, :presenter, :category | |
def initialize(title, presenter, category) | |
@title = title | |
@presenter = presenter | |
@category = category | |
@total = 0.0 | |
@count = 0 | |
end | |
def add_score(n) | |
@total += n | |
@count += 1 | |
end | |
def average_score | |
@total / @count | |
end | |
end |
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'presentation' | |
def read_presentations(ins) | |
result = {} | |
ins.each do |line| | |
id, title, presenter, category = line.strip.split(/:/) | |
p = Presentation.new(title, presenter, category) | |
result[id.to_i] = p | |
end | |
result | |
end | |
presentations = open("../presentations.txt") { |f| read_presentations(f) } | |
open("../votes.txt") do |votes| | |
votes.each do |line| | |
id, score = line.split | |
p = presentations[id.to_i] | |
if p | |
p.add_score(score.to_i) | |
end | |
end | |
end | |
presentations.values.sort_by { |p| p.average_score }.each do |p| | |
printf "%5.2f: %s\n", p.average_score, p.title | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment