CodeKata by Dave Thomas
A fantastic idea! Most of us don't practice enough because we have work to do. Usually dealing with specific problems that need to be solved as quickly as possible. Not that it doesn't require creativity, but it certainly not as much fun as stretching your boundaries by thinking about the best possible solution to even a simple problem without limits.
My Solutions
I believe that it is my responsibility to keep - not only my body, but my mind fit. I thought, why not share the fun with you along the way... On the following pages I will describe my solutions to Dave's exercises just to share it with the public. Newbie programmers or - maybe even experienced colleagues, might find it interesting.
First Kata: Supermarket Pricing
The problem: This kata is based on a commonly known issue with the pricing in the supermarkets. Believe it or not most of the ordinary people in the UK would not know how to deal with the Maths behind this problem. However, we are programmers and their happiness of saving a few quid depends on our success.
- three for £1 (so what's the price if I buy 4, or 5?)
- £1.99/pound (so what does a kilo cost?)
- buy two, get one free (so does the third item have a price?)
The questions we are seeking answers for are
- does fractional money exist?
- when (if ever) does rounding take place?
- how do you keep an audit trail of pricing decisions?
- are costs and prices the same class of thing?
- if a shelf of 100 cans is priced using "buy two, get one free", how do you value the stock?
My thoughts:
1.) 3 for £1 offers
This could be done by calling a method if the offer is enabled for the product. Let's call this method MultiBuyOffer.
If the MultiBuyOffer method is called, first it calculates the discount value (DV) by subtracting the total price of the offer (£1) from the equivalent of the (normal) retail item price (NRP) - for example £0.39, multiplied by the number of products in the offer (NPO) - in this case 3. Then it counts the total quantity of the particular product in the basket (TQP) - let's do 5, divides it by the NPO. The integer fraction of this value shows how many times the discount needs to be applied to the product. Let's call this discount multiplicative (DM), multiply that with the DV and get the total discount value (TDV). The total discounted price (TDP) is calculated by multiplying the NRP by the TQP and subtracting the TDV.
Let's do the maths.
- TDV = (3 * 0.39) - 1 = 0.17
- 5 / 3 = 1.66... (dots mean the number of 6's is endless)
- DM = 1 (integer fraction from above)
- TDV = 1 * 0.17
- TDP = (5 * 0.39) - 0.17 = 1.78
Let's verify.
£1 + (2 * 39p) = £1.78
2. pound to kilogram conversion
I'm not sure if the question is referring to a simple conversion, but here we go.
1 kilogram = 2.2046 lbs
Therefore the price for a kilo is 2.2046 * £1.99 = 4.387154. This needs to be rounded to £4.39.
3. buy two get one free
I'm not a retail expert but I assume every product acquired has a price so there should not be anything given away for free. Therefore the price of the free product must be spread across the number of items bought.
I think this is very similar to the 3 for £1 pound example.
If you buy 2 of something, a third one will be added and a calculated discount will be applied. So it does have a price.
As for the questions:
- The example above shows that we use only the integer value of the single division we use in the process in the first exercise. In the second one, we have to round the value to get a price with 2 decimals.
- It is important to keep an audit trail by tracking the modifiers and modification dates for the product details.
- I'd say cost and prices are in the same class since their values are constantly related.
- Technically, products on buy 1 get 1 free could be bought either way, using or not using the offer so valuation isn't easy. However, I would probably value the stock at full price since the discount will only be applied at the time of purchase, if applicable. The total value of discounted items sold can be reflected in the revenue figures if required.
Interested in finding a solution yourself?
Check out Dave's CodeKata Blog at http://codekata.pragprog.com/.
Back
|