David Gries’ Coffee Can Problem
October 22, 2013
David Gries described today’s exercise in his 1981 book The Science of Programming; I learned it from Jon Bentley’s 2000 book Programming Pearls, second edition.
You are initially given a coffee can that contains some black beans and some white beans and a large pile of “extra” black beans. You then repeat the following process until there is a single bean left in the can.
Randomly select two beans from the can. If they are the same color, throw them both out and insert an extra black bean. If they are different colors, return the white bean to the can and throw out the black.
Prove that the process terminates. What can you say about the color of the final remaining bean as a function of the numbers of black and white beans originally in the can?
Your task is to answer the two questions asked above, then write a program that simulates the coffee can problem. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
A solution in Racket and a proof by induction in “engrish”:
I went a slightly different route, both with language (C++11) and simulation
technique. As for the problems, the second one tripped me up. Moreover, it
reminded me of a similar problem in Hofstadter’s “Godel, Escher, Bach” that
also stumped me a bit. You’d think I would have learned my lesson.
Solution in ruby
WHITE = ‘W’
BLACK = ‘B’
class CoffeCan
def initialize
@beans = []
end
def add_bean(bean)
@beans.push bean
end
def fill_randomly(size)
amount = rand size
(0..amount).each do
if (rand 2) == 0
@beans.push WHITE
else
@beans.push BLACK
end
end
end
def remove_two_beans
[@beans.delete_at(rand(@beans.size)), @beans.delete_at(rand(@beans.size))]
end
def is_possible
(@beans.size > 1) || false
end
def to_s
@beans.join ‘ ‘
end
end
class SimulateCan
def run (size=10000)
can = CoffeCan.new
can.fill_randomly size
puts can.to_s
while can.is_possible
beans = can.remove_two_beans
if beans[0] == beans[1]
can.add_bean BLACK
else
can.add_bean WHITE
end
puts can.to_s
end
end
end
sim = SimulateCan.new
sim.run
First, clearly this problem calls for a coffee-associated programming language. As I find that I need to learn some Coffeescript anyway, here’s my first ever Coffeescript program.
Second, while the randomness specification is a red herring, let’s at least make it more realistic: my replacement step shuffles some number of the beans on top, not the whole can. To do this, I represent the can as an array of distinct beans. I also take care to return the same white bean to the can (another red herring in the specification).
I omit the Fisher-Yates shuffle code that I found on the web.
replace = (can) ->
[a, b] = [can.shift(), can.shift()]
if a[0] is b[0] then can.unshift(['black'])
else can.unshift(if a[0] is 'white' then a else b)
top = Math.min(some, can.length)
can[0...top] = shuffle(can[0...top])
can
process = (can) ->
while can.length > 1
replace(can)
To make and shuffle an initial can, run the process on it with the number of shuffled top beans specified, and print the final can, I had the following. It appears to print a can with a white bean in it when there is an odd initial number of white beans.
some = 8
whites = 39
beans = 41
can = ([if k < whites then 'white' else 'black'] for k in [0...beans])
shuffle(can)
process(can)
console.log(can)
This was fun.
Is this an acceptable simulation of the problem in Java?
Mark, I think it would be better if the simulation could be run with different numbers of beans and different proportions of white and black. Your program generates only cans (of 100000, quite a lot) with roughly equal proportions of the colours.
It would also be appropriate to print the initial numbers of white and black beans together with the colour of the final bean. Or let the user supply the initial numbers.
It’s unrealistic that every bean is equally likely to be picked but the framers of the problem didn’t have such realism in mind. The original framers didn’t even have simulation in mind. It was about reasoning.
And yes, Java is an appropriate language for coffee-can problems.