From fdc81223ddb25535a882db93bde3b33698fc8ff2 Mon Sep 17 00:00:00 2001 From: Jon de la Vega Date: Mon, 31 Jan 2011 10:54:04 +0100 Subject: [PATCH 1/2] empiezo con el delimitador avanzado --- deadsunrise/calc.rb | 44 +++++++++++++++++++++++++++++++ deadsunrise/calc_spec.rb | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 deadsunrise/calc.rb create mode 100644 deadsunrise/calc_spec.rb diff --git a/deadsunrise/calc.rb b/deadsunrise/calc.rb new file mode 100644 index 0000000..7ec901f --- /dev/null +++ b/deadsunrise/calc.rb @@ -0,0 +1,44 @@ +class Calc + + def add(operation) + return 0 if operation == '' + @delimiter = parse_delimiter(operation) + @clean_operation = parse(operation) + calculate(@clean_operation) + end + + private + + def parse(operation) + operands = operation.gsub(/\/\/.\n/ , '').gsub("\n",',').split(@delimiter) + reject_big(operands) + negative_operands?(operands) + operands.map! {|s| s.to_i } + end + + + def parse_delimiter(operation) + delimiter = ',' + if operation[0..1] == "//" + delimiter = operation[2..2] + end + delimiter + end + + + def negative_operands?(operands) + negatives = operands.reject {|i| i.to_i >= 0 } + raise("negatives not allowed #{negatives.join(',')}") unless negatives.empty? + end + + + def reject_big(operands) + operands.reject! {|i| i.to_i > 1000} + end + + + def calculate(operands) + operands.reduce(:+) + end + +end diff --git a/deadsunrise/calc_spec.rb b/deadsunrise/calc_spec.rb new file mode 100644 index 0000000..867f3cc --- /dev/null +++ b/deadsunrise/calc_spec.rb @@ -0,0 +1,57 @@ +require 'calc' + +describe 'calc' do + + describe 'add' do + + before(:each) do + @calc = Calc.new + end + + it "should return 0 with an empty arg" do + @calc.add("").should == 0 + end + + it "should return 1 with 1" do + @calc.add('1').should == 1 + end + + it "should return 3 with 1,2" do + @calc.add("1,2").should == 3 + end + + it "should sum 2 or more numbers" do + @calc.add("1,2,3").should == 6 + end + + it "should allow new lines between numbers" do + @calc.add("1,2,3").should == 6 + @calc.add("1\n2,3").should == 6 + end + + it "should allow different delimiters" do + @calc.add("//;\n1").should == 1 + @calc.add("//:\n1:2").should == 3 + @calc.add("//;\n1;2;3").should == 6 + + end + + it "should rise an exception if numbers are negative" do + lambda {@calc.add("-2")}.should raise_error(RuntimeError, 'negatives not allowed -2') + lambda {@calc.add("1,-2,3")}.should raise_error(RuntimeError, 'negatives not allowed -2') + lambda {@calc.add("1,-2,-3")}.should raise_error(RuntimeError, 'negatives not allowed -2,-3') + end + + + it "should ignore numbers bigger than 1000" do + @calc.add("1,2000,2").should == 3 + end + + it "should allow delimiters longer than 1 char" do + @calc.add("//[***]\n1***2***3").should == 6 + end + + + end + +end From 4fad859dacd0231e7fda540100e527526c503249 Mon Sep 17 00:00:00 2001 From: jon de la vega Date: Mon, 31 Jan 2011 18:26:43 +0100 Subject: [PATCH 2/2] primera version ruby + rspec --- deadsunrise/calc.rb | 35 +++++++++++++++++++---------------- deadsunrise/calc_spec.rb | 11 ++++++++++- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/deadsunrise/calc.rb b/deadsunrise/calc.rb index 7ec901f..94be4f7 100644 --- a/deadsunrise/calc.rb +++ b/deadsunrise/calc.rb @@ -2,38 +2,41 @@ class Calc def add(operation) return 0 if operation == '' - @delimiter = parse_delimiter(operation) - @clean_operation = parse(operation) - calculate(@clean_operation) + @clean_operands = clean(operation) + calculate(@clean_operands) end private - def parse(operation) - operands = operation.gsub(/\/\/.\n/ , '').gsub("\n",',').split(@delimiter) + def clean(operation) + operands = replace_delimiters(operation) + check_for_negative_operands?(operands) reject_big(operands) - negative_operands?(operands) - operands.map! {|s| s.to_i } + return operands end - def parse_delimiter(operation) - delimiter = ',' - if operation[0..1] == "//" - delimiter = operation[2..2] - end - delimiter + def replace_delimiters(operation) + delimiters = parse_delimiter(operation) + operands = operation.gsub(/\/\/.*\n/ , '') + delimiters.flatten.each { |d| operands.gsub!(d.to_s,',') } + operands.split(',').map! {|s| s.to_i } end + def parse_delimiter(operation) + delimiters = ["\n"] + delimiters << operation.scan(/\[([^[]+)\]/) + delimiters << operation.scan(/\/\/(.)\n/) + end - def negative_operands?(operands) - negatives = operands.reject {|i| i.to_i >= 0 } + def check_for_negative_operands?(operands) + negatives = operands.reject {|i| i >= 0 } raise("negatives not allowed #{negatives.join(',')}") unless negatives.empty? end def reject_big(operands) - operands.reject! {|i| i.to_i > 1000} + operands.reject! {|i| i > 1000} end diff --git a/deadsunrise/calc_spec.rb b/deadsunrise/calc_spec.rb index 867f3cc..f92985b 100644 --- a/deadsunrise/calc_spec.rb +++ b/deadsunrise/calc_spec.rb @@ -39,7 +39,7 @@ it "should rise an exception if numbers are negative" do lambda {@calc.add("-2")}.should raise_error(RuntimeError, 'negatives not allowed -2') lambda {@calc.add("1,-2,3")}.should raise_error(RuntimeError, 'negatives not allowed -2') - lambda {@calc.add("1,-2,-3")}.should raise_error(RuntimeError, 'negatives not allowed -2,-3') + lambda {@calc.add("//;\n1;2;1,-2,-3")}.should raise_error(RuntimeError, 'negatives not allowed -2,-3') end @@ -51,6 +51,15 @@ @calc.add("//[***]\n1***2***3").should == 6 end + it "should allow multiple delimiters" do + @calc.add("//[*][%]\n1*2%3").should == 6 + end + + it "should allow multiple delimiters longer than 1char" do + @calc.add("//[,,,][;][;;]\n1,,,2;3").should == 6 + @calc.add("//;[***]\n1,***2,3").should == 6 + end + end