(Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Feb 27 23:29:54 EST 2024


On 26/02/24 12:45 pm, Lawrence D'Oliveiro wrote:
> def score(candidate, answer) :
>      return \
>          (
>              sum(a == b for a, b in zip(candidate, answer)),
>              sum
>                (
>                  i != j and a == b
>                  for i, a in enumerate(candidate)
>                  for j, b in enumerate(answer)
>                )
>          )

This is not correct. score((1,1,1), (1,1,2)) gives (2,4). According to
the usual rules of Mastermind, it should be (2, 0).

-- 
Greg



More information about the Python-list mailing list