From 1f3a92ba3ca92efa72d8d60ebb83eb8bf6bbbb8a Mon Sep 17 00:00:00 2001 From: Rene Alfaro Date: Thu, 9 Jul 2015 12:21:57 -0500 Subject: [PATCH 1/4] Copying and refactoring of the original version bassed on passing hashes: basic test passing --- ruby/potter/5/potter.rb | 24 +++++++++++++++++++++++ ruby/potter/5/potter_test.rb | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 ruby/potter/5/potter.rb create mode 100644 ruby/potter/5/potter_test.rb diff --git a/ruby/potter/5/potter.rb b/ruby/potter/5/potter.rb new file mode 100644 index 0000000..ed719ef --- /dev/null +++ b/ruby/potter/5/potter.rb @@ -0,0 +1,24 @@ +class Potter + + attr_accessor :books + #Constants + BOOK_PRICE = 8 + PERC_1 = 0.95 + PERC_2 = 0.90 + PERC_3 = 0.80 + PERC_4 = 0.75 + + def initialize(books) + @price = 0 + @books = books || [] + end + +#Determines when the book collection should be submitted to a discount + def price + return 0 if books.empty? + if books.length == 1 + return books.size * BOOK_PRICE + end + + end +end \ No newline at end of file diff --git a/ruby/potter/5/potter_test.rb b/ruby/potter/5/potter_test.rb new file mode 100644 index 0000000..0242faa --- /dev/null +++ b/ruby/potter/5/potter_test.rb @@ -0,0 +1,37 @@ +require "minitest/autorun" +require './potter' + +class Test_Potter < Minitest::Test + + def test_price_basic + assert_equal(0, Potter.new([]).price) + assert_equal(8, Potter.new([0]).price) + assert_equal(8, Potter.new([1]).price) + assert_equal(8, Potter.new([2]).price) + assert_equal(8, Potter.new([3]).price) + assert_equal(8, Potter.new([4]).price) + assert_equal(8 * 2 , Potter.new([0, 0]).price) + assert_equal(8 * 3, Potter.new([1, 1, 1]).price) + end + + def test_simple_discounts + #assert_equal(8 * 2 * 0.95, Potter.new([0, 1]).price) + #assert_equal(8 * 3 * 0.9, Potter.new([0, 2, 4]).price) + #assert_equal(8 * 4 * 0.8, Potter.new([0, 1, 2, 4]).price) + #assert_equal(8 * 5 * 0.75, Potter.new([0, 1, 2, 3, 4]).price) + end + + def test_several_discounts + #assert_equal(8 + (8 * 2 * 0.95), Potter.new([0, 0, 1]).price) + #assert_equal(2 * (8 * 2 * 0.95), Potter.new([0, 0, 1, 1]).price) + #assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), Potter.new([0, 0, 1, 2, 2, 3]).price) + #assert_equal(8 + (8 * 5 * 0.75), Potter.new([0, 1, 1, 2, 3, 4]).price) + end + + def test_edgeCases + #assert_equal(2 * (8 * 4 * 0.8), Potter.new([0, 0, 1, 1, 2, 2, 3, 4]).price) + #assert_equal(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8), + #Potter.new([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4]).price) + end + +end \ No newline at end of file From d23ba85fc6377321fc6cdece054b4013a8b5b5fc Mon Sep 17 00:00:00 2001 From: Rene Alfaro Date: Thu, 9 Jul 2015 12:37:30 -0500 Subject: [PATCH 2/4] Joshi's iteration coupling applied: all test are passing now --- ruby/potter/5/potter.rb | 38 +++++++++++++++++++++++++++++++++++- ruby/potter/5/potter_test.rb | 8 ++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ruby/potter/5/potter.rb b/ruby/potter/5/potter.rb index ed719ef..c5af7f8 100644 --- a/ruby/potter/5/potter.rb +++ b/ruby/potter/5/potter.rb @@ -18,7 +18,43 @@ def price return 0 if books.empty? if books.length == 1 return books.size * BOOK_PRICE + else + #Dirty little trick that calculates when the edge cases occurs + begin + repeated_books = [] + books.delete_if do |i| + unless repeated_books.include? i + repeated_books.push i + end + end + if books.length == 3 && repeated_books.length == 5 + books.push(5) + repeated_books.pop + end + total_disscount(repeated_books.uniq.size) + end while books.any? end - + @price + end + + #Main disscount of calculations for the givven cart. + def total_disscount(books) + @price += BOOK_PRICE * books * available_disscounts(books) + end + + # Return 1 if the size of the array + def available_disscounts(books) + case books + when 1 + return 1 + when 2 + PERC_1 + when 3 + PERC_2 + when 4 + PERC_3 + when 5 + PERC_4 + end end end \ No newline at end of file diff --git a/ruby/potter/5/potter_test.rb b/ruby/potter/5/potter_test.rb index 0242faa..4c7067b 100644 --- a/ruby/potter/5/potter_test.rb +++ b/ruby/potter/5/potter_test.rb @@ -15,10 +15,10 @@ def test_price_basic end def test_simple_discounts - #assert_equal(8 * 2 * 0.95, Potter.new([0, 1]).price) - #assert_equal(8 * 3 * 0.9, Potter.new([0, 2, 4]).price) - #assert_equal(8 * 4 * 0.8, Potter.new([0, 1, 2, 4]).price) - #assert_equal(8 * 5 * 0.75, Potter.new([0, 1, 2, 3, 4]).price) + assert_equal(8 * 2 * 0.95, Potter.new([0, 1]).price) + assert_equal(8 * 3 * 0.9, Potter.new([0, 2, 4]).price) + assert_equal(8 * 4 * 0.8, Potter.new([0, 1, 2, 4]).price) + assert_equal(8 * 5 * 0.75, Potter.new([0, 1, 2, 3, 4]).price) end def test_several_discounts From ec9b90c5d4890f525ee6edb9d66c347c43194ff2 Mon Sep 17 00:00:00 2001 From: Rene Alfaro Date: Thu, 9 Jul 2015 12:41:27 -0500 Subject: [PATCH 3/4] I forgot to uncomment my latest tests --- ruby/potter/5/potter_test.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ruby/potter/5/potter_test.rb b/ruby/potter/5/potter_test.rb index 4c7067b..a3c2075 100644 --- a/ruby/potter/5/potter_test.rb +++ b/ruby/potter/5/potter_test.rb @@ -22,16 +22,16 @@ def test_simple_discounts end def test_several_discounts - #assert_equal(8 + (8 * 2 * 0.95), Potter.new([0, 0, 1]).price) - #assert_equal(2 * (8 * 2 * 0.95), Potter.new([0, 0, 1, 1]).price) - #assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), Potter.new([0, 0, 1, 2, 2, 3]).price) - #assert_equal(8 + (8 * 5 * 0.75), Potter.new([0, 1, 1, 2, 3, 4]).price) + assert_equal(8 + (8 * 2 * 0.95), Potter.new([0, 0, 1]).price) + assert_equal(2 * (8 * 2 * 0.95), Potter.new([0, 0, 1, 1]).price) + assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), Potter.new([0, 0, 1, 2, 2, 3]).price) + assert_equal(8 + (8 * 5 * 0.75), Potter.new([0, 1, 1, 2, 3, 4]).price) end def test_edgeCases - #assert_equal(2 * (8 * 4 * 0.8), Potter.new([0, 0, 1, 1, 2, 2, 3, 4]).price) - #assert_equal(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8), - #Potter.new([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4]).price) + assert_equal(2 * (8 * 4 * 0.8), Potter.new([0, 0, 1, 1, 2, 2, 3, 4]).price) + assert_equal(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8), + Potter.new([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4]).price) end end \ No newline at end of file From c7d28cf40d9395af74735cb2c9f4af69074c3887 Mon Sep 17 00:00:00 2001 From: Rene Alfaro Date: Thu, 9 Jul 2015 21:21:53 -0500 Subject: [PATCH 4/4] des - plagiarism: basics and simple passed --- ruby/potter/5/potter.rb | 79 ++++++++++++------------------------ ruby/potter/5/potter_test.rb | 45 +++++++++++--------- 2 files changed, 51 insertions(+), 73 deletions(-) diff --git a/ruby/potter/5/potter.rb b/ruby/potter/5/potter.rb index c5af7f8..dad6367 100644 --- a/ruby/potter/5/potter.rb +++ b/ruby/potter/5/potter.rb @@ -1,60 +1,33 @@ -class Potter - - attr_accessor :books +class Potter + #Constants - BOOK_PRICE = 8 - PERC_1 = 0.95 - PERC_2 = 0.90 - PERC_3 = 0.80 - PERC_4 = 0.75 - - def initialize(books) - @price = 0 - @books = books || [] - end + BPRICE = 8 + #Main invocation for returning the price + def price(books) + #Checks for an empty collection + return 0 if books.empty? + if books.uniq.size == 1 + BPRICE * books.count + else + count_collection(books) + end -#Determines when the book collection should be submitted to a discount - def price - return 0 if books.empty? - if books.length == 1 - return books.size * BOOK_PRICE - else - #Dirty little trick that calculates when the edge cases occurs - begin - repeated_books = [] - books.delete_if do |i| - unless repeated_books.include? i - repeated_books.push i - end - end - if books.length == 3 && repeated_books.length == 5 - books.push(5) - repeated_books.pop - end - total_disscount(repeated_books.uniq.size) - end while books.any? - end - @price end - #Main disscount of calculations for the givven cart. - def total_disscount(books) - @price += BOOK_PRICE * books * available_disscounts(books) - end - # Return 1 if the size of the array - def available_disscounts(books) - case books - when 1 - return 1 - when 2 - PERC_1 - when 3 - PERC_2 - when 4 - PERC_3 - when 5 - PERC_4 - end + def count_collection(books) + collection = books.clone.uniq.size + sum = 0 + case collection + when 2 + sum += BPRICE * collection * 0.95 + when 3 + sum += BPRICE * collection * 0.90 + when 4 + sum += BPRICE * collection * 0.80 + when 5 + sum += BPRICE * collection * 0.75 + end end + end \ No newline at end of file diff --git a/ruby/potter/5/potter_test.rb b/ruby/potter/5/potter_test.rb index a3c2075..b5ed955 100644 --- a/ruby/potter/5/potter_test.rb +++ b/ruby/potter/5/potter_test.rb @@ -2,36 +2,41 @@ require './potter' class Test_Potter < Minitest::Test + def setup + @potter = Potter.new + end + + def test_basics + assert_equal(0, @potter.price([])) + assert_equal(8, @potter.price([0])) + assert_equal(8, @potter.price([0])) + assert_equal(8, @potter.price([0])) + assert_equal(8, @potter.price([0])) + assert_equal(8, @potter.price([0])) - def test_price_basic - assert_equal(0, Potter.new([]).price) - assert_equal(8, Potter.new([0]).price) - assert_equal(8, Potter.new([1]).price) - assert_equal(8, Potter.new([2]).price) - assert_equal(8, Potter.new([3]).price) - assert_equal(8, Potter.new([4]).price) - assert_equal(8 * 2 , Potter.new([0, 0]).price) - assert_equal(8 * 3, Potter.new([1, 1, 1]).price) + assert_equal(8 * 2, @potter.price([0, 0])) + assert_equal(8 * 3, @potter.price([0, 0, 0])) end def test_simple_discounts - assert_equal(8 * 2 * 0.95, Potter.new([0, 1]).price) - assert_equal(8 * 3 * 0.9, Potter.new([0, 2, 4]).price) - assert_equal(8 * 4 * 0.8, Potter.new([0, 1, 2, 4]).price) - assert_equal(8 * 5 * 0.75, Potter.new([0, 1, 2, 3, 4]).price) + assert_equal(8 * 2 * 0.95, @potter.price([0, 1])) #15.2 + assert_equal(8 * 3 * 0.90, @potter.price([0, 1, 2])) #21.6 + assert_equal(8 * 4 * 0.80, @potter.price([0, 1, 2, 3])) #25.6 + assert_equal(8 * 5 * 0.75, @potter.price([0, 1, 2, 3, 4])) #30.0 + end def test_several_discounts - assert_equal(8 + (8 * 2 * 0.95), Potter.new([0, 0, 1]).price) - assert_equal(2 * (8 * 2 * 0.95), Potter.new([0, 0, 1, 1]).price) - assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), Potter.new([0, 0, 1, 2, 2, 3]).price) - assert_equal(8 + (8 * 5 * 0.75), Potter.new([0, 1, 1, 2, 3, 4]).price) + assert_equal(8 + (8 * 2 * 0.95), @potter.price([0, 0, 1])) #23.2 + #assert_equal(2 * (8 * 2 * 0.95), @potter.price({b1: 2, b2: 2, b3: 0, b4: 0, b5: 0})) #30.4 + #assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), @potter.price({b1: 2, b2: 1, b3: 2, b4: 1, b5: 0})) #40.8 + #assert_equal(8 + (8 * 5 * 0.75), @potter.price({b1: 1, b2: 2, b3: 1, b4: 1, b5: 1})) #38 end - def test_edgeCases - assert_equal(2 * (8 * 4 * 0.8), Potter.new([0, 0, 1, 1, 2, 2, 3, 4]).price) + def edge_cases + assert_equal(2 * (8 * 4 * 0.80), @potter.price({b1: 2, b2: 2, b3: 2, b4: 1, b5: 1})) assert_equal(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8), - Potter.new([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4]).price) + @potter.price({b1: 5, b2: 5, b3: 4, b4: 5, b5: 4})) end end \ No newline at end of file