Решение на Осма задача от Петър Иванов

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

Към профила на Петър Иванов

Резултати

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

Код

module Helpers
def calculate_cell(cell)
if cell =~ /=([-+]?[0-9])/ then return calculate_number(cell) end
if cell =~ /=([A-Z])([0-9])/ then calculate_other_cell(cell) end
end
def calculate_number(cell)
cell[1..cell.size]
end
def calculate_other_cell(cell)
row, column = get_row_col_index(cell[1..cell.size])
validate_cell_index(row, column, @sheet[row - 1][column - 1])
calculate_cell(@sheet[row - 1][column - 1])
end
def validate_cell_index(row, column, cell_index)
raise Spreadsheet::Error.new("Invalid cell index '#{cell_index}'") if
row == 0 or column == 0
raise Spreadsheet::Error.new("Cell '#{cell_index}' does not exist") if
row > @sheet.size or column > @sheet.first.size
end
def format_string(string)
sheet = []
string.split(/\n/).each do |v|
sheet << v.split(/\t| {2,}/).reject { |s| s.empty? }
end
sheet.reject { |s| s.empty? }
end
def get_row_col_index(cell_index)
row = cell_index.split(/[A-Z]/).reject { |s| s.empty? }
column = cell_index.split(/[0-9]/).reject { |s| s.empty? }
[row.first.to_i, get_column_index(column.first)]
end
def get_column_index(letters = "")
column = 0
letters.split("").each do |i|
if letters.split("").size % 2 == 0 then column *= 26 end
column += i.ord - "A".ord + 1
end
column
end
end
class Spreadsheet
include Helpers
def initialize(string = "")
@sheet = format_string(string)
end
def empty?
@sheet == [] ? true : false
end
def to_s
result = []
@sheet.each do |v|
result << v.join("\t")
end
result.join("\n")
end
def cell_at(cell_index)
row, column = get_row_col_index(cell_index)
validate_cell_index(row, column, cell_index)
@sheet[row - 1][column - 1]
end
def [](cell_index)
row, column = get_row_col_index(cell_index)
validate_cell_index(row, column, cell_index)
calculate_formula(@sheet[row - 1][column - 1])
@sheet[row - 1][column - 1][0] == '=' ? calculate_cell(
@sheet[row - 1][column - 1]) : @sheet[row - 1][column - 1]
end
end
class Spreadsheet::Error < StandardError
end

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

.........F...FFFFFFFFFFFFFFFFFFFFFFFFFFF

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, C1, 2.9)\nbar\t11\t2.2\t=DIVIDE(B2, C2)\nbaz\t12\t2.3\t=MULTIPLY(C3, B3)"
       
       (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-ew2lg9/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#[] returns the value of existing cells for simple cell indexes
     Failure/Error: expect(sheet['A1']).to eq 'foo'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a001fcd8>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/spec.rb:84: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#[] returns the value of existing cells for complex cell indexes
     Failure/Error: expect(sheet['AD1']).to eq 'b'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a000be90>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/spec.rb:93: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['A1']).to eq 'foo'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f639ffe0150>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/spec.rb:101: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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f639ffd5480 @sheet=[["=ADD(2, 2)"]]>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e20170 @sheet=[["=ADD(1, 2, 3, 4, 5)"]]>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e111e8 @sheet=[["=ADD(1)"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:119:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0eb6850>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0eb4230>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0ead660>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0ea6770 @sheet=[["=MULTIPLY(1)"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:149:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e932b0>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e90768>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e898f0 @sheet=[["=SUBTRACT(1)"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:171:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e85250 @sheet=[["=DIVIDE(84, 2)", "10"]]>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e827d0>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e7fb70 @sheet=[["=DIVIDE(1)"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:195:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e74ea0 @sheet=[["=MOD(42, 5)"]]>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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')
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e71b88>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e6f068 @sheet=[["=MOD(1)"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:219:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e649b0 @sheet=[["10", "=ADD(A1, 1.1)"]]>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e61648>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e5eee8>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e5c968 @sheet=[["10", "=DIVIDE(A1, 4)"]]>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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'
     NoMethodError:
       undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e5a500>
     # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
     # /tmp/d20160121-5693-ew2lg9/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'/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e59650 @sheet=[["=FOO(42)", "100"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:254:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e4cef0 @sheet=[["=ADD(1, B4)", "100"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:260:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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'/, got #<NoMethodError: undefined method `calculate_formula' for #<Spreadsheet:0x007f63a0e41c58 @sheet=[["=FOO", "100"]]>> with backtrace:
         # /tmp/d20160121-5693-ew2lg9/solution.rb:79:in `[]'
         # /tmp/d20160121-5693-ew2lg9/spec.rb:266:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-ew2lg9/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)>'
     # /tmp/d20160121-5693-ew2lg9/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.03056 seconds
40 examples, 28 failures

Failed examples:

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

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

Петър обнови решението на 11.01.2016 17:05 (преди около 9 години)

+module Helpers
+ def calculate_cell(cell)
+ if cell =~ /=([-+]?[0-9])/ then return calculate_number(cell) end
+ if cell =~ /=([A-Z])([0-9])/ then calculate_other_cell(cell) end
+ end
+
+ def calculate_number(cell)
+ cell[1..cell.size]
+ end
+
+ def calculate_other_cell(cell)
+ row, column = get_row_col_index(cell[1..cell.size])
+ validate_cell_index(row, column, @sheet[row - 1][column - 1])
+ calculate_cell(@sheet[row - 1][column - 1])
+ end
+
+ def validate_cell_index(row, column, cell_index)
+ raise Spreadsheet::Error.new("Invalid cell index '#{cell_index}'") if
+ row == 0 or column == 0
+
+ raise Spreadsheet::Error.new("Cell '#{cell_index}' does not exist") if
+ row > @sheet.size or column > @sheet.first.size
+ end
+
+ def format_string(string)
+ sheet = []
+ string.split(/\n/).each do |v|
+ sheet << v.split(/\t| {2,}/).reject { |s| s.empty? }
+ end
+ sheet.reject { |s| s.empty? }
+ end
+
+ def get_row_col_index(cell_index)
+ row = cell_index.split(/[A-Z]/).reject { |s| s.empty? }
+ column = cell_index.split(/[0-9]/).reject { |s| s.empty? }
+
+ [row.first.to_i, get_column_index(column.first)]
+ end
+
+ def get_column_index(letters = "")
+ column = 0
+ letters.split("").each do |i|
+ if letters.split("").size % 2 == 0 then column *= 26 end
+ column += i.ord - "A".ord + 1
+ end
+ column
+ end
+end
+
+class Spreadsheet
+ include Helpers
+
+ def initialize(string = "")
+ @sheet = format_string(string)
+ end
+
+ def empty?
+ @sheet == [] ? true : false
+ end
+
+ def to_s
+ result = []
+ @sheet.each do |v|
+ result << v.join("\t")
+ end
+ result.join("\n")
+ end
+
+ def cell_at(cell_index)
+ row, column = get_row_col_index(cell_index)
+ validate_cell_index(row, column, cell_index)
+ @sheet[row - 1][column - 1]
+ end
+
+ def [](cell_index)
+ row, column = get_row_col_index(cell_index)
+ validate_cell_index(row, column, cell_index)
+
+ calculate_formula(@sheet[row - 1][column - 1])
+
+ @sheet[row - 1][column - 1][0] == '=' ? calculate_cell(
+ @sheet[row - 1][column - 1]) : @sheet[row - 1][column - 1]
+ end
+end
+
+class Spreadsheet::Error < StandardError
+end