Water Jug Problem

 # def pour_water(juga, jugb):

#     print("%d \t\t %d" % (juga, jugb))
#
#     if jugb == max2:
#         pour_water( juga , 0)
#     elif juga == fill:
#         return
#     elif juga != 0 and jugb == 0:
#         pour_water(0, juga)
#     elif jugb == fill:
#         pour_water(juga, 0)
#     elif juga < max1:
#         pour_water(max1, jugb)
#     elif juga < (max2 - jugb):
#         pour_water(0, (juga + jugb))
#     else:
#         pour_water(juga - (max2 - jugb), (max2 - jugb) + jugb)
#
#
# max1 = int(input("Enter max capacity of Jug A: "))
# max2 = int(input("Enter max capacity of Jug B: "))
# fill = int(input("Enter the quantity to be filled in Jug A : "))
# print("Jug A \t Jug B ")
# pour_water(0, 0)









def pour_water(juga, jugb):
    print("%d \t\t %d" % (juga, jugb))
    if jugb == fill:
        return
    elif jugb == max2:
        pour_water(0, juga)
    elif juga != 0 and jugb == 0:
        pour_water(0, juga)
    elif juga == fill:
        pour_water(juga, 0)
    elif juga < max1:
        pour_water(max1, jugb)
    elif juga < (max2 - jugb):
        pour_water(0, (juga + jugb))
    else:
        pour_water(juga - (max2 - jugb), (max2 - jugb) + jugb)


max1 = int(input("Enter max capacity of Jug A: "))
max2 = int(input("Enter max capacity of Jug B: "))
fill = int(input("Enter the quantity to be filled in Jug B : "))
print("Jug A \t Jug B ")
pour_water(0, 0)




# def water_jug_problem(capacity_jug1, capacity_jug2, target_amount):
#     jug1 = 0
#     jug2 = 0
#
#     while jug1 != target_amount:
#         if jug1 == 0:
#             jug1 = capacity_jug1
#             print(f"Fill the {capacity_jug1}L jug")
#         elif jug1 > 0 and jug2 == 0:
#             pour_amount = min(jug1, capacity_jug2)
#             jug1 -= pour_amount
#             jug2 += pour_amount
#             print(f"Pour {pour_amount}L from {capacity_jug1}L jug to {capacity_jug2}L jug")
#         elif jug2 > 0:
#             pour_amount = min(capacity_jug2 - jug2, jug1)
#             jug1 -= pour_amount
#             jug2 += pour_amount
#             print(f"Pour {pour_amount}L from {capacity_jug1}L jug to {capacity_jug2}L jug")
#
#         if jug1 == target_amount:
#             print("Target amount reached!")
#             break
#
#         if jug2 == capacity_jug2:
#             jug2 = 0
#             print(f"Empty the {capacity_jug2}L jug")
#
#     print("Problem solved!")
#
#
# jug1_capacity = int(input('Enter maximum capacity of jug1'))
# jug2_capacity = int(input('Enter maximum capacity of jug2'))
# target_amount = int(input("Enter target: "))
#
# water_jug_problem(jug1_capacity, jug2_capacity,target_amount)

Comments

Popular posts from this blog

what is Machenical Engineering

Arithmatic operations, factorial of a number, while loop, prime number, etc

PHOTO ( CHINESE LADKA)