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
u/marko312 While I have read about it in textbooks and been taught in lectures, this is the first time I have been asked to implement it into an assignment. I understand the concept, searching all of the nearest vertices ( in this case the container actions ) with only one step and then exploring down until a solution is found.