Решение на Осма задача от София Петрова

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

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

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 8 успешни тест(а)
  • 32 неуспешни тест(а)

Код

class String
def numeric?
Float(self) != nil rescue false
end
end
class Spreadsheet
def initialize(data = nil)
@data = data.strip
@rows = @data.split("\n")
@cells = @rows.map do |char|
if char.include?(" ")
char.split(" ") - [""]
else
char.split("\t")
end
end
end
def to_s
@data.to_s
end
def empty?
@data.empty?
end
def cell_at(cell_index)
/\b[A-Z][A-Z0-9]+\b/.match(cell_index) or
raise Spreadsheet::Error, "Invalid cell index #{cell_index}"
cell, row = [], ""
cell_index.split("").each { |e| e.numeric? ? row << e : cell << e }
cell, row = cell_processing(cell), row.to_i
@cells[row - 1][cell - 1]
end
def [](cell_index)
cell_value = cell_at(cell_index)
if cell_value.start_with?("=")
cell_value.slice!("=")
cell_value.numeric? ? cell_value : cell_at(cell_value)
#/\b[A-Z][A-Z0-9]+\b/.match(cell_value) for cell_at(cell_value)
end
cell_value
end
class Error < StandardError
# attr_reader :object
# def initialize(object)
# @object = object
# end
end
private
def cell_processing(cell)
hash_from_cell = Hash[(1...cell.size + 1).to_a.reverse.zip cell]
evaluated_cell = hash_from_cell.map do |key,value|
key != 1 ? (value.ord - 64) * 26 : value.ord - 64
end
cell = evaluated_cell.reduce(:+)
rescue Spreadsheet::Error => e
puts "Cell #{cell} does not exist"
end
# def evaluate(expression)
# case expression
# when "ADD"
# expression_processing(expression, "ADD", :+)
# when "MULTIPLY"
# expression_processing(expression, "MULTIPLY", :*)
# when "SUBTRACT"
# expression_processing(expression, "SUBTRACT", :-)
# when "DIVIDE"
# expression_processing(expression, "DIVIDE", :/)
# when "MOD"
# expression_processing(expression, "MOD", :%)
# end
# end
# def expression_processing(expression, function, symbol)
# expression.slice!(function).slice!("(").slice!(")")
# expression = expression.split(",")
# recursion_cells = expression.find_all do |char|
# /\b[A-Z][A-Z0-9]+\b/.match(char)
# end
# recursion_cells = recursion_cells.map {|cell| [cell]}
# expression << recursion_cells
# expression.inject {|sum, n| sum symbol n}
# end
end

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

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

Failures:

  1) Spreadsheet#new can be called with no arguments or with a single string argument
     Failure/Error: Spreadsheet.new
     NoMethodError:
       undefined method `strip' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:9:in `initialize'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:4:in `new'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:4: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#new creates a blank sheet when no arguments are passed
     Failure/Error: expect(Spreadsheet.new).to be_empty
     NoMethodError:
       undefined method `strip' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:9:in `initialize'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:10:in `new'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:10: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#to_s returns blank tables as blank strings
     Failure/Error: expect(Spreadsheet.new.to_s).to eq ''
     NoMethodError:
       undefined method `strip' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:9:in `initialize'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:24:in `new'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:24: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#to_s splits cells by two or more spaces
     Failure/Error: expect(Spreadsheet.new("foo  bar   42\nbaz    larodi  100").to_s).to eq "foo\tbar\t42\nbaz\tlarodi\t100"
       
       expected: "foo\tbar\t42\nbaz\tlarodi\t100"
            got: "foo  bar   42\nbaz    larodi  100"
       
       (compared using ==)
       
       Diff:
       @@ -1,3 +1,3 @@
       -foo	bar	42
       -baz	larodi	100
       +foo  bar   42
       +baz    larodi  100
     # /tmp/d20160121-5693-1uvzthx/spec.rb:40: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#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   10  2.1   =ADD(B1, C1, 2.9)\n        bar   11  2.2   =DIVIDE(B2, C2)\n        baz   12  2.3   =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-1uvzthx/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)>'

  6) Spreadsheet#cell_at raises and exception for non-existant cells
     Failure/Error: expect { Spreadsheet.new('foo')['B10'] }.to raise_error(Spreadsheet::Error, /Cell 'B10' does not exist/)
       expected Spreadsheet::Error with message matching /Cell 'B10' does not exist/, got #<NoMethodError: undefined method `[]' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
         # /tmp/d20160121-5693-1uvzthx/solution.rb:40:in `[]'
         # /tmp/d20160121-5693-1uvzthx/spec.rb:59:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-1uvzthx/spec.rb:59: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-1uvzthx/spec.rb:59: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 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 `strip' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-1uvzthx/solution.rb:9:in `initialize'
         # /tmp/d20160121-5693-1uvzthx/spec.rb:75:in `new'
         # /tmp/d20160121-5693-1uvzthx/spec.rb:75:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-1uvzthx/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-1uvzthx/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)>'

  8) Spreadsheet#[] returns the calculated value of formulae cells
     Failure/Error: expect(sheet['C1']).to eq '4'
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/spec.rb:103: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 two numbers with ADD
     Failure/Error: expect(sheet['A1']).to eq('4')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  10) Spreadsheet#[] adds five numbers with ADD
     Failure/Error: expect(sheet['A1']).to eq('15')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  11) 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-1uvzthx/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)>'

  12) Spreadsheet#[] adds numbers from cell references and as immediate arguments with ADD
     Failure/Error: expect(sheet['B1']).to eq('55')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  13) Spreadsheet#[] adds numbers only from cell references with ADD
     Failure/Error: expect(sheet['D1']).to eq('10')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  14) Spreadsheet#[] multiplies numbers with MULTIPLY
     Failure/Error: expect(sheet1['A1']).to eq('120')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  15) 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-1uvzthx/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)>'

  16) Spreadsheet#[] subtracts two numbers with SUBTRACT
     Failure/Error: expect(sheet['A1']).to eq('2')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  17) Spreadsheet#[] subtracts numbers via cell references
     Failure/Error: expect(sheet['D1']).to eq('4')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  18) 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-1uvzthx/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)>'

  19) Spreadsheet#[] divides two numbers with DIVIDE
     Failure/Error: expect(sheet['A1']).to eq('42')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  20) Spreadsheet#[] divides numbers via cell references
     Failure/Error: expect(sheet1['C1']).to eq('42')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  21) 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-1uvzthx/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)>'

  22) Spreadsheet#[] calculates the modulo of two numbers with MOD
     Failure/Error: expect(Spreadsheet.new('=MOD(42, 5)')['A1']).to eq('2')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  23) Spreadsheet#[] calculates the modulo of two numbers with MOD via cell references
     Failure/Error: expect(sheet1['C1']).to eq('4')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  24) 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-1uvzthx/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)>'

  25) 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 `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  26) 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 `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  27) Spreadsheet#[] multiplies floating point numbers with MULTIPLY
     Failure/Error: expect(Spreadsheet.new('10  =MULTIPLY(A1, 1.1)')['B1']).to eq '11'
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  28) Spreadsheet#[] divides floating point numbers with DIVIDE
     Failure/Error: expect(Spreadsheet.new('10  =DIVIDE(A1, 4)')['B1']).to eq '2.50'
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  29) 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 `[]' for nil:NilClass
     # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
     # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
     # /tmp/d20160121-5693-1uvzthx/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)>'

  30) 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 `[]' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
         # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
         # /tmp/d20160121-5693-1uvzthx/spec.rb:254:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-1uvzthx/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-1uvzthx/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)>'

  31) 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 `[]' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-1uvzthx/solution.rb:35:in `cell_at'
         # /tmp/d20160121-5693-1uvzthx/solution.rb:43:in `[]'
         # /tmp/d20160121-5693-1uvzthx/spec.rb:260:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-1uvzthx/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-1uvzthx/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)>'

  32) 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-1uvzthx/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.04088 seconds
40 examples, 32 failures

Failed examples:

rspec /tmp/d20160121-5693-1uvzthx/spec.rb:3 # Spreadsheet#new can be called with no arguments or with a single string argument
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:9 # Spreadsheet#new creates a blank sheet when no arguments are passed
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:23 # Spreadsheet#to_s returns blank tables as blank strings
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:39 # Spreadsheet#to_s splits cells by two or more spaces
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:43 # Spreadsheet#to_s returns the evaluated spreadsheet as a table
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:58 # Spreadsheet#cell_at raises and exception for non-existant cells
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:74 # Spreadsheet#[] raises an exception for non-existant cells
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:98 # Spreadsheet#[] returns the calculated value of formulae cells
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:106 # Spreadsheet#[] adds two numbers with ADD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:112 # Spreadsheet#[] adds five numbers with ADD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:118 # Spreadsheet#[] raises an exception for less than two arguments passed to ADD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:128 # Spreadsheet#[] adds numbers from cell references and as immediate arguments with ADD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:134 # Spreadsheet#[] adds numbers only from cell references with ADD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:140 # Spreadsheet#[] multiplies numbers with MULTIPLY
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:148 # Spreadsheet#[] raises an exception for less than two arguments to MULTIPLY
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:158 # Spreadsheet#[] subtracts two numbers with SUBTRACT
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:164 # Spreadsheet#[] subtracts numbers via cell references
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:170 # Spreadsheet#[] raises an exception when SUBTRACT is called with a wrong number of arguments
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:180 # Spreadsheet#[] divides two numbers with DIVIDE
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:186 # Spreadsheet#[] divides numbers via cell references
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:194 # Spreadsheet#[] raises an exception when DIVIDE is called with a wrong number of arguments
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:204 # Spreadsheet#[] calculates the modulo of two numbers with MOD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:210 # Spreadsheet#[] calculates the modulo of two numbers with MOD via cell references
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:218 # Spreadsheet#[] raises an exception when MOD is called with a wrong number of arguments
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:228 # Spreadsheet#[] adds floating point numbers with ADD
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:233 # Spreadsheet#[] subtracts floating point numbers with SUBTRACT
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:238 # Spreadsheet#[] multiplies floating point numbers with MULTIPLY
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:243 # Spreadsheet#[] divides floating point numbers with DIVIDE
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:249 # Spreadsheet#[] evaluates deeply-nested cell references
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:253 # Spreadsheet#[] raises an exception for unknown functions
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:259 # Spreadsheet#[] raises an exception for missing cells passed as function arguments
rspec /tmp/d20160121-5693-1uvzthx/spec.rb:265 # Spreadsheet#[] raises an exception for invalid expressions

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

София обнови решението на 11.01.2016 05:49 (преди около 9 години)

+class String
+ def numeric?
+ Float(self) != nil rescue false
+ end
+end
+
+class Spreadsheet
+
+ def initialize(data = nil)
+ @data = data.strip
+ @rows = @data.split("\n")
+ @cells = @rows.map do |char|
+ if char.include?(" ")
+ char.split(" ") - [""]
+ else
+ char.split("\t")
+ end
+ end
+ end
+
+ def to_s
+ @data.to_s
+ end
+
+ def empty?
+ @data.empty?
+ end
+
+ def cell_at(cell_index)
+ cell, row = [], ""
+ cell_split = cell_index.split("")
+ cell_split.each { |char| char.numeric? ? row << char : cell << char }
+ cell = cell_processing(cell)
+ row = row.to_i
+ @cells[row - 1][cell - 1]
+ end
+
+ def [](cell_index)
+ cell_at(cell_index)
+ end
+
+private
+ def cell_processing(cell)
+ hash_from_cell = Hash[(1...cell.size + 1).to_a.reverse.zip cell]
+ evaluated_cell = hash_from_cell.map do |key,value|
+ if key != 1
+ (value.ord - 64) * 26
+ else
+ value.ord - 64
+ end
+ end
+ cell = evaluated_cell.reduce(:+)
+ end
+end

София обнови решението на 11.01.2016 13:22 (преди около 9 години)

class String
def numeric?
Float(self) != nil rescue false
end
end
class Spreadsheet
-
def initialize(data = nil)
@data = data.strip
@rows = @data.split("\n")
@cells = @rows.map do |char|
if char.include?(" ")
char.split(" ") - [""]
else
char.split("\t")
end
end
end
def to_s
@data.to_s
end
def empty?
@data.empty?
end
def cell_at(cell_index)
+ /\b[A-Z][A-Z0-9]+\b/.match(cell_index) or
+ raise Spreadsheet::Error, "Invalid cell index #{cell_index}"
cell, row = [], ""
- cell_split = cell_index.split("")
- cell_split.each { |char| char.numeric? ? row << char : cell << char }
- cell = cell_processing(cell)
- row = row.to_i
+ cell_index.split("").each { |e| e.numeric? ? row << e : cell << e }
+ cell, row = cell_processing(cell), row.to_i
+
@cells[row - 1][cell - 1]
end
+
def [](cell_index)
- cell_at(cell_index)
+ cell_value = cell_at(cell_index)
+ if cell_value.start_with?("=")
+ cell_value.slice!("=")
+ cell_value.numeric? ? cell_value : cell_at(cell_value)
+ #/\b[A-Z][A-Z0-9]+\b/.match(cell_value) for cell_at(cell_value)
+ end
+ cell_value
end
+ class Error < StandardError
+ # attr_reader :object
+
+ # def initialize(object)
+ # @object = object
+ # end
+ end
+
+
private
def cell_processing(cell)
hash_from_cell = Hash[(1...cell.size + 1).to_a.reverse.zip cell]
evaluated_cell = hash_from_cell.map do |key,value|
- if key != 1
- (value.ord - 64) * 26
- else
- value.ord - 64
- end
+ key != 1 ? (value.ord - 64) * 26 : value.ord - 64
end
cell = evaluated_cell.reduce(:+)
+ rescue Spreadsheet::Error => e
+ puts "Cell #{cell} does not exist"
end
+
+ # def evaluate(expression)
+
+ # case expression
+ # when "ADD"
+ # expression_processing(expression, "ADD", :+)
+ # when "MULTIPLY"
+ # expression_processing(expression, "MULTIPLY", :*)
+ # when "SUBTRACT"
+ # expression_processing(expression, "SUBTRACT", :-)
+ # when "DIVIDE"
+ # expression_processing(expression, "DIVIDE", :/)
+ # when "MOD"
+ # expression_processing(expression, "MOD", :%)
+ # end
+ # end
+
+ # def expression_processing(expression, function, symbol)
+ # expression.slice!(function).slice!("(").slice!(")")
+ # expression = expression.split(",")
+ # recursion_cells = expression.find_all do |char|
+ # /\b[A-Z][A-Z0-9]+\b/.match(char)
+ # end
+ # recursion_cells = recursion_cells.map {|cell| [cell]}
+ # expression << recursion_cells
+ # expression.inject {|sum, n| sum symbol n}
+ # end
end