Advertisement
dimipan80

Exams - Bulls and Cows (on JavaScript)

Jan 2nd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* All we love the “Bulls and Cows” game (http://en.wikipedia.org/wiki/Bulls_and_cows).
  2.  Given a 4-digit secret number and a 4-digit guess number we say that we have b bulls and c cows when we have
  3.  b matching digits on their right positions (bulls) and c matching digits on different positions (cows).
  4.  Given the secret number and the number of bulls and cows your task is to write a program to find
  5.  all matching guess numbers in increasing order.
  6.  The secret number will always consist of exactly 4 digits, each in the range [1…9]!
  7.  The input will consist of exactly 3 lines. At the first line there the secret number will be given.
  8.  At the second line the number of bulls b will be given. At the third line the number of cows c will be given.
  9.  The output should consist of a single line holding all matched guess numbers, given in increasing order,
  10.  separated by single space. */
  11.  
  12. "use strict";
  13.  
  14. function solve(args) {
  15.     var secretStr = args[0];
  16.     var bulls = parseInt(args[1]);
  17.     var cows = parseInt(args[2]);
  18.  
  19.     Array.prototype.contains = function (checkElement) {
  20.         for (var index in this) {
  21.             if (this.hasOwnProperty(index)) {
  22.                 if (this[index] === checkElement) {
  23.                     return true;
  24.                 }
  25.             }
  26.         }
  27.         return false;
  28.     };
  29.  
  30.     var matchedNumbers = [];
  31.     var num, secretNumber, guessNumber;
  32.     for (num = 1111; num < 10000; num += 1) {
  33.         guessNumber = num.toString().split('');
  34.         if (!guessNumber.contains('0')) {
  35.             secretNumber = secretStr.split('');
  36.             var countBulls = getBullsDigits();
  37.             var countCows = getCowsDigits();
  38.             if (countBulls == bulls && countCows == cows) {
  39.                 matchedNumbers.push(num);
  40.             }
  41.         }
  42.     }
  43.  
  44.     if (matchedNumbers.length) {
  45.         console.log(matchedNumbers.join(' '));
  46.     } else {
  47.         console.log('No');
  48.     }
  49.  
  50.     function getBullsDigits() {
  51.         var countBulls = 0;
  52.         for (var i = 0; i < 4; i += 1) {
  53.             if (guessNumber[i] == secretNumber[i]) {
  54.                 guessNumber[i] = '-';
  55.                 secretNumber[i] = '*';
  56.                 countBulls += 1;
  57.             }
  58.         }
  59.  
  60.         return countBulls;
  61.     }
  62.  
  63.     function getCowsDigits() {
  64.         var countCows = 0;
  65.         if (countBulls < 3) {
  66.             for (var i = 0; i < 4; i += 1) {
  67.                 for (var j = 0; j < 4; j += 1) {
  68.                     if (guessNumber[j] == secretNumber[i]) {
  69.                         guessNumber[j] = '-';
  70.                         secretNumber[i] = '*';
  71.                         countCows += 1;
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.  
  77.         return countCows;
  78.     }
  79. }
  80.  
  81. solve(['2228', '2', '1']);
  82. solve(['1234', '3', '0']);
  83. solve(['1234', '3', '1']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement