Last active
August 29, 2015 14:13
-
-
Save anishmashankar/418957e2ca8743a9888e to your computer and use it in GitHub Desktop.
Water Jug problem. An arithmatic approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Written by: Anish Mashankar | |
#Blog: http://techieanish.blogspot.com | |
#####BEGIN##### | |
print 'enter capacity of first jug' | |
x_capacity = input() | |
print 'enter capacity of second jug' | |
y_capacity = input() | |
print 'enter goal capacity' | |
x_goal = input() | |
print 'enter where the goal state is' | |
req_goal = input() | |
x = 0 | |
y = 0 | |
finish = False | |
def fill_x(): | |
print 'fill x' | |
global x, x_capacity, y | |
x = x_capacity | |
print x,y | |
def empty_y(): | |
print 'empty y' | |
global y,x | |
y = 0 | |
print x,y | |
def transfer_x_to_y(): | |
print 'transfer x to y' | |
global x_capacity, y_capacity, x, y | |
fin=0 | |
while fin==0: | |
x = x-1 | |
y = y+1 | |
if (y == y_capacity) or x == 0: | |
fin=1 | |
print x,y | |
while not finish: | |
if x == x_goal or y == x_goal: | |
finish = True | |
elif x == 0: | |
fill_x() | |
continue | |
elif x > 0 and y != y_capacity: | |
transfer_x_to_y() | |
continue | |
elif x > 0 and y == y_capacity: | |
empty_y() | |
continue | |
if req_goal == 1: | |
if x != x_goal: | |
x == x_goal | |
y = 0 | |
if req_goal == 2: | |
if y != x_goal: | |
y = x_goal | |
x = 0 | |
print 'making modifications' | |
print x,y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment