Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created February 7, 2012 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save jimweirich/1759774 to your computer and use it in GitHub Desktop.
Save jimweirich/1759774 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 4
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
#!/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