Bulls and Cows The problem description is not ideal. Both bulls and cows are defined for "pair", which is a pair of identical digit from secret and guess. For example, there is a pair of '1' that is not matching in "1234" and 0111″, hence the result is 0A1B. Whilst in "1122" and "2211", there are 2 pairs of '1's and 2 pairs '2's. Because the 2 '1's and 2 '2's are not matching pairs, we have 4 bulls. class Solution(object): def getHint(self, secret, guess): digits = [0 for x in range(10)] bulls = cows = 0 for index, secret_digit in enumerate(secret): guess_digit = guess[index] if guess_digit == secret_digit: bulls += 1 else: a, b = int(secret_digit), int(guess_digit) if digits[b] > 0: cows += 1 if digits[a] < 0: cows += 1 digits[a] += 1 digits[b] -= 1 return '{}A{}B'.format(bulls, cows) Share this: Enter your comment here... Fill in your details below or click an icon to log in: Email (required) (Address never made public) Name (required) You are commenting using your WordPress.
Read full article from Bulls and Cows | richdalgo
No comments:
Post a Comment