module AtCoderFriends::Parser::InputFormatUtils
utilities for input format parser
Public Instance Methods
extract_delim(str)
click to toggle source
# File lib/at_coder_friends/parser/input_format.rb, line 82 def extract_delim(str) # a-b, a/b, a:b -> a b str = str.dup dlms = DELIMS.select { |c| str.gsub!(/#{c}(#{RE_SINGLE})/, ' \1') }.join [str, dlms] end
normalize_fmt(str)
click to toggle source
1) &npsp;, fill-width space -> half width space 2) {i, j}->{i,j} for nested {}
# File lib/at_coder_friends/parser/input_format.rb, line 46 def normalize_fmt(str) str .tr('0-9A-Za-z', '0-9A-Za-z') .gsub(/[[:space:]]/) { |c| c.gsub(/[^\n]/, ' ') } # 1) .gsub(%r{<var>([^<>]+)</var>}i, '\1') # <sub><var>N</var></sub> .gsub(%r{<sup>([^<>]+)</sup>}i, '^\1') .gsub(%r{<sub>([^<>]+)</sub>}i, '_{\1}') .gsub(%r{<sub>([^<>]+)</sub>}i, '_{\1}') # for nested<sub> .gsub(/<("[^"]*"|'[^']*'|[^'"<>])*>/, '') .gsub('&', '&') .gsub('>', '>') .gsub('<', '<') .gsub('\\ ', ' ') .gsub(/\\hspace\{\d+pt\}/, ' ') .gsub('\\(', '') .gsub('\\)', '') .gsub('\\lvert', '|') .gsub('\\rvert', '|') .gsub('\\mathit', '') .gsub('\\mathrm', '') .gsub('\\times', '*') .gsub(/\\begin(\{[^{}]*\})*/, '') .gsub(/\\end(\{[^{}]*\})*/, '') .gsub(/\\[cdlv]?dots/, '..') .gsub(/\{\}/, ' ') .gsub('−', '-') # full width hyphen .gsub(/[・.:‥⋮︙…]+/, '..') .gsub(/[\\$']/, '') # s' -> s .gsub(/[&~|]/, ' ') # |S| -> S .gsub(/^\s*[.:][\s.:]*$/, '..') .tr('()', '{}') .gsub(/#{RE_BLOCK}/) { |w| w.delete(' ') } # 2) .split("\n") .map(&:strip) end
normalize_name(s)
click to toggle source
# File lib/at_coder_friends/parser/input_format.rb, line 90 def normalize_name(s) s.delete('{},').gsub(/(\A_+|_+\z)/, '') end
normalize_names(names)
click to toggle source
# File lib/at_coder_friends/parser/input_format.rb, line 94 def normalize_names(names) names.map { |nm| normalize_name(nm) } end
normalize_size(container, size, ix0)
click to toggle source
# File lib/at_coder_friends/parser/input_format.rb, line 98 def normalize_size(container, size, ix0) sz = size_array(container, size) sz0 = size_array(container, ix0) sz.map.with_index do |s, i| if sz0[i] == '0' # 0 -> 1, N-1 -> N, N-2 -> N-1 if 0 origin s.gsub(/\A0\z/, '1').gsub(/-1\z/, '').gsub(/-2\z/, '-1') else s end end end
size_array(container, size)
click to toggle source
split size by container dimension
# File lib/at_coder_friends/parser/input_format.rb, line 113 def size_array(container, size) ( case DIMENSION_TBL[container] when 2 split_size(size) when 1 [size] when 0 [] end ).map { |s| normalize_name(s) } end
split_size(str)
click to toggle source
# File lib/at_coder_friends/parser/input_format.rb, line 126 def split_size(str) str = str.gsub(/(\A\{|\}\z)/, '') while str =~ /\A#{RE_BLOCK}\z/ sz = str.split(',') return sz if sz.size == 2 sz = str.scan(/(?<nbl>[^{}]+)|#{RE_BLOCK}/).flatten.compact return sz if sz.size == 2 str = str.delete('{},') sz = str.scan(/[^_](?:_[^_])?/) return sz if sz.size == 2 sz = str.split('_') return sz if sz.size == 2 [str[0] || '_', str[1..-1] || '_'] end