#!/bin/ruby require 'time' # a student who submitted for the assignment class Student attr_reader :name, :section def initialize(name, section) @name = name @section = section end def to_s "#{@name}, sect #{@section}" end end # a particular submission for a student class Submission attr_reader :student, :assignment, :diffs, :time def initialize(stu, assmnt, diffs, time) @student = stu @assignment = assmnt @diffs = diffs @time = time end def to_s @student.to_s + ": #{@diffs} diffs, #{@time.ctime}; score #{score}" end def score if diffs > 0 then 0 else 5 end end end # all "final" submissions, organized by submissions # - this stores only the LAST submission class Submits def initialize @results = {} end # record a Submission, comparing this submission against previous ones # and keeping only the latest def add(sub) if @results[sub.student.name] == nil || @results[sub.student.name].time < sub.time then #puts "[Storing submission #{sub}]" @results[sub.student.name] = sub else #puts "[NOT adding submission #{sub}]" end end # look up student by name def lookup(name) r = @results[name] if r == nil then nil else r.student end end # load submissions from standard input def Submits::load subs = Submits.new while (line = gets) do items = line.chop.split(',') stu = subs.lookup(items[0]) if stu == nil then stu = Student.new(items[0], items[1]) end t = Time.parse(items[4]) subs.add(Submission.new(stu, items[2], items[3].to_i, t)) end return subs end # return array of final submissions, sorted by student name def final_results sorted = @results.sort # now need to strip off names from each entry sorted.map { |x| x[1] } end end if __FILE__ == $0 then all = Submits::load res = all.final_results res.each { |x| puts x } end