#!/usr/bin/ruby # Facebook's 'Gattaca' puzzle. # tom.luo@mgail.com class Prediction attr_accessor :start, :stop, :score, :highest_score def initialize(start,stop,score) @start=start @stop=stop @score=score @highest_score=0 end def SetHighestScore(score) @highest_score=score end end def HighestScore(list) last=list.size-1 global_max = list[last].highest_score temp_list=[] last.downto 0 do |i| max = 0 i_seq = list[i] # temp_list.uniq! temp_list.each do |x| # k+=1 # puts k if k>1 # puts i_seq.stop.to_s + " " + x.start.to_s if i_seq.stop < x.start and max < x.highest_score then max = x.highest_score break end end i_seq.SetHighestScore(i_seq.score + max) if temp_list.empty? temp_list[0]=i_seq else if i_seq.highest_score >= temp_list[0].highest_score then temp_list = [i_seq].concat(temp_list) else temp_list.push(i_seq) end end global_max = i_seq.highest_score if i_seq.highest_score > global_max end global_max end # Main file_name=ARGV[0] file=File.open(file_name,"r") lines = file.readlines # ---------------------------------------------------- # collect the genes # ---------------------------------------------------- predictions = [] lines.each do |line| fields=line.split() if fields.size==3 start=fields[0].to_i stop=fields[1].to_i score=fields[2].to_i predictions << Prediction.new(start,stop,score) # predictions << Prediction.new(fields[0],fields[1],fields[2]) end end # puts predictions.size # predictions.sort! {|x,y| x.start <=> y.start} predictions = predictions.sort_by { |a| a.start } # predictions.each {|p| puts "#{p.start} #{p.stop} #{p.score}"} puts "#{HighestScore(predictions)}\n" # predictions.each {|p| puts "#{p.start} #{p.stop} #{p.score} #{p.highest_score}"} # -----------------------------------------