about | help | code help+videos | done | prefs |
This is Car Talk Puzzler #201143: RAY: A farmer had a 40-pound stone that he could use to weight 40 pounds of feed, hay, or whatever else he wanted to sell from his farm. He had a balance scale, and he put the stone on one side and piled the other side with feed or hay, and when it balanced, he knew he had 40 pounds. One day, a neighbor borrows the stone, but he had to apologize when he returned it, broken into four pieces. The farmer who owned the stone later told the neighbor that he actually had done him a favor. The pieces of the broken stone could now be used to weigh any item, assuming those items were in one-pound increments, from one pound to 40. The question is, what were the weights of the four individual stones? And if you want to weigh one pound, six pounds, 11 pounds, 22 pounds, 39 pounds -- how would you use the stones, the thing you are weighing, and the balance scale? And here's the Hint: how would you weigh two pounds? NOTE: To turn this from a Puzzler to a CodingBat, your task is to write the function stone_temple_farmers(pieces, w), which takes a list of four pieces and a weight w. If it is possible to balance an item of weight w with the pieces, return a list of the pieces you use, with a minus sign to mean they go on the same side of the balance as the item. For example, atone_temple_farmers([1,2,10,27], 9) should return [1, -2, 10]; note that this sums to 9. It means that you should put the item to be weighed and the 2-pound stone on one side of the balance and the 1 and 10-pound stones on the other side; if they balance, the item weighs 9 pounds. If there is no such solution, return None. stone_temple_farmers([1, 2, 10, 27], 9) → [1, -2, 10] stone_temple_farmers([2, 4, 6, 8], 18) → [4, 6, 8] stone_temple_farmers([2, 4, 6, 8], 13) → None ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 326 Post-solution available
Copyright Nick Parlante 2017 - privacy