Skip to content

Commit 4b63bdb

Browse files
authored
Merge pull request exercism#4 from exercism/master
merge in changes from main repo
2 parents e848fbf + 529f4e6 commit 4b63bdb

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

‎exercises/book-store/book_store.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
defcalculate_total(books):
1+
deftotal(basket):
22
pass

‎exercises/book-store/book_store_test.py‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
importunittest
22

3-
frombook_storeimportcalculate_total
3+
frombook_storeimporttotal
44

55

66
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.4.0
77

88
classBookStoreTest(unittest.TestCase):
99
deftest_only_a_single_book(self):
10-
self.assertEqual(calculate_total([1]), 800)
10+
self.assertEqual(total([1]), 800)
1111

1212
deftest_two_of_the_same_book(self):
13-
self.assertEqual(calculate_total([2, 2]), 1600)
13+
self.assertEqual(total([2, 2]), 1600)
1414

1515
deftest_empty_basket(self):
16-
self.assertEqual(calculate_total([]), 0)
16+
self.assertEqual(total([]), 0)
1717

1818
deftest_two_different_books(self):
19-
self.assertEqual(calculate_total([1, 2]), 1520)
19+
self.assertEqual(total([1, 2]), 1520)
2020

2121
deftest_three_different_books(self):
22-
self.assertEqual(calculate_total([1, 2, 3]), 2160)
22+
self.assertEqual(total([1, 2, 3]), 2160)
2323

2424
deftest_four_different_books(self):
25-
self.assertEqual(calculate_total([1, 2, 3, 4]), 2560)
25+
self.assertEqual(total([1, 2, 3, 4]), 2560)
2626

2727
deftest_five_different_books(self):
28-
self.assertEqual(calculate_total([1, 2, 3, 4, 5]), 3000)
28+
self.assertEqual(total([1, 2, 3, 4, 5]), 3000)
2929

3030
deftest_two_groups_of_4_is_cheaper_than_group_of_5_plus_group_of_3(self):
31-
self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 5]), 5120)
31+
self.assertEqual(total([1, 1, 2, 2, 3, 3, 4, 5]), 5120)
3232

3333
deftest_two_groups_of_4_is_cheaper_than_groups_of_5_and_3(self):
34-
self.assertEqual(calculate_total([1, 1, 2, 3, 4, 4, 5, 5]), 5120)
34+
self.assertEqual(total([1, 1, 2, 3, 4, 4, 5, 5]), 5120)
3535

3636
deftest_group_of_4_plus_group_of_2_is_cheaper_than_2_groups_of_3(self):
37-
self.assertEqual(calculate_total([1, 1, 2, 2, 3, 4]), 4080)
37+
self.assertEqual(total([1, 1, 2, 2, 3, 4]), 4080)
3838

3939
deftest_two_each_of_first_4_books_and_1_copy_each_of_rest(self):
40-
self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5]), 5560)
40+
self.assertEqual(total([1, 1, 2, 2, 3, 3, 4, 4, 5]), 5560)
4141

4242
deftest_two_copies_of_each_book(self):
43-
self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]), 6000)
43+
self.assertEqual(total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]), 6000)
4444

4545
deftest_three_copies_of_first_book_and_2_each_of_remaining(self):
4646
self.assertEqual(
47-
calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]), 6800)
47+
total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]), 6800)
4848

4949
deftest_three_each_of_first_2_books_and_2_each_of_remaining_books(self):
5050
self.assertEqual(
51-
calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]), 7520)
51+
total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]), 7520)
5252

5353
deftest_four_groups_of_4_are_cheaper_than_2_groups_each_of_5_and_3(self):
5454
self.assertEqual(
55-
calculate_total([1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]),
55+
total([1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]),
5656
10240)
5757

5858

‎exercises/book-store/example.py‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,48 @@
44
discount= [1.0, 1.0, 0.95, 0.9, 0.8, 0.75]
55

66

7-
defgroupCost(g):
8-
returnlen(g) *discount[len(g)]
7+
defgroup_cost(group):
8+
returnlen(group) *discount[len(group)]
99

1010

1111
classGrouping:
1212
def__init__(self, groups=None):
1313
self.groups= [set()] ifgroupsisNoneelsegroups
1414

1515
deftotal(self):
16-
returnsum(map(groupCost, self.groups)) *BASE_COST
16+
returnsum(map(group_cost, self.groups)) *BASE_COST
1717

1818
defdup(self):
1919
returnGrouping(list(map(set, self.groups)))
2020

21-
defadd_to_valid(self, b):
22-
"""Returns all possible groupings from current grouping adding book b
21+
defadd_to_valid(self, book):
22+
"""Returns all possible groupings from the
23+
current grouping adding book
2324
"""
2425
other=self.dup()
2526
other.groups.sort(key=lambdag: len(g))
2627
results= []
27-
fori, ginenumerate(other.groups):
28-
ifbnoting:
29-
o2=other.dup()
30-
o2.groups[i].add(b)
31-
results.append(o2)
28+
forindex, groupinenumerate(other.groups):
29+
ifbooknotingroup:
30+
other2=other.dup()
31+
other2.groups[index].add(book)
32+
results.append(other2)
3233
ifnotresults:
33-
other.groups.append(set([b]))
34+
other.groups.append(set([book]))
3435
return [other]
3536
returnresults
3637

3738
def__lt__(self, other):
3839
returnself.total() <other.total()
3940

4041

41-
defstep(rs, b):
42-
return [gforrinrsforginr.add_to_valid(b)]
42+
defstep(basket, book):
43+
return [groupforgroupingsinbasket
44+
forgroupingroupings.add_to_valid(book)]
4345

4446

45-
defcalculate_total(books):
46-
iflen(books) ==0:
47+
deftotal(basket):
48+
iflen(basket) ==0:
4749
return0
48-
start=Grouping([{books[0]}])
49-
returnround(min(reduce(step, books[1:], [start])).total())
50+
start=Grouping([{basket[0]}])
51+
returnround(min(reduce(step, basket[1:], [start])).total())

0 commit comments

Comments
(0)