Class: Clicoder::Judge

Inherits:
Object
  • Object
show all
Defined in:
lib/clicoder/judge.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Judge

Returns a new instance of Judge.



3
4
5
# File 'lib/clicoder/judge.rb', line 3

def initialize(options)
  @options = options
end

Instance Method Details

#diff_judge(file1, file2) ⇒ Object



15
16
17
# File 'lib/clicoder/judge.rb', line 15

def diff_judge(file1, file2)
  File.read(file1) == File.read(file2)
end

#float_judge(file1, file2, absolute_error) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/clicoder/judge.rb', line 19

def float_judge(file1, file2, absolute_error)
  lines1 = File.read(file1).split($/).map(&:strip)
  floats1 = lines1.map{ |line| line.split(/\s+/).map(&:to_f) }.flatten
  lines2 = File.read(file2).split($/).map(&:strip)
  floats2 = lines2.map{ |line| line.split(/\s+/).map(&:to_f) }.flatten
  floats1.zip(floats2).each do |float1, float2|
    return false if (float1 - float2).abs >= absolute_error
  end
  true
end

#judge(file1, file2) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/clicoder/judge.rb', line 7

def judge(file1, file2)
  if @options[:decimal]
    float_judge(file1, file2, 10**(- @options[:decimal]))
  else
    diff_judge(file1, file2)
  end
end