# 299 bulls and cows

### 299. Bulls and Cows

https://leetcode.com/problems/bulls-and-cows/

Easy

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
maps = {}
for i in range(len(secret)):
if secret[i] not in maps:
maps[secret[i]] = [i]
else:
maps[secret[i]].append(i)
mapg = {}
for i in range(len(guess)):
if guess[i] not in mapg:
mapg[guess[i]] = [i]
else:
mapg[guess[i]].append(i)

print maps, mapg

a,b = 0,0
for key in maps.keys():
if key in mapg.keys():
common = list(set(mapg[key]) & set(maps[key]))
#check for bull
a += len(common)
mapg[key] = [item for item in mapg[key] if item not in common]
maps[key] = [item for item in maps[key] if item not in common]
b += min(len(maps[key]), len(mapg[key]))
return str(a) + 'A' + str(b) + 'B'


class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
bull = sum(map(operator.eq, secret, guess))
sa = collections.Counter(secret)
sb = collections.Counter(guess)
cow = sum((sa & sb).values()) - bull
return str(bull) + 'A' + str(cow) + 'B'


bull = secret与guess下标与数值均相同的数字个数

cow = secret与guess中出现数字的公共部分 - bull

def getHint(self, secret, guess):
bulls = sum(map(operator.eq, secret, guess))
both = sum(min(secret.count(x), guess.count(x)) for x in '0123456789')
return '%dA%dB' % (bulls, both - bulls)


>>> def add100(x):
...     return x+100
...
>>> hh = [11,22,33]
[111, 122, 133]


map(operator.eq, secret, guess)
[False, True, False, False]