Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Error in Exercise "Book Store" #181

Closed
cmccandless opened this issue Jan 24, 2017 · 3 comments
Closed

Test Error in Exercise "Book Store" #181

cmccandless opened this issue Jan 24, 2017 · 3 comments

Comments

@cmccandless
Copy link
Contributor

The test "Basket_with_eight_books" tests for a sum value of "51.20", when the correct sum should be "51.60".

E.G: books=[1,1,2,2,3,3,4,5]
groups=[count(1,2,3,4,5),count(1,2,3)]=[5,3]
discount[5]=.75
discount[3]=.9
groups8=[40,24]
groups
8discount=[40.75,24*.9]=[30,21.6]
total=30+21.6=51.60

This particular example also happens to be contained in the Readme for the exercise, but the arithmetic was done correctly there.

@robkeim
Copy link
Contributor

robkeim commented Jan 24, 2017

@cmccandless are you talking about this readme?

If so, the Readme calls out two different groupings for the eights books, and you need to choose the cheaper one. If you're getting 51.60 it's probably because you're using a greedy algorithm and the greedy algorithm doesn't always work in this problem :)

@robkeim
Copy link
Contributor

robkeim commented Jan 24, 2017

Copying the relevant part of the readme for your convenience:

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.

@cmccandless
Copy link
Contributor Author

Ah, that's what I get for trying to code before coffee; I misunderstood that part of the readme. My mistake.

ErikSchierboom pushed a commit to exercism/problem-specifications that referenced this issue Feb 13, 2017
* book-store: Explain importance of 4+4 vs 5+3 case

You cannot greedily add books to a single group in this case. You have
to try the possibilities and see that two groups of four is a cheaper
grouping than a group of five and a group of three.

If this is not made clear, people may make assumptions that greedy is
the correct way to go, as in:
exercism/csharp#181

* book-store: Contrast 4+4 < 5+3 case with a 4+2 < 3+3 case

In the former, you MUST NOT be greedy.
In the latter, you MUST be greedy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants