• Articles
    • Popular
  • Katas
    • Get Started
  • Subscribe
  • Github Sign in with Github

Craftsmanship

Reviewed katas for software excellence
Shore_198

←Older posts
Newer posts→
New Kata

Weighing with stones Kata

Updated on May 05, 2013 by Isak Sky
Rating:   

Challenge Level: Medium
Category: Off-the-Shelf Algorithms

 

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.
9 Reviews

9 Reviews

Upvote 1 Downvote
Rating:    Programming Language: Java

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

Upvote 1 Downvote

Good starter kata

Rating:    Challenge Level: Medium Programming Language: Java

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 May 05, 2013 by oscaralvaro162

Upvote 1 Downvote
Rating:    Programming Language: C++

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

Upvote 1 Downvote
Rating:    Programming Language: Javascript

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

Upvote 1 Downvote
Rating:    Challenge Level: High Programming Language: python

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 lydian79

Upvote 0 Downvote
Rating:    Programming Language: Java

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

Upvote 0 Downvote
Rating:    Programming Language: Java

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

Upvote 0 Downvote
Rating:    Programming Language: Clojure

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

Upvote 0 Downvote

Think before you dive

Rating:    Challenge Level: Medium Programming Language: Java

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 singhprabhjot54

Please sign in to review.

Sign in with Github
Carnegie Mellon University - Silicon Valley
Rss-news
Powered by Hibiscus.