id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

transfer_water_search


This was the CarTalk Puzzler called "Two Glasses of Water Puzzler": RAY: This Puzzler was sent in by Frank Juskolka. Frank writes, "You have a four-ounce glass and a nine-ounce glass. You have an endless supply of water. You can fill or dump either glass. TOM: Do you have a plastic bag in your pocket? No. Okay. Just checking. RAY: It turns out, you can measure six ounces of water using these two glasses. The question is: 1. How many steps are involved; and B. What's the fewest number of steps in which you can measure six ounces? In this second part, we will solve the problem, not only for 4- and 9-ounce glasses with a goal of 6 ounces, but for general numbers. You will define the function transfer_water_search(A, B, goal) that returns a path of states reaching from the start state of (0, 0) -- meaning 0 in glass A and 0 in glass B -- to the goal state -- meaning either A or B has 'goal' ounces. The specific puzzler would be represented by transfer_water_search(4, 9, 6). The result should be a list (known as a path) of states, where each state in the path is a (a, b) pair of water-levels in glasses (A, B). The first state of the path will always be (0, 0), and the last state will have at least one of the (a, b) values equal to the goal. If there is no path, the function should return None instead of a path.


...Save, Compile, Run (ctrl-enter)

def transfer_water_search(A, B, goal):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 901 Post-solution available

Copyright Nick Parlante 2017 - privacy