Sujet : Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)
De : no.email (at) *nospam* nospam.invalid (Paul Rubin)
Groupes : comp.lang.pythonDate : 15. Mar 2024, 03:19:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87h6h8z17f.fsf@nightsong.com>
References : 1 2 3 4
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Lawrence D'Oliveiro <
ldo@nz.invalid> writes:
How about this as a more general Mastermind scoring function, then:
This works for me:
from collections import Counter as multiset
def msum(m: multiset) -> int: return sum(m.values())
def digits(n: int) -> int: return n//100, (n//10)%10, n%10
def score(answer: int,candidate: int) -> Tuple[int,int]:
a = digits(answer)
b = digits(candidate)
well_placed = sum(x==y for x,y in zip(a,b))
wrongly_placed = msum(multiset(a) & multiset(b)) - well_placed
return well_placed, wrongly_placed
print (score(111,112)) # prints (2, 0)