translation from Ruby to Python of Davis/Hamon Mastermind