Skip to content

Commit 5357669

Browse files
authored
Update bulls-and-cows.py
1 parent 567a376 commit 5357669

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

Python/bulls-and-cows.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,14 @@ def getHint(self, secret, guess):
1717
:rtype: str
1818
"""
1919
A, B = 0, 0
20-
s_lookup, g_lookup = defaultdict(int), defaultdict(int)
20+
lookup = defaultdict(int)
2121
for s, g in izip(secret, guess):
2222
if s == g:
2323
A += 1
2424
else:
25-
if s_lookup[g]:
26-
s_lookup[g] -= 1
27-
B += 1
28-
else:
29-
g_lookup[g] += 1
30-
if g_lookup[s]:
31-
g_lookup[s] -= 1
32-
B += 1
33-
else:
34-
s_lookup[s] += 1
35-
25+
B += int(lookup[s] < 0) + int(lookup[g] > 0)
26+
lookup[s] += 1
27+
lookup[g] -= 1
3628
return "%dA%dB" % (A, B)
3729

3830

0 commit comments

Comments
 (0)