Решение на Осма задача от Димитър Ангелов

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

Към профила на Димитър Ангелов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 21 успешни тест(а)
  • 19 неуспешни тест(а)

Код

class Spreadsheet
FUNCTION = {
"AD" => Proc.new do | result, * rest |
rest.each {|y| result += y }
result
end,
"MU" => Proc.new do | result, * rest |
rest.each {|y| result *= y }
result
end,
"SU" => ->(x, y) { x - y },
"DI" => ->(x, y) { x / y },
"MO" => ->(x, y) { x % y }}
def initialize (values = "")
values.strip!
while values[0..1] == "\n" do values.slice!(0..1) end
while values[-2..-1] == "\n" do values.slice!(-2..-1) end
@table, @size = values.split("\n"), values.split("\n").size
@table.each_index do |index|
@table[index] = @table[index].strip.gsub(/ * /, "\t").split("\t")
end
end
def empty?
@size == 0
end
def cell_at (cell_index)
row, column = 0, 0
cell_index.each_byte do |code|
if code >= 65 then column = column * 26 + (code - 64)
else row = row * 10 + (code - 48)
end
end
raise Error, "Cell '#{cell_index}' does not exist" if @table[row - 1] == nil
@table[row - 1][column - 1]
end
def [](cell_index)
cell = cell_at(cell_index)
is_formula? (cell)
end
def to_s
result = ""
@table.each do |row|
row.each do |cell|
result << is_formula?(cell) << "\t"
end
result.chop! << "\n"
end
result.chop
end
def formula (cell)
if cell.getbyte(cell.length - 1) > 48 and
cell.getbyte(0) > 64 then self[cell]
elsif cell.getbyte(cell.length - 1) > 48 then cell
elsif cell.getbyte(cell.length - 1) == 41 then function_call(cell)
else "error"
end
end
def function_call (function_string)
x, y = [], 0
function_string.each_byte do |code|
if(code != 44 && code > 48 && code < 64) then y = y * 10 + (code - 48)
elsif (code == 44 or code == 41) then x << y
y = 0
end
end
FUNCTION[function_string[0..1]].call(*x).to_s
end
def is_formula?(cell)
if cell[0] == '=' then
formula(cell[1..cell.length])
else cell
end
end
class Error < StandardError
end
end

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

.........F........FFFFF.FF.FF.FFFFFF.FFF

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\t31\nbar\t11\t2.2\t1\nbaz\t12\t2.3\t9"
       
       (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	31
       +bar	11	2.2	1
       +baz	12	2.3	9
     # /tmp/d20160121-5693-xk2063/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#[] 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-xk2063/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)>'

  3) Spreadsheet#[] adds numbers from cell references and as immediate arguments with ADD
     Failure/Error: expect(sheet['B1']).to eq('55')
       
       expected: "55"
            got: "5"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  4) Spreadsheet#[] adds numbers only from cell references with ADD
     Failure/Error: expect(sheet['D1']).to eq('10')
       
       expected: "10"
            got: "3"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  5) Spreadsheet#[] multiplies numbers with MULTIPLY
     Failure/Error: expect(sheet2['E1']).to eq('120')
       
       expected: "120"
            got: "5"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/spec.rb:145: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#[] 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-xk2063/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)>'

  7) Spreadsheet#[] subtracts numbers via cell references
     Failure/Error: expect(sheet['D1']).to eq('4')
       
       expected: "4"
            got: "0"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  8) 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 #<ArgumentError: wrong number of arguments (1 for 2)> with backtrace:
         # /tmp/d20160121-5693-xk2063/solution.rb:11:in `block in <class:Spreadsheet>'
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `call'
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `function_call'
         # /tmp/d20160121-5693-xk2063/solution.rb:59:in `formula'
         # /tmp/d20160121-5693-xk2063/solution.rb:77:in `is_formula?'
         # /tmp/d20160121-5693-xk2063/solution.rb:41:in `[]'
         # /tmp/d20160121-5693-xk2063/spec.rb:171:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-xk2063/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-xk2063/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)>'

  9) Spreadsheet#[] divides numbers via cell references
     Failure/Error: expect(sheet1['C1']).to eq('42')
       
       expected: "42"
            got: "1"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  10) 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 #<ArgumentError: wrong number of arguments (1 for 2)> with backtrace:
         # /tmp/d20160121-5693-xk2063/solution.rb:12:in `block in <class:Spreadsheet>'
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `call'
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `function_call'
         # /tmp/d20160121-5693-xk2063/solution.rb:59:in `formula'
         # /tmp/d20160121-5693-xk2063/solution.rb:77:in `is_formula?'
         # /tmp/d20160121-5693-xk2063/solution.rb:41:in `[]'
         # /tmp/d20160121-5693-xk2063/spec.rb:195:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-xk2063/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-xk2063/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)>'

  11) Spreadsheet#[] calculates the modulo of two numbers with MOD via cell references
     Failure/Error: expect(sheet1['C1']).to eq('4')
       
       expected: "4"
            got: "0"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  12) 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 #<ArgumentError: wrong number of arguments (1 for 2)> with backtrace:
         # /tmp/d20160121-5693-xk2063/solution.rb:13:in `block in <class:Spreadsheet>'
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `call'
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `function_call'
         # /tmp/d20160121-5693-xk2063/solution.rb:59:in `formula'
         # /tmp/d20160121-5693-xk2063/solution.rb:77:in `is_formula?'
         # /tmp/d20160121-5693-xk2063/solution.rb:41:in `[]'
         # /tmp/d20160121-5693-xk2063/spec.rb:219:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-xk2063/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-xk2063/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)>'

  13) 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: "12"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  14) 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: "-10"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  15) Spreadsheet#[] multiplies floating point numbers with MULTIPLY
     Failure/Error: expect(Spreadsheet.new('10  1.1  =MULTIPLY(A1, B1)')['C1']).to eq '11'
       
       expected: "11"
            got: "1"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/spec.rb:240: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 floating point numbers with DIVIDE
     Failure/Error: expect(Spreadsheet.new('10  =DIVIDE(A1, 4)')['B1']).to eq '2.50'
       
       expected: "2.50"
            got: "0"
       
       (compared using ==)
     # /tmp/d20160121-5693-xk2063/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)>'

  17) 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 `call' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-xk2063/solution.rb:72:in `function_call'
         # /tmp/d20160121-5693-xk2063/solution.rb:59:in `formula'
         # /tmp/d20160121-5693-xk2063/solution.rb:77:in `is_formula?'
         # /tmp/d20160121-5693-xk2063/solution.rb:41:in `[]'
         # /tmp/d20160121-5693-xk2063/spec.rb:254:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-xk2063/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-xk2063/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)>'

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

  19) 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 `[]' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-xk2063/solution.rb:76:in `is_formula?'
         # /tmp/d20160121-5693-xk2063/solution.rb:41:in `[]'
         # /tmp/d20160121-5693-xk2063/solution.rb:57:in `formula'
         # /tmp/d20160121-5693-xk2063/solution.rb:77:in `is_formula?'
         # /tmp/d20160121-5693-xk2063/solution.rb:41:in `[]'
         # /tmp/d20160121-5693-xk2063/spec.rb:266:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-xk2063/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-xk2063/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.03175 seconds
40 examples, 19 failures

Failed examples:

rspec /tmp/d20160121-5693-xk2063/spec.rb:43 # Spreadsheet#to_s returns the evaluated spreadsheet as a table
rspec /tmp/d20160121-5693-xk2063/spec.rb:118 # Spreadsheet#[] raises an exception for less than two arguments passed to ADD
rspec /tmp/d20160121-5693-xk2063/spec.rb:128 # Spreadsheet#[] adds numbers from cell references and as immediate arguments with ADD
rspec /tmp/d20160121-5693-xk2063/spec.rb:134 # Spreadsheet#[] adds numbers only from cell references with ADD
rspec /tmp/d20160121-5693-xk2063/spec.rb:140 # Spreadsheet#[] multiplies numbers with MULTIPLY
rspec /tmp/d20160121-5693-xk2063/spec.rb:148 # Spreadsheet#[] raises an exception for less than two arguments to MULTIPLY
rspec /tmp/d20160121-5693-xk2063/spec.rb:164 # Spreadsheet#[] subtracts numbers via cell references
rspec /tmp/d20160121-5693-xk2063/spec.rb:170 # Spreadsheet#[] raises an exception when SUBTRACT is called with a wrong number of arguments
rspec /tmp/d20160121-5693-xk2063/spec.rb:186 # Spreadsheet#[] divides numbers via cell references
rspec /tmp/d20160121-5693-xk2063/spec.rb:194 # Spreadsheet#[] raises an exception when DIVIDE is called with a wrong number of arguments
rspec /tmp/d20160121-5693-xk2063/spec.rb:210 # Spreadsheet#[] calculates the modulo of two numbers with MOD via cell references
rspec /tmp/d20160121-5693-xk2063/spec.rb:218 # Spreadsheet#[] raises an exception when MOD is called with a wrong number of arguments
rspec /tmp/d20160121-5693-xk2063/spec.rb:228 # Spreadsheet#[] adds floating point numbers with ADD
rspec /tmp/d20160121-5693-xk2063/spec.rb:233 # Spreadsheet#[] subtracts floating point numbers with SUBTRACT
rspec /tmp/d20160121-5693-xk2063/spec.rb:238 # Spreadsheet#[] multiplies floating point numbers with MULTIPLY
rspec /tmp/d20160121-5693-xk2063/spec.rb:243 # Spreadsheet#[] divides floating point numbers with DIVIDE
rspec /tmp/d20160121-5693-xk2063/spec.rb:253 # Spreadsheet#[] raises an exception for unknown functions
rspec /tmp/d20160121-5693-xk2063/spec.rb:259 # Spreadsheet#[] raises an exception for missing cells passed as function arguments
rspec /tmp/d20160121-5693-xk2063/spec.rb:265 # Spreadsheet#[] raises an exception for invalid expressions

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

Димитър обнови решението на 11.01.2016 15:31 (преди над 8 години)

+class Spreadsheet
+ FUNCTION = {
+ "AD" => Proc.new do | result, * rest |
+ rest.each {|y| result += y }
+ result
+ end,
+ "MU" => Proc.new do | result, * rest |
+ rest.each {|y| result *= y }
+ result
+ end,
+ "SU" => ->(x, y) { x - y },
+ "DI" => ->(x, y) { x / y },
+ "MO" => ->(x, y) { x % y }}
+ def initialize (values = "")
+ values.strip!
+ while values[0..1] == "\n" do values.slice!(0..1) end
+ while values[-2..-1] == "\n" do values.slice!(-2..-1) end
+ @table, @size = values.split("\n"), values.split("\n").size
+ @table.each_index do |index|
+ @table[index] = @table[index].strip.gsub(/ * /, "\t").split("\t")
+ end
+ end
+
+ def empty?
+ @size == 0
+ end
+
+ def cell_at (cell_index)
+ row, column = 0, 0
+ cell_index.each_byte do |code|
+ if code >= 65 then column = column * 26 + (code - 64)
+ else row = row * 10 + (code - 48)
+ end
+ end
+ raise Error, "Cell '#{cell_index}' does not exist" if @table[row - 1] == nil
+ @table[row - 1][column - 1]
+ end
+
+ def [](cell_index)
+ cell = cell_at(cell_index)
+ is_formula? (cell)
+ end
+
+ def to_s
+ result = ""
+ @table.each do |row|
+ row.each do |cell|
+ result << is_formula?(cell) << "\t"
+ end
+ result.chop! << "\n"
+ end
+ result.chop
+ end
+
+ def formula (cell)
+ if cell.getbyte(cell.length - 1) > 48 and
+ cell.getbyte(0) > 64 then self[cell]
+ elsif cell.getbyte(cell.length - 1) > 48 then cell
+ elsif cell.getbyte(cell.length - 1) == 41 then function_call(cell)
+ else "error"
+ end
+ end
+
+ def function_call (function_string)
+ x, y = [], 0
+ function_string.each_byte do |code|
+ if(code != 44 && code > 48 && code < 64) then y = y * 10 + (code - 48)
+ elsif (code == 44 or code == 41) then x << y
+ y = 0
+ end
+ end
+ FUNCTION[function_string[0..1]].call(*x).to_s
+ end
+
+ def is_formula?(cell)
+ if cell[0] == '=' then
+ formula(cell[1..cell.length])
+ else cell
+ end
+ end
+
+ class Error < StandardError
+ end
+end