Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

You're at Witt's End.


devel / comp.lang.python / Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)

SubjectAuthor
* (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
+* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
|+- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
|+* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
||`* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
|| `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
||  `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
||   `- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
|`* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtGreg Ewing
| +- Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)Lawrence D'Oliveiro
| `* Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)Lawrence D'Oliveiro
|  `- Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)Paul Rubin
`- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin

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

<urga12$22ao7$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25365&group=comp.lang.python#25365

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@gmail.com (HenHanna)
Newsgroups: comp.lang.python
Subject: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Sun, 25 Feb 2024 13:04:34 -0800
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <urga12$22ao7$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 25 Feb 2024 21:04:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ce7bfe69056d0adae4ddb67da71b95e1";
logging-data="2173703"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18aL45EMCfNyDcXg5vnXmnoHHVvX4Odf98="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:O+QBpAJhsv4DYEfTbjKJRiyzGhY=
Content-Language: en-US
 by: HenHanna - Sun, 25 Feb 2024 21:04 UTC

(i just wrote (non-elegant) Python code.)

Could you share a short, VERY Readable Pythonic code that solves this?

Thank you!

https://i.imgur.com/72LGJjj.jpeg

3 digit lock
[682]: One number is correct and well-placed
[614]: One number is correct but wrongly placed
[206]: Two numbers are correct but wrongly placed
[738]: Nothing is correct
[780]: One number is correct but wrongly placed

HINT -- A mark of a great puzzle, this one contains a surprises or two.

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

<urgjdt$24v86$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25369&group=comp.lang.python#25369

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Sun, 25 Feb 2024 23:45:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <urgjdt$24v86$1@dont-email.me>
References: <urga12$22ao7$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 25 Feb 2024 23:45:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ea15bd06c2061660f1136ed37eb9a688";
logging-data="2260230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CPNSCZ3j/4SFI2DKx9tzh"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:RpX610h7iI27ZAbfRvYNGTF0S9k=
 by: Lawrence D'Oliv - Sun, 25 Feb 2024 23:45 UTC

On Sun, 25 Feb 2024 13:04:34 -0800, HenHanna wrote:

> Could you share a short, VERY Readable Pythonic code that solves this?

import itertools

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)
)
)
#end score

required_scores = \
(
((6, 8, 2), (1, 0)),
((6, 1, 4), (0, 1)),
((2, 0, 6), (0, 2)),
((7, 3, 8), (0, 0)),
((7, 8, 0), (0, 1)),
)

for answer in itertools.product(*(range(10),) * 3) :
if all \
(
score(candidate, answer) == required_score
for candidate, required_score in required_scores
) \
:
print(answer)
#end if
#end for

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

<24d255dfd5b0368a52926188cf07b614@www.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25370&group=comp.lang.python#25370

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!.POSTED!not-for-mail
From: HenHanna@gmail.com (HenHanna)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Mon, 26 Feb 2024 03:46:48 +0000
Organization: novaBBS
Message-ID: <24d255dfd5b0368a52926188cf07b614@www.novabbs.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="4142601"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Posting-User: 5a1f1f09909a70d7ae18ae9af00e018f83ece577
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Site: $2y$10$3HE/4omPwCq/gwY.mNcWFeff0G87N6bWakvZOQ/6pluHrKOfb46Cq
 by: HenHanna - Mon, 26 Feb 2024 03:46 UTC

wow... Thank you... I'm glad i asked.

HINT -- A mark of a great puzzle, this one contains a surprise

== the last clue is not required.

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

<87edcz1wlc.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25373&group=comp.lang.python#25373

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Mon, 26 Feb 2024 00:03:11 -0800
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <87edcz1wlc.fsf@nightsong.com>
References: <urga12$22ao7$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="19e8cf7f3d615d5b0a3ccfdfd1037b59";
logging-data="2561759"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WE8CCA18Sj/grZwhBfWFY"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:qb3mhtnYovqXs8TqruHDra06op4=
sha1:AGpC0TfLUZ9QVMe9JwsKgWTV3AY=
 by: Paul Rubin - Mon, 26 Feb 2024 08:03 UTC

HenHanna <HenHanna@gmail.com> writes:
> Could you share a short, VERY Readable Pythonic code that solves this?

Here is my brute force solution. It prints the list [42] which means
that the 3-digit answer is 042.

def check(n: int) -> bool:
"""return true iff n satisfies all the constraints. n is a 3 digit number
possibly with leading zeros."""
a,b,c = n//100, (n//10)%10, n%10

# 682 -- one number correct and well placed
k1 = (a==6 or b==8 or c==2) and len({a,b,c} & {6,8,2})==1
# 614 -- one number correct but wrongly placed
k2 = a != 6 and b != 1 and c != 4 and \
len({a,b,c} & {6,1,4})==1
# 206 -- two numbers correct but wrongly placed
k3 = len({a,b,c} & {2,0,6})==2 and a!=2 and b!=0 and c!=6
# 738 -- nothing correct
k4 = len({a,b,c} & {7,3,8}) == 0
# 780 -- one number correct but wrongly placed
k5 = len({a,b,c} & {7,8,0})==1 and a!=7 and b!=8 and c!=1

return all([k1,k2,k3,k4,k5])

print(list(filter(check, range(0,1000))))

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

<875xyb1v3a.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25374&group=comp.lang.python#25374

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Mon, 26 Feb 2024 00:35:37 -0800
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <875xyb1v3a.fsf@nightsong.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="19e8cf7f3d615d5b0a3ccfdfd1037b59";
logging-data="2573740"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jYeHR+hAVNZHkD+fxa9e3"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:uvWQAC8OvfbUgSvI+f4WwG4+9MM=
sha1:qx9F0iJhhNUmWB24tsFoyGkfYOA=
 by: Paul Rubin - Mon, 26 Feb 2024 08:35 UTC

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>> Could you share a short, VERY Readable Pythonic code that solves this?
> def score(candidate, answer) :

I like that approach. Here is my version:

from typing import Tuple
def digits(n): return n//100, (n//10)%10, n%10

def score(answer,n) -> Tuple[int,int]:
def count(*v): return sum(filter(bool,v))
x,y,z = digits(answer)
a,b,c = digits(n)

well_placed = count(a==x,b==y,c==z)
wrongly_placed = count(a in {y,z}, b in {x,z}, c in {x,y})
return well_placed, wrongly_placed

wanted = [(682,1,0),(614,0,1),(206,0,2),(738,0,0),(780,0,1)]

def check(n: int) -> bool:
return all(score(n,a)==(b,c) for a,b,c in wanted)

print(list(filter(check2, range(0,1000))))

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

<87zfvnzk47.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25375&group=comp.lang.python#25375

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Mon, 26 Feb 2024 00:48:40 -0800
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <87zfvnzk47.fsf@nightsong.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
<875xyb1v3a.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="19e8cf7f3d615d5b0a3ccfdfd1037b59";
logging-data="2579071"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19KHiAngdInRF2bPJVKyKqP"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:gmVPTLNP4ppiBKgnYh27/HLXr+Y=
sha1:DXz125IEqqgBwV75YfA+E6wXQp0=
 by: Paul Rubin - Mon, 26 Feb 2024 08:48 UTC

Paul Rubin <no.email@nospam.invalid> writes:
> I like that approach. Here is my version:

Oops, garbled. Fixed here, and I updated to follow your naming
convention.
================================================================
from typing import Tuple
def digits(n): return n//100, (n//10)%10, n%10

def score(answer,candidate) -> Tuple[int,int]:
def count(*v): return sum(filter(bool,v))
x,y,z = digits(answer)
a,b,c = digits(candidate)

well_placed = count(a==x,b==y,c==z)
wrongly_placed = count(a in {y,z}, b in {x,z}, c in {x,y})
return well_placed, wrongly_placed

wanted = [(682,1,0),(614,0,1),(206,0,2),(738,0,0),(780,0,1)]

def check2(n: int) -> bool:
return all(score(n,candidate)==(right,wrong)
for candidate,right,wrong in wanted)

print(list(filter(check2, range(0,1000))))

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

<7f2222b92b82ec4b23eb3c69769a0260@www.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25377&group=comp.lang.python#25377

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!.POSTED!not-for-mail
From: HenHanna@gmail.com (HenHanna)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Mon, 26 Feb 2024 09:55:09 +0000
Organization: novaBBS
Message-ID: <7f2222b92b82ec4b23eb3c69769a0260@www.novabbs.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me> <875xyb1v3a.fsf@nightsong.com> <87zfvnzk47.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="4173028"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Site: $2y$10$rQNl/Obd6kov/urDU7zehOOjADucfBYGjBxYefNh6VI.eecqW8Q0m
X-Rslight-Posting-User: 5a1f1f09909a70d7ae18ae9af00e018f83ece577
 by: HenHanna - Mon, 26 Feb 2024 09:55 UTC

wow... Thank you... I'm glad i asked.

HINT -- A mark of a great puzzle, this one contains a surprise (or two)
== the last 2 clues are not required.


1. Could there be a different approach? (not exhaustive search) ?

2. What's a nice tweak on this problem that
would call for a program that's just a bit longer, harder?

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

<87v86byxm8.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25381&group=comp.lang.python#25381

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Mon, 26 Feb 2024 08:54:39 -0800
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87v86byxm8.fsf@nightsong.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
<875xyb1v3a.fsf@nightsong.com> <87zfvnzk47.fsf@nightsong.com>
<7f2222b92b82ec4b23eb3c69769a0260@www.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="19e8cf7f3d615d5b0a3ccfdfd1037b59";
logging-data="2784223"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18X3vUnEhUXggdvNZBY1cum"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:q8n1gzTrQgQ1uEbwN22M0Fn0FZg=
sha1:19YTEvBE34pP5NgbzMYOrUqhYlI=
 by: Paul Rubin - Mon, 26 Feb 2024 16:54 UTC

HenHanna@gmail.com (HenHanna) writes:
> 1. Could there be a different approach? (not exhaustive search) ?

Yes, this type of puzzle is called a constraint satisfaction problem
(CSP). There is a big literature on how to solve those. Basically you
use the different clues to narrow the search space. There is a language
called Prolog which is designed for this type of problem. Beyond that,
there are tools called SAT solvers (SAT = boolean satisfiability
problem) that try to solve a related class of problems as efficiently as
they can. But, in general, there is no known algorithm that solves the
entire class efficiently, and probably none exists (this is the famous P
vs NP problem).

> 2. What's a nice tweak on this problem that
> would call for a program that's just a bit longer, harder?

I think it is ok the way it is.

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

<b39c9658b896527342b6c7b99f58ee29@www.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25384&group=comp.lang.python#25384

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!.POSTED!not-for-mail
From: HenHanna@gmail.com (HenHanna)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Tue, 27 Feb 2024 18:51:38 +0000
Organization: novaBBS
Message-ID: <b39c9658b896527342b6c7b99f58ee29@www.novabbs.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me> <875xyb1v3a.fsf@nightsong.com> <87zfvnzk47.fsf@nightsong.com> <7f2222b92b82ec4b23eb3c69769a0260@www.novabbs.com> <87v86byxm8.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="140911"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Posting-User: 5a1f1f09909a70d7ae18ae9af00e018f83ece577
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Site: $2y$10$tE.6UmpD4.VcJx2X5EqUUuevJEC6XL0vgc7ecjnyYMQPEm3Kvqpoq
 by: HenHanna - Tue, 27 Feb 2024 18:51 UTC

bard.google.com (now Gemini) can't write Python code that solves this.

can ChatGPT do it?

Paul Rubin wrote:
> (HenHanna) writes:
>> 1. Could there be a different approach? (not exhaustive search) ?

> Yes, this type of puzzle is called a constraint satisfaction problem
> (CSP). There is a big literature on how to solve those. Basically you
> use the different clues to narrow the search space. There is a language
> called Prolog which is designed for this type of problem.

i have Knuth's book on CSP... i'll check inside.

yes, i studied Prolog when i was younger. (Wrote a Prolog interpreter in Lisp, etc.)

Is this a typical easy exercise in Prolog programming? Could someone share Prolog code for it?

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

<l47r62Fp7gcU1@mid.individual.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25385&group=comp.lang.python#25385

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ewing@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Wed, 28 Feb 2024 17:29:54 +1300
Lines: 19
Message-ID: <l47r62Fp7gcU1@mid.individual.net>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net PPMqM2nR4aMDjT1oK6/edQ6DLQjxmdBJnBm1PvtVLnSkC7HI2e
Cancel-Lock: sha1:gfmNy+oFGhJ05FiBLbyQlSDcqGc= sha256:D6AMqbcORyi1D3pnf+w74I3CJ4QQ8R/2GRuHK37Kj7I=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <urgjdt$24v86$1@dont-email.me>
 by: Greg Ewing - Wed, 28 Feb 2024 04:29 UTC

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

Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)

<urmgb1$3ml7a$3@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25386&group=comp.lang.python#25386

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List
Prohibited)
Date: Wed, 28 Feb 2024 05:29:05 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <urmgb1$3ml7a$3@dont-email.me>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
<l47r62Fp7gcU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 28 Feb 2024 05:29:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2b5f550de027bed53e7de3d0720ccb3e";
logging-data="3888362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KdIASMaCitNvUMCkG3QhB"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:2CkrAgxAGUXkCqq09lcu776biqM=
 by: Lawrence D'Oliv - Wed, 28 Feb 2024 05:29 UTC

On Wed, 28 Feb 2024 17:29:54 +1300, Greg Ewing wrote:

> This is not correct. score((1,1,1), (1,1,2)) gives (2,4).

As a general solution, you would be right. As a solution to this
particular problem (with no repeated numbers), it did the job.

Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)

<usu3dh$1enrd$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25469&group=comp.lang.python#25469

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List
Prohibited)
Date: Thu, 14 Mar 2024 05:53:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <usu3dh$1enrd$1@dont-email.me>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
<l47r62Fp7gcU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 14 Mar 2024 05:53:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8a2351749b4eebd2a62c741e1ddb0aa2";
logging-data="1531757"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//EH8IrDjejkK64l65NPBz"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:0NWqVZtIuAQltvny2HXr24gPH00=
 by: Lawrence D'Oliv - Thu, 14 Mar 2024 05:53 UTC

On Wed, 28 Feb 2024 17:29:54 +1300, Greg Ewing wrote:

> 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).

How about this as a more general Mastermind scoring function, then:

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)
for s in (set(i for i, (a, b) in enumerate(zip(candidate, answer)) if a == b),)
if i not in s and j not in s
)
)
#end score

Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)

<87h6h8z17f.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25471&group=comp.lang.python#25471

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python
Subject: Re: (Mastermind) puzzle (with 3 digits) (Posting On Python-List Prohibited)
Date: Thu, 14 Mar 2024 19:19:16 -0700
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87h6h8z17f.fsf@nightsong.com>
References: <urga12$22ao7$2@dont-email.me> <urgjdt$24v86$1@dont-email.me>
<l47r62Fp7gcU1@mid.individual.net> <usu3dh$1enrd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="1c4c328df770804111002d845ef94b32";
logging-data="2064724"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XjZaXBvPtTi7KGZzrSSaV"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:9AtvDUJ6DQfOzUHNkSdEj0gUfF8=
sha1:JfvSNIZ5l7clwo/AjBf4ixp8v98=
 by: Paul Rubin - Fri, 15 Mar 2024 02:19 UTC

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)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor