summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-09-23 21:35:14 +0200
committerDimitri Sokolyuk <demon@dim13.org>2018-09-23 21:35:14 +0200
commitf2e7ca36de4fe799a002250d5a35e44f69532c39 (patch)
tree03c9100353b2e08dc34f1452162f373b3f6cd374
parentfd7f1d0574b489b5c8bb72093f7982fca924c042 (diff)
add book-store
-rw-r--r--go/book-store/.solution.json1
-rw-r--r--go/book-store/README.md108
-rw-r--r--go/book-store/book_store_test.go24
-rw-r--r--go/book-store/cases_test.go87
4 files changed, 220 insertions, 0 deletions
diff --git a/go/book-store/.solution.json b/go/book-store/.solution.json
new file mode 100644
index 0000000..4ced633
--- /dev/null
+++ b/go/book-store/.solution.json
@@ -0,0 +1 @@
+{"track":"go","exercise":"book-store","id":"01052411de484c9d8cff5e300843f581","url":"https://exercism.io/my/solutions/01052411de484c9d8cff5e300843f581","handle":"dim13","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/go/book-store/README.md b/go/book-store/README.md
new file mode 100644
index 0000000..ee6ea70
--- /dev/null
+++ b/go/book-store/README.md
@@ -0,0 +1,108 @@
+# Book Store
+
+To try and encourage more sales of different books from a popular 5 book
+series, a bookshop has decided to offer discounts on multiple book purchases.
+
+One copy of any of the five books costs $8.
+
+If, however, you buy two different books, you get a 5%
+discount on those two books.
+
+If you buy 3 different books, you get a 10% discount.
+
+If you buy 4 different books, you get a 20% discount.
+
+If you buy all 5, you get a 25% discount.
+
+Note: that if you buy four books, of which 3 are
+different titles, you get a 10% discount on the 3 that
+form part of a set, but the fourth book still costs $8.
+
+Your mission is to write a piece of code to calculate the
+price of any conceivable shopping basket (containing only
+books of the same series), giving as big a discount as
+possible.
+
+For example, how much does this basket of books cost?
+
+- 2 copies of the first book
+- 2 copies of the second book
+- 2 copies of the third book
+- 1 copy of the fourth book
+- 1 copy of the fifth book
+
+One way of grouping these 8 books is:
+
+- 1 group of 5 --> 25% discount (1st,2nd,3rd,4th,5th)
+- +1 group of 3 --> 10% discount (1st,2nd,3rd)
+
+This would give a total of:
+
+- 5 books at a 25% discount
+- +3 books at a 10% discount
+
+Resulting in:
+
+- 5 x (8 - 2.00) == 5 x 6.00 == $30.00
+- +3 x (8 - 0.80) == 3 x 7.20 == $21.60
+
+For a total of $51.60
+
+However, a different way to group these 8 books is:
+
+- 1 group of 4 books --> 20% discount (1st,2nd,3rd,4th)
+- +1 group of 4 books --> 20% discount (1st,2nd,3rd,5th)
+
+This would give a total of:
+
+- 4 books at a 20% discount
+- +4 books at a 20% discount
+
+Resulting in:
+
+- 4 x (8 - 1.60) == 4 x 6.40 == $25.60
+- +4 x (8 - 1.60) == 4 x 6.40 == $25.60
+
+For a total of $51.20
+
+And $51.20 is the price with the biggest discount.
+
+## Implementation
+
+Define a single Go func, Cost, which calculates the cost
+for a given list of books based on the defined discounts.
+
+Use the following signature for func Cost:
+
+```
+func Cost(books []int) int
+```
+Cost will return the total cost (after discounts) in cents.
+For example, for a single book, the cost is 800 cents, which equals $8.00.
+Only integer calculations are necessary for this exercise.
+
+
+
+## Running the tests
+
+To run the tests run the command `go test` from within the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
+flags:
+
+ go test -v --bench . --benchmem
+
+Keep in mind that each reviewer will run benchmarks on a different machine, with
+different specs, so the results from these benchmark tests may vary.
+
+## Further information
+
+For more detailed information about the Go track, including how to get help if
+you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources).
+
+## Source
+
+Inspired by the harry potter kata from Cyber-Dojo. [http://cyber-dojo.org](http://cyber-dojo.org)
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/go/book-store/book_store_test.go b/go/book-store/book_store_test.go
new file mode 100644
index 0000000..4309b65
--- /dev/null
+++ b/go/book-store/book_store_test.go
@@ -0,0 +1,24 @@
+package bookstore
+
+import (
+ "testing"
+)
+
+func TestCost(t *testing.T) {
+ for _, testCase := range testCases {
+ cost := Cost(testCase.basket)
+ if testCase.expected != cost {
+ t.Fatalf("FAIL: %s\n\tCost(%v) expected %v, got %v",
+ testCase.description, testCase.basket, testCase.expected, cost)
+ }
+ t.Logf("PASS: %s", testCase.description)
+ }
+}
+
+func BenchmarkCost(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, testCase := range testCases {
+ Cost(testCase.basket)
+ }
+ }
+}
diff --git a/go/book-store/cases_test.go b/go/book-store/cases_test.go
new file mode 100644
index 0000000..b42d263
--- /dev/null
+++ b/go/book-store/cases_test.go
@@ -0,0 +1,87 @@
+package bookstore
+
+// Source: exercism/problem-specifications
+// Commit: 33c6b60 book-store: Add test case to thwart list-order greedy solutions (#1297)
+// Problem Specifications Version: 1.4.0
+
+var testCases = []struct {
+ description string
+ basket []int
+ expected int
+}{
+ {
+ description: "Only a single book",
+ basket: []int{1},
+ expected: 800,
+ },
+ {
+ description: "Two of the same book",
+ basket: []int{2, 2},
+ expected: 1600,
+ },
+ {
+ description: "Empty basket",
+ basket: []int{},
+ expected: 0,
+ },
+ {
+ description: "Two different books",
+ basket: []int{1, 2},
+ expected: 1520,
+ },
+ {
+ description: "Three different books",
+ basket: []int{1, 2, 3},
+ expected: 2160,
+ },
+ {
+ description: "Four different books",
+ basket: []int{1, 2, 3, 4},
+ expected: 2560,
+ },
+ {
+ description: "Five different books",
+ basket: []int{1, 2, 3, 4, 5},
+ expected: 3000,
+ },
+ {
+ description: "Two groups of four is cheaper than group of five plus group of three",
+ basket: []int{1, 1, 2, 2, 3, 3, 4, 5},
+ expected: 5120,
+ },
+ {
+ description: "Two groups of four is cheaper than groups of five and three",
+ basket: []int{1, 1, 2, 3, 4, 4, 5, 5},
+ expected: 5120,
+ },
+ {
+ description: "Group of four plus group of two is cheaper than two groups of three",
+ basket: []int{1, 1, 2, 2, 3, 4},
+ expected: 4080,
+ },
+ {
+ description: "Two each of first 4 books and 1 copy each of rest",
+ basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5},
+ expected: 5560,
+ },
+ {
+ description: "Two copies of each book",
+ basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5},
+ expected: 6000,
+ },
+ {
+ description: "Three copies of first book and 2 each of remaining",
+ basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1},
+ expected: 6800,
+ },
+ {
+ description: "Three each of first 2 books and 2 each of remaining books",
+ basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2},
+ expected: 7520,
+ },
+ {
+ description: "Four groups of four are cheaper than two groups each of five and three",
+ basket: []int{1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5},
+ expected: 10240,
+ },
+}