Reviewed katas for software excellence

A farmer has a forty pound stone that he uses to weigh food for his animals on a scale. One day, he loans the stone to his neighbor. A few days later, the neighbor returns the stone and apologizes to the farmer because his stone is now broken in four pieces. The farmer responds “Please don’t apologize, you have actually made my life easier because with these four pieces, I can now measure any weight between one and forty pounds.

The question is: how much do these four individual pieces weigh?

I’m adding a few clarifications, which are not necessarily what the Car Talk guys meant (I don’t have the solution and I haven’t looked at any discussion on the subject):

- The four weights are integers.
- The weights we can measure between one and forty pounds are in one pound increment.
- They are measured in one session (otherwise, you could measure forty pounds with a one pound stone by performing forty measurements).

If there is no solution (the farmer might be bad at math), show the four weights that allow to measure the most weights between one and forty pounds.

When I first read the case, I thought was not only how to solve the kata but also how to optimize the algorithm to solve the problem efficiently. As usual, my approach to solve a problem starts in a paper. I draw an example of a set of possible weights. Then I play with then to understand the logic behind the measurement operation. After I while, I realize that the possible combinations in the positions and use (or not) of the stones could be modeled like -1 (if it is on the left), 0 (is it is not used) or +1 (if it is on the right). Now I have a way to check if a particular set of weights can generate what quantity of measures. The other part of the problem is the generation of the possible set of weights. The restrictions are that all of the weights have to sum 40 and they are all integers. Using this information I could improve the exploration of the combinations to make it more efficient. This part was challenging, because even when I could found some rules to reduce significantly the exploration, still I believe that more optimization can be done. It was a great kata. Very helpful to think in how to optimize of combinatorial explosion exploration.

Updated on Jan 30, 2014
by
oscaralvaro**162**

This kata is a good exercise for DRY and readable practice. If we want allow the program to change for different pounds and number of stones, we can't just write 4 layers of loop in our program. To solve this problem, I tried to use recursive function and create trinary system. And it will make the program faster if we eliminate duplicate combinations.

Updated on May 05, 2013 by CarinaZheng

This kata is an excellent way to test my problem solving skills. Before straightly opening Eclipse to write code, I decide to wrote some example on paper to see if I can find come regularity. Although I successfully found a combination and weighed 1 to 40, my approach was not systematical. Through the process of organizing my thoughts and transforming it into Java code, I learned how to think more systematically, which is a critical skill for me and every software developer. I am so glad that I did this kata.

Updated on Feb 12, 2014
by
Chih-Shao Lee**17**

The most interesting part of the problem is to think through it and decompose it to loosely coupled functions. The problem itself is not very difficult. Everything except my code is a little verbose.

Updated on May 05, 2013 by evivrus

This is a good kata for problem solving and requires thinking about the algorithm before starting to write tests /code. Having worked on the combinatorics problems before helped. This is not the perfect kata for TDD. I couldn't come up with a lot of tests.

Updated on May 05, 2013 by Himani

It is a good challenge for trying different solutions. While finish the one the code required, it is interested to spend more time and generalize the algorithm so that it can fit to different situations, for example, different weights.

Updated on May 05, 2013
by
lydian**79**

It was a nice and challenging problem. Trying different numbers in a notepad document to see what a potential solution could be. I'm not sure whether there's a more efficient way of solving the problem than having many for loops. There's a nice way to figure out whether you can weight an item of a certain weight. It involves iterating each weight through the left side, the right side, and a nonexistent side.

Posted on Mar 21, 2012 by Borys

This is a wonderful exercise for brushing up your problem solving skills. I figured out the approach before jumping to solve the problem. Most of my time wasted in searching for small coding issues.

Posted on Mar 21, 2012 by charuaggarwal

A very nice kata to understand the mathematical and digital concepts in algorithms. It was tempting to go for a brute force, but believe me, finding a solution is much more fun. Kata becomes more tricky when coming up with test cases, but again, think of test cases as a new problem.

Posted on May 06, 2013
by
singhprabhjot**54**

This is a great kata to help one strengthen their ability to approach problem solving. For me, approaching this kata as a real-world problem proved to be helpful. Try to understand how a scale works and work out some examples of how you might arrive at different weights. You may not need all four stones to arrive at a certain weight - or you may need to use a combination of stones on either side of the scale (hint: *1 vs *-1).

Posted on Feb 01, 2014
by
William McDonald**6**

This is a very good kata to understand real problems and apply possible solutions. It was my first attempt on a combinations problem. I was amazed at the simplicity of problem and complexity of the solution. Though a ternary scale - bit approach would have been easier and straightforward answer, I chose to do it with code and apply all possible combinations(brute force) . On it, I optimized by somehow reducing the number of times a loop has to run. A good kata to simulate brain into thinking in all possible directions.

Posted on Feb 09, 2014
by
Ira**20**

It's interesting, and has a couple of tricks you have to realize. It is possible to get the right answer without actually getting the right answer, that is, a solution that is general.

Posted on Mar 21, 2012 by Isak Sky