Monday 21 June 2021

Water Jug Puzzle

What is the Water Jug puzzle? There are two Jugs. One jug volume is 4L and another one is 3L. Neither of the Jugs have not marked measurement on jugs. There is a tap, and you can pour jug at any time. Also, you can drop water at any time. if someone ask, they need 2L of water. How do you measure 2L . Also, there is not any additional jugs.


 This kind of question call as an IQ question. I have found similar questions in one of the popular Hollywood film. It is "Die Hard-3.





Solution 1

1.Fist fill 3L jug and then water transfer to 4L jug.
2.Now 4L jug has 3L water only.
3.Do the step 1 activity.
4.Then 3L jug remain 2L water.

Solution 2

1.Fist fill 4L jug and then water transfer to 3L jug.
2.Now 4L jug has 1L water only.
3.Make empty 3L jug.
4.Then 4L jug 1L transfer to 3L Jug.
5.Then fill 4L jug
6.Transfer to 3L jug
7.But can pour 2L only .Because 3L jug has already  1L
8.So 4L jug has 2L now.


Above mentioned two possible solution for this puzzle.


Lets try for python programming approach for this quiz.
Article---> Read
=======================================================================
# This function is used to initialize the
# dictionary elements with a default value.
from collections import defaultdict

# jug1 and jug2 contain the value
# for max capacity in respective jugs
# and aim is the amount of water to be measured.
jug1, jug2, aim = 4, 3, 2

# Initialize dictionary with
# default value as false.
visited = defaultdict(lambda: False)

# Recursive function which prints the
# intermediate steps to reach the final
# solution and return boolean value
# (True if solution is possible, otherwise False).
# amt1 and amt2 are the amount of water present
# in both jugs at a certain point of time.
def waterJugSolver(amt1, amt2):

# Checks for our goal and
# returns true if achieved.
if (amt1 == aim and amt2 == 0) or (amt2 == aim and amt1 == 0):
print(amt1, amt2)
return True
# Checks if we have already visited the
# combination or not. If not, then it proceeds further.
if visited[(amt1, amt2)] == False:
print(amt1, amt2)
# Changes the boolean value of
# the combination as it is visited.
visited[(amt1, amt2)] = True
# Check for all the 6 possibilities and
# see if a solution is found in any one of them.
return (waterJugSolver(0, amt2) or
waterJugSolver(amt1, 0) or
waterJugSolver(jug1, amt2) or
waterJugSolver(amt1, jug2) or
waterJugSolver(amt1 + min(amt2, (jug1-amt1)),
amt2 - min(amt2, (jug1-amt1))) or
waterJugSolver(amt1 - min(amt1, (jug2-amt2)),
amt2 + min(amt1, (jug2-amt2))))
# Return False if the combination is
# already visited to avoid repetition otherwise
# recursion will enter an infinite loop.
else:
return False

print("Steps: ")

# Call the function and pass the
# initial amount of water present in both jugs.
waterJugSolver(0, 0)

=========================================================================

Above code has written in python programming language. It is used recursion method to  solve this quiz. Recursion method is used when know base value. In this quiz know value should equal to (2,0) or (0,2).So this program run until find base value and once find base value it is going to find before step. i.e (0,0) status.










No comments:

Post a Comment

AI Winter !!! Past Today Tomorrow !!!

  What is AI ? According to John McCarthy ( One of the founder  of  area of AI) " It is the science and engineering of making intellige...