r/programminghelp • u/Ccooperdavis • Dec 11 '20
Python Python Programming Assignment: Water Jug Solution with search graph
Hello everyone, I have been assigned to solve the water jug problem in python using graphs, and a searching algorithm breadth-first search. If you are unfamiliar with eh water jug problem, you are given 2 water containers with 2 different maximum capacities and your goal is to get a certain amount of water in either of the two containers. You only have 6 choices, fill container A, fill container B, pour A into B, pour B into A, empty A, empty B. Using these operators you are supposed to find the shortest path to the goal. I am given some skeleton code to start (which I will post below) and really I just need help getting some sort of starting place. I have been looking for a while on how this is supposed to work and I can't get a decent start on it. If you have anything to offer I would appreciate anything, please let me know if I need to clarify something.
Thank you.
import math
import unittest
def findWaterContainerPath(a, b, c):
""" DOCUMENTATION FOR THIS FUNCTION """
starting_state = (0, 0)
final_path = list()
final_path.append(starting_state)
""" THIS IS WHERE THE REAL WORK FOR THIS ASSIGNMENT WILL BE """
return final_path
""" ADD ANY OTHER (HELPER) FUNCTIONS THAT ARE NEEDED HERE """
class TestWaterContainerGraphSearch(unittest.TestCase):
def testFindWaterContainerPath(self):
""" INSERT DESCRIPTION OF WHAT THIS TEST IS CHECKING """
""" IMPLEMENT YOUR FIRST TEST HERE """
pass
""" ADD MORE TESTS TO CHECK YOUR FUNCTIONS WORKS FOR OTHER VALUES """
def main():
capacity_a = input("Enter the capacity of container A: ")
capacity_b = input("Enter the capacity of container B: ")
goal_amount = input("Enter the goal quantity: ")
# ADD SOME TYPE/VALUE CHECKING FOR THE INPUTS (OR INSIDE YOUR FUNCTION)
if int(goal_amount) % math.gcd(int(capacity_a), int(capacity_b)) == 0:
path = findWaterContainerPath(int(capacity_a), int(capacity_b), int(goal_amount))
else:
print("No solution for containers with these sizes and with this final goal amount")
print(path)
# unittest_main() - run all of TestWaterContainerGraphSearch's methods (i.e. test cases)
def unittest_main():
unittest.main()
# evaluates to true if run as standalone program
if __name__ == '__main__':
main()
unittest_main()
1
u/Ccooperdavis Dec 11 '20
Okay, this is starting to click and I'm getting an idea of what I want to do. I thought that there would have to be a helper method dedicated to checking if a state has been visited yet. I have looked at a couple of ways how to do that and some of it isn't making sense.
I have been looking at this example on GitHub recently and the get_index function, as well as been_there, are not making sense. I don't understand the power function and how he uses that to get the index of a node. Maybe you have a better idea of how to do that. If I can figure out how to mark steps as visited then I think I am in a good spot to getting a decent start on this.
https://github.com/khasmamad99/Water-Jug-Problem-Solver/blob/master/Water-Jug-Problem-Solver.py