Commits

Christoph Schindler committed c0cba5a

12. Dojo: Potter

  • Participants
  • Parent commits a15a5c9

Comments (0)

Files changed (3)

File Potter/potter.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+potter.py
+
+Created by Coding Dojo on 2013-05-21.
+Copyright (c) 2013 __MyCompanyName__. All rights reserved.
+"""
+
+DISCOUNTS = [0, 1, .95, .90, .80, 0.75]
+
+# [0, 0, 1, 1]
+# [0, 1] -> price
+# remove them
+# repeat
+
+def split_basket(basket, max=5):
+    """ [0, 0, 1, 1] -> ([0, 1], remaining)"""
+    remaining = list(basket)
+
+    maximum_unique_set = set(basket)
+    if len(maximum_unique_set) > max:        
+        maximum_unique_set = list(maximum_unique_set)[:max]
+    
+    for book in maximum_unique_set:
+        remaining.remove(book)
+    return maximum_unique_set, remaining
+
+def all_books_are_unique(basket):
+    return (len(set(basket)) == len(basket)) 
+
+def price(basket):
+    basket = sorted(basket)
+    
+    total = 0
+    while not all_books_are_unique(basket):
+        unique_set, basket = split_basket(basket, max=5)
+        total += price(unique_set)
+    
+    books = len(basket)
+    return total + books * 8 * DISCOUNTS[books]    
+    

File Potter/setup.cfg

+[nosy]
+base_path = ./
+glob_pattern = *.py
+extra_paths = setup.cfg
+options = --stop --with-yanc

File Potter/test_potter.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+test_potter.py
+
+Created by Coding Dojo on 2013-05-21.
+Copyright (c) 2013 __MyCompanyName__. All rights reserved.
+"""
+
+from nose.tools import *
+
+import potter
+    
+def test_price_empty_basket():
+    assert_equals(potter.price([]), 0)
+    
+def test_price_single_basket():
+    assert_equals(potter.price([0]), 8)
+    
+def test_price_two_books():
+    assert_equals(potter.price([0,1]), 8*2*0.95)
+    
+def test_price_three_books():
+    assert_equals(potter.price([0, 1, 2]), 8*3*.9)
+    
+def test_price_four_books():
+    assert_equals(potter.price([0, 1, 2, 3]), 8*4*.8)
+
+def test_price_five_books():
+    assert_equals(potter.price([0, 1, 2, 3, 4]), 8*5*.75)
+        
+def test_two_same_books():
+    assert_equals(potter.price([2, 2]), 8*2)
+    
+def test_mix_one():
+    assert_equals(potter.price([0 ,0 ,1]), 8*2*0.95 + 8*1)
+
+def test_order_is_important():
+    assert_equals(potter.price([1, 0, 0]), 8*2*0.95 + 8*1)
+
+def test_two_sets_that_are_the_same():
+    assert_equals(potter.price([0, 0, 1, 1]), 2 * (8*2*0.95))
+
+def test_non_trivial():
+    assert_equals(potter.price([0, 0, 1, 1, 2, 2, 3, 4]), 2*(4*8*.80)   )
+
+def test_non_trivial2_1():
+    assert_equals(potter.price([0,0,1,1,2,3,3,4]), 2*(8*4*.8))
+    
+def test_non_trivial_2times5():
+    assert_equals(potter.price([0,0,1,1,2,2,3,3,4,4]), 2*(8*5*.75))
+    
+def test_non_trivial2():
+    assert_equals(potter.price(
+          [0, 0, 0, 0, 0, 
+           1, 1, 1, 1, 1, 
+           2, 2, 2, 2, 
+           3, 3, 3, 3, 3, 
+           4, 4, 4, 4]), 3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8)  )
+
+
+
+
+
+
+
+
+
+