Решение на Осма задача от Методи Димитров

Обратно към всички решения

Към профила на Методи Димитров

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 12 успешни тест(а)
  • 28 неуспешни тест(а)

Код

class Spreadsheet
class Error < RuntimeError
end
def initialize(sheet = '')
@sheet = sheet.strip.split("\n").map(&:split)
end
def rows()
@sheet
end
def empty?()
@sheet.empty?
end
def cell_at(cell_index)
row, column = convert_coordinates(cell_index)
if row == -1 or column == -1
raise Error, "Invalid cell index \'#{cell_index}\'"
end
if row > @sheet.size or column > @sheet[0].size
raise Error, "Cell \'#{cell_index}\' does not exist"
end
@sheet[row][column]
end
def [](cell_index)
cell = cell_at(cell_index)
if cell[0] != "="
cell
else
end
end
def to_s()
@sheet.map { |row| row.join("\t") }.join "\n"
end
private
def convert_coordinates(cell_index)
row_index = /[0-9]+$/.match(cell_index).to_s.to_i - 1
column_chars = /^[A-Z]+/.match(cell_index).to_s.reverse.chars
column_index = column_chars.map.with_index do |char, index|
(26**index) * (("A".."Z").to_a.index(char).next)
end.inject(:+) - 1
[row_index, column_index]
end
def calculate_formula(formula)
number = /^[0-9]+(.[0-9]+)?$/.match(formula)
end
end

Лог от изпълнението

.........F.FF..FFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) Spreadsheet#to_s returns the evaluated spreadsheet as a table
     Failure/Error: expect(sheet.to_s).to eq \
       
       expected: "foo\t10\t2.1\t15\nbar\t11\t2.2\t5\nbaz\t12\t2.3\t27.60"
            got: "foo\t10\t2.1\t=ADD(B1,\tC1,\t2.9)\nbar\t11\t2.2\t=DIVIDE(B2,\tC2)\nbaz\t12\t2.3\t=MULTIPLY(C3,\tB3)"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -foo	10	2.1	15
       -bar	11	2.2	5
       -baz	12	2.3	27.60
       +foo	10	2.1	=ADD(B1,	C1,	2.9)
       +bar	11	2.2	=DIVIDE(B2,	C2)
       +baz	12	2.3	=MULTIPLY(C3,	B3)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:50:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Spreadsheet#cell_at returns the raw value of existing cells
     Failure/Error: expect(sheet.cell_at('B1')).to eq '=ADD(2, 2)'
       
       expected: "=ADD(2, 2)"
            got: "=ADD(2,"
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:69:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Spreadsheet#[] raises an exception for non-existant cells
     Failure/Error: expect { Spreadsheet.new()['A1'] }.to raise_error(Spreadsheet::Error, /Cell 'A1' does not exist/)
       expected Spreadsheet::Error with message matching /Cell 'A1' does not exist/, got #<NoMethodError: undefined method `size' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-1g75h4l/solution.rb:22:in `cell_at'
         # /tmp/d20160121-5693-1g75h4l/solution.rb:29:in `[]'
         # /tmp/d20160121-5693-1g75h4l/spec.rb:75:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-1g75h4l/spec.rb:75:in `block (3 levels) in <top (required)>'
         # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
         # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'
     # /tmp/d20160121-5693-1g75h4l/spec.rb:75:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Spreadsheet#[] returns the calculated value of formulae cells
     Failure/Error: expect(sheet['B1']).to eq 'ADD(2, 2)'
       
       expected: "ADD(2, 2)"
            got: "ADD(2,"
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:102:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Spreadsheet#[] adds two numbers with ADD
     Failure/Error: expect(sheet['A1']).to eq('4')
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:109:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Spreadsheet#[] adds five numbers with ADD
     Failure/Error: expect(sheet['A1']).to eq('15')
       
       expected: "15"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:115:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Spreadsheet#[] raises an exception for less than two arguments passed to ADD
     Failure/Error: expect { Spreadsheet.new('=ADD(1)')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Wrong number of arguments for 'ADD': expected at least 2, got 1/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Spreadsheet#[] adds numbers from cell references and as immediate arguments with ADD
     Failure/Error: expect(sheet['B1']).to eq('55')
       
       expected: "55"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:131:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Spreadsheet#[] adds numbers only from cell references with ADD
     Failure/Error: expect(sheet['D1']).to eq('10')
       
       expected: "10"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:137:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) Spreadsheet#[] multiplies numbers with MULTIPLY
     Failure/Error: expect(sheet1['A1']).to eq('120')
       
       expected: "120"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:144:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) Spreadsheet#[] raises an exception for less than two arguments to MULTIPLY
     Failure/Error: expect { Spreadsheet.new('=MULTIPLY(1)')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Wrong number of arguments for 'MULTIPLY': expected at least 2, got 1/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:149:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Spreadsheet#[] subtracts two numbers with SUBTRACT
     Failure/Error: expect(sheet['A1']).to eq('2')
       
       expected: "2"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:161:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Spreadsheet#[] subtracts numbers via cell references
     Failure/Error: expect(sheet['D1']).to eq('4')
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:167:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Spreadsheet#[] raises an exception when SUBTRACT is called with a wrong number of arguments
     Failure/Error: expect { Spreadsheet.new('=SUBTRACT(1)')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Wrong number of arguments for 'SUBTRACT': expected 2, got 1/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:171:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Spreadsheet#[] divides two numbers with DIVIDE
     Failure/Error: expect(sheet['A1']).to eq('42')
       
       expected: "42"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:183:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Spreadsheet#[] divides numbers via cell references
     Failure/Error: expect(sheet1['C1']).to eq('42')
       
       expected: "42"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:190:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Spreadsheet#[] raises an exception when DIVIDE is called with a wrong number of arguments
     Failure/Error: expect { Spreadsheet.new('=DIVIDE(1)')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Wrong number of arguments for 'DIVIDE': expected 2, got 1/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:195:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Spreadsheet#[] calculates the modulo of two numbers with MOD
     Failure/Error: expect(Spreadsheet.new('=MOD(42, 5)')['A1']).to eq('2')
       
       expected: "2"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:205:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) Spreadsheet#[] calculates the modulo of two numbers with MOD via cell references
     Failure/Error: expect(sheet1['C1']).to eq('4')
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:214:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) Spreadsheet#[] raises an exception when MOD is called with a wrong number of arguments
     Failure/Error: expect { Spreadsheet.new('=MOD(1)')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Wrong number of arguments for 'MOD': expected 2, got 1/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:219:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  21) Spreadsheet#[] adds floating point numbers with ADD
     Failure/Error: expect(Spreadsheet.new('10  =ADD(A1, 1.1)')['B1']).to eq '11.10'
       
       expected: "11.10"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:229:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  22) Spreadsheet#[] subtracts floating point numbers with SUBTRACT
     Failure/Error: expect(Spreadsheet.new('10  =SUBTRACT(A1, 1.1)')['B1']).to eq '8.90'
       
       expected: "8.90"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:234:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) Spreadsheet#[] multiplies floating point numbers with MULTIPLY
     Failure/Error: expect(Spreadsheet.new('10  =MULTIPLY(A1, 1.1)')['B1']).to eq '11'
       
       expected: "11"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:239:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  24) Spreadsheet#[] divides floating point numbers with DIVIDE
     Failure/Error: expect(Spreadsheet.new('10  =DIVIDE(A1, 4)')['B1']).to eq '2.50'
       
       expected: "2.50"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:244:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  25) Spreadsheet#[] evaluates deeply-nested cell references
     Failure/Error: expect(Spreadsheet.new('10  =ADD(5, A1)  3  =DIVIDE(B1, C1)  =MOD(D1, 4)')['E1']).to eq '1'
       
       expected: "1"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-1g75h4l/spec.rb:250:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  26) Spreadsheet#[] raises an exception for unknown functions
     Failure/Error: expect { Spreadsheet.new('=FOO(42)  100')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Unknown function 'FOO'/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:254:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  27) Spreadsheet#[] raises an exception for missing cells passed as function arguments
     Failure/Error: expect { Spreadsheet.new('=ADD(1, B4)  100')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Cell 'B4' does not exist/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:260:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  28) Spreadsheet#[] raises an exception for invalid expressions
     Failure/Error: expect { Spreadsheet.new('=FOO  100')['A1'] }.to raise_error(
       expected Spreadsheet::Error with message matching /Invalid expression 'FOO'/ but nothing was raised
     # /tmp/d20160121-5693-1g75h4l/spec.rb:266:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02592 seconds
40 examples, 28 failures

Failed examples:

rspec /tmp/d20160121-5693-1g75h4l/spec.rb:43 # Spreadsheet#to_s returns the evaluated spreadsheet as a table
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:62 # Spreadsheet#cell_at returns the raw value of existing cells
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:74 # Spreadsheet#[] raises an exception for non-existant cells
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:98 # Spreadsheet#[] returns the calculated value of formulae cells
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:106 # Spreadsheet#[] adds two numbers with ADD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:112 # Spreadsheet#[] adds five numbers with ADD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:118 # Spreadsheet#[] raises an exception for less than two arguments passed to ADD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:128 # Spreadsheet#[] adds numbers from cell references and as immediate arguments with ADD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:134 # Spreadsheet#[] adds numbers only from cell references with ADD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:140 # Spreadsheet#[] multiplies numbers with MULTIPLY
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:148 # Spreadsheet#[] raises an exception for less than two arguments to MULTIPLY
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:158 # Spreadsheet#[] subtracts two numbers with SUBTRACT
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:164 # Spreadsheet#[] subtracts numbers via cell references
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:170 # Spreadsheet#[] raises an exception when SUBTRACT is called with a wrong number of arguments
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:180 # Spreadsheet#[] divides two numbers with DIVIDE
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:186 # Spreadsheet#[] divides numbers via cell references
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:194 # Spreadsheet#[] raises an exception when DIVIDE is called with a wrong number of arguments
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:204 # Spreadsheet#[] calculates the modulo of two numbers with MOD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:210 # Spreadsheet#[] calculates the modulo of two numbers with MOD via cell references
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:218 # Spreadsheet#[] raises an exception when MOD is called with a wrong number of arguments
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:228 # Spreadsheet#[] adds floating point numbers with ADD
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:233 # Spreadsheet#[] subtracts floating point numbers with SUBTRACT
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:238 # Spreadsheet#[] multiplies floating point numbers with MULTIPLY
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:243 # Spreadsheet#[] divides floating point numbers with DIVIDE
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:249 # Spreadsheet#[] evaluates deeply-nested cell references
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:253 # Spreadsheet#[] raises an exception for unknown functions
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:259 # Spreadsheet#[] raises an exception for missing cells passed as function arguments
rspec /tmp/d20160121-5693-1g75h4l/spec.rb:265 # Spreadsheet#[] raises an exception for invalid expressions

История (2 версии и 0 коментара)

Методи обнови решението на 11.01.2016 15:49 (преди над 8 години)

+class Spreadsheet
+ class Error < RuntimeError
+ end
+
+ def initialize(sheet = '')
+ @sheet = sheet.strip.split("\n").map(&:split)
+ end
+
+ def rows()
+ @sheet
+ end
+
+ def empty?()
+ @sheet.empty?
+ end
+
+ def cell_at(cell_index)
+ row, column = convert_coordinates(cell_index)
+ if row == -1 or column == -1
+ raise Error, "Invalid cell index \'#{cell_index}\'"
+ end
+ if row > @sheet.size or column > @sheet[0].size
+ raise Error, "Cell \'#{cell_index}\' does not exist"
+ end
+ @sheet[row][column]
+ end
+
+ def [](cell_index)
+ cell = cell_at(cell_index)
+ if cell[0] != "="
+ cell
+ else
+
+ end
+ end
+
+ private
+
+ def convert_coordinates(cell_index)
+ row_index = /[0-9]+$/.match(cell_index).to_s.to_i - 1
+ column_chars = /^[A-Z]+/.match(cell_index).to_s.reverse.chars
+ column_index = column_chars.map.with_index do |char, index|
+ (26**index) * (("A".."Z").to_a.index(char).next)
+ end.inject(:+) - 1
+ [row_index, column_index]
+ end
+
+ def calculate_formula(formula)
+ number = /^[0-9]+(.[0-9]+)?$/.match(formula)
+ end
+end

Методи обнови решението на 11.01.2016 16:06 (преди над 8 години)

class Spreadsheet
class Error < RuntimeError
end
def initialize(sheet = '')
@sheet = sheet.strip.split("\n").map(&:split)
end
def rows()
@sheet
end
def empty?()
@sheet.empty?
end
def cell_at(cell_index)
row, column = convert_coordinates(cell_index)
if row == -1 or column == -1
raise Error, "Invalid cell index \'#{cell_index}\'"
end
if row > @sheet.size or column > @sheet[0].size
raise Error, "Cell \'#{cell_index}\' does not exist"
end
@sheet[row][column]
end
def [](cell_index)
cell = cell_at(cell_index)
if cell[0] != "="
cell
else
end
end
+ def to_s()
+ @sheet.map { |row| row.join("\t") }.join "\n"
+ end
+
private
def convert_coordinates(cell_index)
row_index = /[0-9]+$/.match(cell_index).to_s.to_i - 1
column_chars = /^[A-Z]+/.match(cell_index).to_s.reverse.chars
column_index = column_chars.map.with_index do |char, index|
(26**index) * (("A".."Z").to_a.index(char).next)
end.inject(:+) - 1
[row_index, column_index]
end
def calculate_formula(formula)
number = /^[0-9]+(.[0-9]+)?$/.match(formula)
end
end