Skip to content

Instantly share code, notes, and snippets.

@nestorsalceda
Created February 5, 2018 19:37
Show Gist options
  • Save nestorsalceda/ce4bb6859defe6b1423854f2b7eb9a6a to your computer and use it in GitHub Desktop.
Save nestorsalceda/ce4bb6859defe6b1423854f2b7eb9a6a to your computer and use it in GitHub Desktop.
from mamba import description, it
from itertools import groupby
def price(books):
if not books:
return 0
numeroDeCada = [len(list(group)) for key, group in groupby(books)]
numeroDeLibrosDistintos=len(numeroDeCada)
cantidadDeLibros=min(numeroDeCada)
precio = 8 * numeroDeLibrosDistintos * cantidadDeLibros
if (numeroDeLibrosDistintos==2):
return precio * 0.95
return precio
with description('Potter kata') as self:
with it('does not apply any discount with only one book'):
assert 0 == price([])
assert 8 == price([1])
assert 8 == price([2])
assert 8 == price([3])
assert 8 == price([4])
with it('does not apply a discount buying same book'):
assert 16 == price([0, 0])
assert 24 == price([1, 1, 1])
with context('when applying discounts'):
with it('applies a 95% discount when two different books'):
assert 8 * 2 * 0.95 == price([0, 1])
assert 8 * 3 * 0.9 == price([0, 2, 4]))
#def testBasics
# assert_equal(0, price([]))
# assert_equal(8, price([0]))
# assert_equal(8, price([1]))
# assert_equal(8, price([2]))
# assert_equal(8, price([3]))
# assert_equal(8, price([4]))
# assert_equal(8 * 2, price([0, 0]))
# assert_equal(8 * 3, price([1, 1, 1]))
#end
#def testSimpleDiscounts
# assert_equal(8 * 2 * 0.95, price([0, 1]))
# assert_equal(8 * 3 * 0.9, price([0, 2, 4]))
# assert_equal(8 * 4 * 0.8, price([0, 1, 2, 4]))
# assert_equal(8 * 5 * 0.75, price([0, 1, 2, 3, 4]))
#end
#def testSeveralDiscounts
# assert_equal(8 + (8 * 2 * 0.95), price([0, 0, 1]))
# assert_equal(2 * (8 * 2 * 0.95), price([0, 0, 1, 1]))
# assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), price([0, 0, 1, 2, 2, 3]))
# assert_equal(8 + (8 * 5 * 0.75), price([0, 1, 1, 2, 3, 4]))
#end
#def testEdgeCases
# assert_equal(2 * (8 * 4 * 0.8), price([0, 0, 1, 1, 2, 2, 3, 4]))
# assert_equal(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8),
# price([0, 0, 0, 0, 0,
# 1, 1, 1, 1, 1,
# 2, 2, 2, 2,
# 3, 3, 3, 3, 3,
# 4, 4, 4, 4]))
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment