Class: AtCoderFriends::Problem::InputFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/at_coder_friends/problem.rb

Overview

holds information about input format

Constant Summary collapse

ITEM_RANK =
{ number: 1, decimal: 2, string: 3 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container: nil, item: nil, names: [], size: [], delim: '', cols: []) ⇒ InputFormat

Returns a new instance of InputFormat.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/at_coder_friends/problem.rb', line 28

def initialize(
  container: nil,
  item: nil,
  names: [],
  size: [],
  delim: '',
  cols: []
)
  @container = container
  @item = item
  @names = names
  @size = size
  @delim = delim
  @cols = cols
end

Instance Attribute Details

#colsObject

Returns the value of attribute cols.



26
27
28
# File 'lib/at_coder_friends/problem.rb', line 26

def cols
  @cols
end

#containerObject (readonly)

Returns the value of attribute container.



25
26
27
# File 'lib/at_coder_friends/problem.rb', line 25

def container
  @container
end

#delimObject (readonly)

Returns the value of attribute delim.



25
26
27
# File 'lib/at_coder_friends/problem.rb', line 25

def delim
  @delim
end

#namesObject (readonly)

Returns the value of attribute names.



25
26
27
# File 'lib/at_coder_friends/problem.rb', line 25

def names
  @names
end

#sizeObject (readonly)

Returns the value of attribute size.



25
26
27
# File 'lib/at_coder_friends/problem.rb', line 25

def size
  @size
end

Instance Method Details

#componentsObject



61
62
63
64
65
66
67
68
69
# File 'lib/at_coder_friends/problem.rb', line 61

def components
  @components ||=
    case container
    when :varray_matrix
      varray_matrix_components
    when :matrix_varray
      matrix_varray_components
    end
end

#itemObject



52
53
54
# File 'lib/at_coder_friends/problem.rb', line 52

def item
  @item || cols.max_by { |k| ITEM_RANK[k] } || :number
end

#matrix_varray_componentsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/at_coder_friends/problem.rb', line 86

def matrix_varray_components
  [
    self.class.new(
      container: :matrix, item: @item,
      names: names[0..0], size: size,
      delim: delim, cols: cols[0..0]
    ),
    self.class.new(
      container: :varray,
      names: names[1..], size: size[0..0],
      delim: delim, cols: cols[1..] || []
    )
  ]
end

#to_sObject



44
45
46
47
48
49
50
# File 'lib/at_coder_friends/problem.rb', line 44

def to_s
  if container == :unknown
    "#{container} #{item}"
  else
    "#{container} #{item}(#{cols}) #{names} #{size} #{delim}"
  end
end

#varray_matrix_componentsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/at_coder_friends/problem.rb', line 71

def varray_matrix_components
  [
    self.class.new(
      container: :varray,
      names: names[0..-2], size: size[0..0],
      delim: delim, cols: cols[0..-2]
    ),
    self.class.new(
      container: :matrix, item: @item,
      names: names[-1..], size: size,
      delim: delim, cols: cols[-1..] || []
    )
  ]
end

#varsObject



56
57
58
59
# File 'lib/at_coder_friends/problem.rb', line 56

def vars
  tmp = (@item && [@item]) || cols
  names.zip(tmp).map { |(name, col)| [name, col || :number] }
end