title: bulls and cows game -- programming algorithm(python)
body: <p><strong>This is a simulation of the game <a href="http://en.wikipedia.org/wiki/Bulls_and_cows" rel="nofollow noreferrer">Cows and Bulls</a> with three digit numbers</strong></p>
  <p>I am trying to get the number of cows and bulls between two numbers. One of which is generated by the computer and the other is guessed by the user. I have parsed the two numbers I have so that now I have two lists with three elements each and each element is one of the digits in the number. So:</p>
  <p>237 will give the list <code>[2,3,7]</code>. And I make sure that the relative indices are maintained.the general pattern is:<code>(hundreds, tens, units)</code>.</p>
  <p>And these two lists are stored in the two lists: <code>machine</code> and <code>person</code>.</p>
  <h1>ALGORITHM 1</h1>
  <p>So, I wrote the following code, The most intuitive algorithm:</p>
  <p><code>cows</code> and <code>bulls</code> are initialized to 0 before the start of this loop.</p>
  <pre><code>for x in person:
      if x in machine:
          if machine.index(x) == person.index(x):
              bulls += 1
              print x,' in correct place'
          else:
              print x,' in wrong place'
              cows += 1
  </code></pre>
  <p>And I started testing this with different type of numbers guessed by the computer.</p>
  <p>Quite randomly, I decided on 277. And I guessed 447. Here, I got the <strong>first</strong> clue that this algorithm may not work. I got 1 cow and 0 bulls. Whereas I should have got 1 bull and 1 cow.</p>
  <p>This is a table of outputs with the first algorithm:</p>
  <pre><code>Guess        Output            Expected Output
  
  447     0 bull, 1 cow          1 bull, 0 cow 
  477     2 bulls, 0 cows        2 bulls, 0 cows
  777     0 bulls, 3 cows        2 bulls, 0 cows
  </code></pre>
  <p>So obviously this algorithm was not working when there are repeated digits in the number randomly selected by the computer.</p>
  <p>I tried to understand why these errors are taking place, But I could not. I have tried a lot but I just could not see any mistake in the algorithm(probably because I wrote it!)</p>
  <h1>ALGORITHM 2</h1>
  <p>On thinking about this for a few days I tried this:</p>
  <p><code>cows</code> and <code>bulls</code> are initialized to 0 before the start of this loop.</p>
  <pre><code>for x in range(3):
      for y in range(3):
              if x == y and machine[x] == person[y]:
                  bulls += 1
              if not (x == y) and machine[x] == person[y]:                   
                  cows += 1
  </code></pre>
  <p>I was more hopeful about this one. But when I tested this, this is what I got:</p>
  <pre><code>Guess        Output            Expected Output
  
  447     1 bull, 1 cow          1 bull, 0 cow 
  477     2 bulls, 2 cows        2 bulls, 0 cows
  777     2 bulls, 4 cows        2 bulls, 0 cows
  </code></pre>
  <p>The mistake I am making is quite clear here, I understood that the numbers were being counted again and again.</p>
  <p>i.e.: 277 versus 477</p>
  <p>When you count for bulls then the 2 bulls come up and thats alright. But when you count for cows:</p>
  <ol>
  <li>the 7 in 277 at units place is matched with the 7 in 477 in tens place and thus a cow is generated.</li>
  <li>the 7 in 277 at tens place is matched with the 7 in 477 in units place and thus a cow is generated.'</li>
  </ol>
  <p>Here the matching is exactly right as I have written the code as per that. But this is not what I want. And I have no idea whatsoever on what to do after this.</p>
  <h2>Furthermore...</h2>
  <p>I would like to stress that both the algorithms work perfectly, if there are no repeated digits in the number selected by the computer.</p>