Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Water Pouring Problem

A sketch of a "water pouring problem" in Python3.

This README contains basic setup and test instructions. Full blog post is available with more information on features of the code and how some elements were put together.

The Problem

There are three glasses on the table - 3, 5, and 8 oz. The first two are empty, the last contains 8 oz of water. By pouring water from one glass to another make at least one of them contain exactly 4 oz of water.

Source: A. Bogomolny, 3 Glasses Puzzle from Interactive Mathematics Miscellany and Puzzles, Accessed 08 January 2015

Set up and Solve

Grab the code and change into the directory created:

git clone
cd water-pouring-python

I like to make a virtualenv. This sketch is Python3, so specify that when making the env.

virtualenv env --python=python3
source env/bin/activate

I also like iPython, so install that and run it:

pip install ipython

Now the 'water' module can be used to solve the problem with the 3, 5 and 8 oz cups set up as described above.

In [1]: from import Game

In [2]: game = Game(sizes=[(3, 0), (5, 0), (8, 8)])

In [3]: game.is_solvable()
[<Cup 0/3>, <Cup 0/5>, <Cup 8/8>]
[<Cup 3/3>, <Cup 0/5>, <Cup 5/8>]
[<Cup 0/3>, <Cup 3/5>, <Cup 5/8>]
[<Cup 3/3>, <Cup 3/5>, <Cup 2/8>]
[<Cup 1/3>, <Cup 5/5>, <Cup 2/8>]
[<Cup 1/3>, <Cup 0/5>, <Cup 7/8>]
[<Cup 0/3>, <Cup 1/5>, <Cup 7/8>]
[<Cup 3/3>, <Cup 1/5>, <Cup 4/8>]
Out[3]: True

This shows that the problem described above is solvable and a route to the solution. When printing the state of the Game, each Cup is printed with its contents and capacity shown as:

<Cup `contents`/`capacity`>


There are tests in the code as a side effect of writing it in a generally TDD manner.

Tests require nose, but flake8 is also helpful. Install both with the test requirements file:

pip install -r test-requirements.txt

Now run each in turn:

flake8 water

... and both should pass. Happy days.

License and contribution

Please open any discussion using GitHub Issues. Contributions and questions very welcome.

Licensed under GNU GPL v2.0


Sketch of a solution to a water pouring problem using graph searching technique in Python3







No releases published


No packages published