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

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

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

Резултати

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

Код

class Spreadsheet
attr_accessor :sheet, :rows, :columns
class Error < StandardError
def message_invalid_cell_index(cell_a_one)
"Invalid cell index #{cell_a_one}"
end
def message_cell_does_not_exist(cell_a_one)
"Cell #{cell_a_one} does not exist"
end
end
class Helpers
def self.case_helper(string_info)
case
when string_info[1..-1].strip.to_i.to_s == string_info[1..-1].strip
"#{string_info[1..-1].strip.to_i}"
when string_info[1..-1].strip.to_f.to_s == string_info[1..-1].strip
"#{string_info[1..-1].strip.to_f.round(2)}"
end
end
def self.helper(string_info)
if (not string_info.nil?) and string_info[0] == "="
case_helper(string_info)
else
"#{string_info}"
end
end
def self.message_cell_does_not_exist_helper(cell_a_one)
begin
raise Error
rescue Error => error
error.message_cell_does_not_exist(cell_a_one)
end
end
def self.message_invalid_cell_index_helper(cell_a_one)
begin
raise Error
rescue Error => error
error.message_invalid_cell_index(cell_a_one)
end
end
def self.these_restrictions_are_stupid(match, rows, columns)
(0..rows - 1).include?(match[2].to_i - 1) or
(0..columns - 1).include?(Spreadsheet::col_a_one_to(match[1]) - 1)
end
end
def initialize(information_string = nil)
@sheet = []
if not information_string.nil?
information = (information_string.strip.gsub(/^$\n/, '')).split("\n")
(0..information.size - 1).to_a.each {|line|
@sheet << information[line].strip.split(/\s{2,}|\t/)}
end
@rows, @columns = @sheet.size, @sheet[0].size
end
def empty?
@sheet == []
end
def cell_at(cell_a_one)
match = cell_a_one.match(/^\$?([A-Z]+)\$?(\d+)$/)
if match.nil?
Helpers::message_invalid_cell_index_helper(cell_a_one)
elsif not Helpers::these_restrictions_are_stupid(match, @rows, @columns)
Helpers::message_cell_does_not_exist_helper(cell_a_one)
else
@sheet[match[2].to_i - 1][col_a_one_to_index(match[1]) - 1]
end
end
def [](cell_a_one)
string_info = cell_at(cell_a_one)
if (not string_info.nil?) and string_info[0] == "="
Helpers::case_helper(string_info)
else
"#{string_info}"
end
end
def col_a_one_to_index(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
def self.col_a_one_to(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
def to_s
sheet_as_a_string = ""
@sheet.each do |line|
line.each do |element|
sheet_as_a_string << Helpers::helper(element) + "\t"
end
sheet_as_a_string.strip! << "\n"
end
sheet_as_a_string.strip!
end
end

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

FFF.F....FF.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 `size' for nil:NilClass
     # /tmp/d20160121-5693-rdpm7z/solution.rb:62:in `initialize'
     # /tmp/d20160121-5693-rdpm7z/spec.rb:4:in `new'
     # /tmp/d20160121-5693-rdpm7z/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 `size' for nil:NilClass
     # /tmp/d20160121-5693-rdpm7z/solution.rb:62:in `initialize'
     # /tmp/d20160121-5693-rdpm7z/spec.rb:10:in `new'
     # /tmp/d20160121-5693-rdpm7z/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#new creates a blank sheet when a blank string is passed
     Failure/Error: expect(Spreadsheet.new('')).to be_empty
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20160121-5693-rdpm7z/solution.rb:62:in `initialize'
     # /tmp/d20160121-5693-rdpm7z/spec.rb:14:in `new'
     # /tmp/d20160121-5693-rdpm7z/spec.rb:14: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 returns blank tables as blank strings
     Failure/Error: expect(Spreadsheet.new.to_s).to eq ''
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20160121-5693-rdpm7z/solution.rb:62:in `initialize'
     # /tmp/d20160121-5693-rdpm7z/spec.rb:24:in `new'
     # /tmp/d20160121-5693-rdpm7z/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)>'

  5) Spreadsheet#to_s returns the evaluated spreadsheet as a table
     Failure/Error: expect(sheet.to_s).to eq \
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20160121-5693-rdpm7z/solution.rb:113:in `block (2 levels) in to_s'
     # /tmp/d20160121-5693-rdpm7z/solution.rb:112:in `each'
     # /tmp/d20160121-5693-rdpm7z/solution.rb:112:in `block in to_s'
     # /tmp/d20160121-5693-rdpm7z/solution.rb:111:in `each'
     # /tmp/d20160121-5693-rdpm7z/solution.rb:111:in `to_s'
     # /tmp/d20160121-5693-rdpm7z/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/ but nothing was raised
     # /tmp/d20160121-5693-rdpm7z/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 `size' for nil:NilClass> with backtrace:
         # /tmp/d20160121-5693-rdpm7z/solution.rb:62:in `initialize'
         # /tmp/d20160121-5693-rdpm7z/spec.rb:75:in `new'
         # /tmp/d20160121-5693-rdpm7z/spec.rb:75:in `block (4 levels) in <top (required)>'
         # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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'
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "15"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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')
       
       expected: "55"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "10"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "120"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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')
       
       expected: "2"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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')
       
       expected: "42"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "42"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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')
       
       expected: "2"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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')
       
       expected: "4"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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'
       
       expected: "11.10"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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'
       
       expected: "8.90"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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'
       
       expected: "11"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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'
       
       expected: "2.50"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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'
       
       expected: "1"
            got: nil
       
       (compared using ==)
     # /tmp/d20160121-5693-rdpm7z/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'/ but nothing was raised
     # /tmp/d20160121-5693-rdpm7z/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/ but nothing was raised
     # /tmp/d20160121-5693-rdpm7z/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-rdpm7z/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.02628 seconds
40 examples, 32 failures

Failed examples:

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

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

Пламена обнови решението на 10.01.2016 14:32 (преди над 8 години)

+class Spreadsheet
+ attr_accessor :sheet
+
+ class Error < StandardError
+ def message(cell_a_one)
+ "Invalid cell index #{cell_a_one}"
+ end
+ end
+
+ def initialize(information_string = nil)
+ @sheet = []
+ if not information_string.nil?
+ information = (information_string.strip.gsub(/^$\n/, '')).split("\n")
+ (0..information.size - 1).to_a.each {|line|
+ @sheet << information[line].strip.split(/\s{2,}|\t/)}
+ end
+ end
+
+ def empty?
+ @sheet == []
+ end
+
+ def cell_at(cell_a_one)
+ match = cell_a_one.match(/^\$?([A-Z]+)\$?(\d+)$/)
+ if match.nil?
+ begin
+ raise Error
+ rescue Error => error
+ puts error.message(cell_a_one)
+ end
+ else
+ @sheet[row_a_one_to_index(match[2]) - 1][col_a_one_to_index(match[1]) - 1]
+ end
+ end
+
+ def col_a_one_to_index(col_a_one)
+ sum, a = 0, "A".unpack('U')[0]
+ source = "Z".unpack('U')[0] - a + 1
+ col_a_one.split("").each do |i|
+ char = i.unpack('U')[0]
+ sum = sum * source + char - a + 1
+ end
+ sum
+ end
+
+ def row_a_one_to_index(row_a_one)
+ row_a_one.to_i
+ end
+end

Пламена обнови решението на 10.01.2016 22:21 (преди над 8 години)

class Spreadsheet
- attr_accessor :sheet
+ attr_accessor :sheet, :rows, :columns
class Error < StandardError
- def message(cell_a_one)
+ def message_invalid_cell_index(cell_a_one)
"Invalid cell index #{cell_a_one}"
end
+
+ def message_cell_does_not_exist(cell_a_one)
+ "Cell #{cell_a_one} does not exist"
+ end
end
+ class Helpers
+ def self.case_helper(string_info)
+ case
+ when string_info[1..-1].strip.to_i.to_s == string_info[1..-1].strip
+ "#{string_info[1..-1].strip.to_i}"
+ when string_info[1..-1].strip.to_f.to_s == string_info[1..-1].strip
+ "#{string_info[1..-1].strip.to_f.round(2)}"
+ when string_info[1..-1].strip =~ /^[A-Z]+\d+$/
+ "#{cell_at(string_info[1..-1].strip)}"
+ end
+ end
+
+ def self.helper(string_info)
+ if (not string_info.nil?) and string_info[0] == "="
+ case_helper(string_info)
+ else
+ "#{string_info}"
+ end
+ end
+
+ def self.message_cell_does_not_exist_helper(cell_a_one)
+ begin
+ raise Error
+ rescue Error => error
+ error.message_cell_does_not_exist(cell_a_one)
+ end
+ end
+
+ def self.message_invalid_cell_index_helper(cell_a_one)
+ begin
+ raise Error
+ rescue Error => error
+ error.message_invalid_cell_index(cell_a_one)
+ end
+ end
+
+ def self.these_restrictions_are_stupid(match, rows, columns)
+ (0..rows - 1).include?(match[2].to_i - 1) or
+ (0..columns - 1).include?(Spreadsheet::col_a_one_to(match[1]) - 1)
+ end
+ end
+
+
def initialize(information_string = nil)
@sheet = []
if not information_string.nil?
information = (information_string.strip.gsub(/^$\n/, '')).split("\n")
(0..information.size - 1).to_a.each {|line|
@sheet << information[line].strip.split(/\s{2,}|\t/)}
end
+ @rows, @columns = @sheet.size, @sheet[0].size
end
def empty?
@sheet == []
end
def cell_at(cell_a_one)
match = cell_a_one.match(/^\$?([A-Z]+)\$?(\d+)$/)
if match.nil?
- begin
- raise Error
- rescue Error => error
- puts error.message(cell_a_one)
- end
+ Helpers::message_cell_does_not_exist_helper(cell_a_one)
+ elsif not Helpers::these_restrictions_are_stupid(match, @rows, @columns)
+ Helpers::message_cell_does_not_exist_helper(cell_a_one)
else
- @sheet[row_a_one_to_index(match[2]) - 1][col_a_one_to_index(match[1]) - 1]
+ @sheet[match[2].to_i - 1][col_a_one_to_index(match[1]) - 1]
end
end
+ def [](cell_a_one)
+ string_info = cell_at(cell_a_one)
+ if (not string_info.nil?) and string_info[0] == "="
+ Helpers::case_helper(string_info)
+ else
+ "#{string_info}"
+ end
+ end
+
def col_a_one_to_index(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
- def row_a_one_to_index(row_a_one)
- row_a_one.to_i
+ def self.col_a_one_to(col_a_one)
+ sum, a = 0, "A".unpack('U')[0]
+ source = "Z".unpack('U')[0] - a + 1
+ col_a_one.split("").each do |i|
+ char = i.unpack('U')[0]
+ sum = sum * source + char - a + 1
+ end
+ sum
+ end
+
+ def to_s
+ sheet_as_a_string = ""
+ @sheet.each do |line|
+ line.each do |element|
+ sheet_as_a_string << Helpers::helper(element) + "\t"
+ end
+ sheet_as_a_string.strip! << "\n"
+ end
+ sheet_as_a_string.strip!
end
end

Пламена обнови решението на 11.01.2016 15:59 (преди над 8 години)

class Spreadsheet
attr_accessor :sheet, :rows, :columns
class Error < StandardError
def message_invalid_cell_index(cell_a_one)
"Invalid cell index #{cell_a_one}"
end
def message_cell_does_not_exist(cell_a_one)
"Cell #{cell_a_one} does not exist"
end
end
class Helpers
def self.case_helper(string_info)
case
when string_info[1..-1].strip.to_i.to_s == string_info[1..-1].strip
"#{string_info[1..-1].strip.to_i}"
when string_info[1..-1].strip.to_f.to_s == string_info[1..-1].strip
"#{string_info[1..-1].strip.to_f.round(2)}"
when string_info[1..-1].strip =~ /^[A-Z]+\d+$/
"#{cell_at(string_info[1..-1].strip)}"
end
end
def self.helper(string_info)
if (not string_info.nil?) and string_info[0] == "="
case_helper(string_info)
else
"#{string_info}"
end
end
def self.message_cell_does_not_exist_helper(cell_a_one)
begin
raise Error
rescue Error => error
error.message_cell_does_not_exist(cell_a_one)
end
end
def self.message_invalid_cell_index_helper(cell_a_one)
begin
raise Error
rescue Error => error
error.message_invalid_cell_index(cell_a_one)
end
end
def self.these_restrictions_are_stupid(match, rows, columns)
(0..rows - 1).include?(match[2].to_i - 1) or
(0..columns - 1).include?(Spreadsheet::col_a_one_to(match[1]) - 1)
end
end
def initialize(information_string = nil)
@sheet = []
if not information_string.nil?
information = (information_string.strip.gsub(/^$\n/, '')).split("\n")
(0..information.size - 1).to_a.each {|line|
@sheet << information[line].strip.split(/\s{2,}|\t/)}
end
@rows, @columns = @sheet.size, @sheet[0].size
end
def empty?
@sheet == []
end
def cell_at(cell_a_one)
match = cell_a_one.match(/^\$?([A-Z]+)\$?(\d+)$/)
if match.nil?
- Helpers::message_cell_does_not_exist_helper(cell_a_one)
+ Helpers::message_invalid_cell_index_helper(cell_a_one)
elsif not Helpers::these_restrictions_are_stupid(match, @rows, @columns)
Helpers::message_cell_does_not_exist_helper(cell_a_one)
else
@sheet[match[2].to_i - 1][col_a_one_to_index(match[1]) - 1]
end
end
def [](cell_a_one)
string_info = cell_at(cell_a_one)
if (not string_info.nil?) and string_info[0] == "="
Helpers::case_helper(string_info)
else
"#{string_info}"
end
end
def col_a_one_to_index(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
def self.col_a_one_to(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
def to_s
sheet_as_a_string = ""
@sheet.each do |line|
line.each do |element|
sheet_as_a_string << Helpers::helper(element) + "\t"
end
sheet_as_a_string.strip! << "\n"
end
sheet_as_a_string.strip!
end
-end
+end

Пламена обнови решението на 11.01.2016 16:25 (преди над 8 години)

class Spreadsheet
attr_accessor :sheet, :rows, :columns
class Error < StandardError
def message_invalid_cell_index(cell_a_one)
"Invalid cell index #{cell_a_one}"
end
def message_cell_does_not_exist(cell_a_one)
"Cell #{cell_a_one} does not exist"
end
end
class Helpers
def self.case_helper(string_info)
case
when string_info[1..-1].strip.to_i.to_s == string_info[1..-1].strip
"#{string_info[1..-1].strip.to_i}"
when string_info[1..-1].strip.to_f.to_s == string_info[1..-1].strip
"#{string_info[1..-1].strip.to_f.round(2)}"
- when string_info[1..-1].strip =~ /^[A-Z]+\d+$/
- "#{cell_at(string_info[1..-1].strip)}"
end
end
def self.helper(string_info)
if (not string_info.nil?) and string_info[0] == "="
case_helper(string_info)
else
"#{string_info}"
end
end
def self.message_cell_does_not_exist_helper(cell_a_one)
begin
raise Error
rescue Error => error
error.message_cell_does_not_exist(cell_a_one)
end
end
def self.message_invalid_cell_index_helper(cell_a_one)
begin
raise Error
rescue Error => error
error.message_invalid_cell_index(cell_a_one)
end
end
def self.these_restrictions_are_stupid(match, rows, columns)
(0..rows - 1).include?(match[2].to_i - 1) or
(0..columns - 1).include?(Spreadsheet::col_a_one_to(match[1]) - 1)
end
end
def initialize(information_string = nil)
@sheet = []
if not information_string.nil?
information = (information_string.strip.gsub(/^$\n/, '')).split("\n")
(0..information.size - 1).to_a.each {|line|
@sheet << information[line].strip.split(/\s{2,}|\t/)}
end
@rows, @columns = @sheet.size, @sheet[0].size
end
def empty?
@sheet == []
end
def cell_at(cell_a_one)
match = cell_a_one.match(/^\$?([A-Z]+)\$?(\d+)$/)
if match.nil?
Helpers::message_invalid_cell_index_helper(cell_a_one)
elsif not Helpers::these_restrictions_are_stupid(match, @rows, @columns)
Helpers::message_cell_does_not_exist_helper(cell_a_one)
else
@sheet[match[2].to_i - 1][col_a_one_to_index(match[1]) - 1]
end
end
def [](cell_a_one)
string_info = cell_at(cell_a_one)
if (not string_info.nil?) and string_info[0] == "="
Helpers::case_helper(string_info)
else
"#{string_info}"
end
end
def col_a_one_to_index(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
def self.col_a_one_to(col_a_one)
sum, a = 0, "A".unpack('U')[0]
source = "Z".unpack('U')[0] - a + 1
col_a_one.split("").each do |i|
char = i.unpack('U')[0]
sum = sum * source + char - a + 1
end
sum
end
def to_s
sheet_as_a_string = ""
@sheet.each do |line|
line.each do |element|
sheet_as_a_string << Helpers::helper(element) + "\t"
end
sheet_as_a_string.strip! << "\n"
end
sheet_as_a_string.strip!
end
end