Sketch of a solution to a water pouring problem using graph searching technique in Python3
Python
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

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 http://www.cut-the-knot.org/water.shtml, Accessed 08 January 2015

Set up and Solve

Grab the code and change into the directory created:

git clone https://github.com/jamescooke/water-pouring-python.git
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
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 water.game 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`>

Testing

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:

nosetests
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