Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61382
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'true,': 0.05; 'false,': 0.09; 'newest': 0.09; 'objects:': 0.09; 'part,': 0.09; 'stack.': 0.09; 'todo:': 0.09; 'try:': 0.09; 'val': 0.09; 'valueerror:': 0.09; 'def': 0.12; 'changes': 0.15; '%r"': 0.16; '10:49': 0.16; '__or__(self,': 0.16; 'boolean': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'i1,': 0.16; 'i2,': 0.16; 'idea:': 0.16; 'inputs': 0.16; 'limit.': 0.16; 'once.': 0.16; 'operators,': 0.16; 'opposite': 0.16; 'so!': 0.16; 'subject:Programming': 0.16; 'throw': 0.16; 'valueerror': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'value.': 0.19; 'not,': 0.20; '(the': 0.22; 'input': 0.22; 'python?': 0.22; 'to:name:python-list@python.org': 0.22; 'mon,': 0.24; "haven't": 0.24; 'question': 0.24; 'first,': 0.26; 'this:': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'long.': 0.31; 'piece': 0.31; 'values.': 0.31; 'with,': 0.31; 'class': 0.32; 'this.': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'equal': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'are,': 0.36; 'crazy': 0.36; 'false': 0.36; 'found.': 0.36; 'done': 0.36; 'so,': 0.37; 'too': 0.37; 'two': 0.37; 'skip:o 20': 0.38; 'easiest': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; "couldn't": 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'then,': 0.60; 'most': 0.60; 'helps': 0.61; 'new': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'taking': 0.65; 'combining': 0.68; 'search,': 0.74; 'goal': 0.75; 'hey,': 0.75; 'other.': 0.75; 'gain': 0.79; '"not': 0.84; 'circuit': 0.84; 'hardest': 0.91; 'fun!': 0.93; 'have.': 0.93; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=pqTd8d9kPM5/dsyoVRGSCpVhr6bQzFtjztDT+Cylk7E=; b=yNv+HE1aMk0bcVuTN/u+r+YIa1Ih94hJEpVH2PTzA2QCa/jx3ltUZW9P0+GyHjTtkR QgWx0LvWvpe56DOhZFHf0j5x57pKC9/WWcUmxBKEc7I3EeLKIvMFKVMYHHLsgsqa6uFr PZvjWi0rtHMgY76cF6twLFRFFZ4w+Gz/DtkwA3JkDeJ+JnDUDXiZiCU8abFFLWGYBhCk EP+XPPegWz0l/P7SbZuWvMPEu7gJBEzZWZRUBfQOZyfNYcS4b1dgnpjHBJOdHavQG8Xr TOj1v6G9wrZBJ5+/b6LaY5Y3ugIBCfH95CPR7veWvp9SGHvcCNwOdg/wvgnRxREXzg1w ERYA== |
| MIME-Version | 1.0 |
| X-Received | by 10.66.118.71 with SMTP id kk7mr20794013pab.14.1386595546169; Mon, 09 Dec 2013 05:25:46 -0800 (PST) |
| In-Reply-To | <l84aor$3mj$1@news.albasani.net> |
| References | <l84aor$3mj$1@news.albasani.net> |
| Date | Tue, 10 Dec 2013 00:25:45 +1100 |
| Subject | Re: Programming puzzle with boolean circuits |
| From | Chris Angelico <rosuav@gmail.com> |
| To | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3775.1386595556.18130.python-list@python.org> (permalink) |
| Lines | 147 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1386595556 news.xs4all.nl 2830 [2001:888:2000:d::a6]:38901 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:61382 |
Show key headers only | View raw
On Mon, Dec 9, 2013 at 10:49 PM, Johannes Bauer <dfnsonfsduifb@gmx.de> wrote:
> The question is: How do you design a boolean circuit that contains at
> most 2 NOT gates, but may contain as many AND or OR gates that inverts
> three inputs? IOW: Build three inverters by using only two inverters
> (and an infinite amount of AND/OR).
>
> I found this puzzle again and was thinking about: How would I code a
> brute-force approach to this problem in Python?
Ooooh interesting!
Well, here's a start: There's no value in combining the same value in
an AND or an OR, ergo every gate you add must bring together two
different values.
To start with, you have three values (the three inputs). Every time
you combine two of them, with either type of gate, you create a new
value. You can also combine a single value with a NOT to create its
inverse, but only if you have done so no more than once.
The goal is to produce something which is provably the opposite of
each of the three inputs.
I'm not sure if this helps or not, but one thing I learned from
geometry is that setting down everything you know and need to know is
a good basis for the search!
The hardest part, so far, is proving a result. The algorithm that's
coming to mind is this:
def find_solution(inputs, not_count):
# TODO: First, see if inputs contains three values that are the inverses of
# the three values i1,i2,i3. If they are, throw something, that's probably
# the easiest way to unwind the stack.
if not_count < 2:
for val in inputs:
find_solution(inputs + [not val], not_count + 1)
for val1 in inputs:
for val2 in inputs:
if val1 is not val2:
find_solution(inputs + [val1 and val2], not_count)
find_solution(inputs + [val1 or val2], not_count)
find_solution([i1, i2, i3], 0)
So, here's a crazy idea: Make i1, i2, i3 into objects of a type with
an __eq__ that actually does the verification. Schrodinger's Objects:
they might be True, might be False, and until you call __eq__, they're
in both states. This probably isn't the best way, but I think it's the
most fun!
I couldn't make this work with the and/or/not operators, so I'm using
the &/|/~ operators, which can be overridden.
So! There's the basics, but it's a depth-first search, which means
it's bound to hit the recursion limit. Refinement needed;
specifically, it needs to not add any input that's equal to any other.
That's easy enough.
Unfortunately I haven't been able to prove that the code works,
because even with some changes it's taking way too long. But hey, it's
a crazy fun piece to work with!
class Schrodinger:
def __init__(self, bit):
self.state = bit
def coalesce(self, master):
return bool(master & self.state)
def __len__(self):
return 1;
def __invert__(self):
return Negated(self)
def __and__(self, other):
return Anded((self, other))
def __or__(self, other):
return Ored((self, other))
def __eq__(self, other):
for master in range(8):
if self.coalesce(master) != other.coalesce(master):
return False
return True
def __repr__(self):
return "$%d" % self.state
class Negated(Schrodinger):
def coalesce(self, master):
return not self.state.coalesce(master)
def __len__(self):
return len(self.state) + 1
def __repr__(self):
return "not %r" % self.state
class Anded(Schrodinger):
def coalesce(self, master):
return self.state[0].coalesce(master) and self.state[1].coalesce(master)
def __len__(self):
return len(self.state[0]) + len(self.state[1]) + 1
def __repr__(self):
return "%r and %r" % self.state
class Ored(Schrodinger):
def coalesce(self, master):
return self.state[0].coalesce(master) or self.state[1].coalesce(master)
def __len__(self):
return len(self.state[0]) + len(self.state[1]) + 1
def __repr__(self):
return "%r or %r" % self.state
class SolutionFound(Exception):
pass
def find_solution(inputs, not_count):
# First see if the newest input is equal to anything we already have.
# If it is, we gain nothing by probing this.
if inputs[-1] in inputs[:-1]: return
# Then, see if inputs contains three values that are the inverses of
# the three values i1,i2,i3. If they are, throw something, that's probably
# the easiest way to unwind the stack.
try:
raise SolutionFound("""Solution:
~$1 = %r
~$2 = %r
~$4 = %r""" % (
inputs[inputs.index(~inputs[0])],
inputs[inputs.index(~inputs[1])],
inputs[inputs.index(~inputs[2])],
))
except ValueError:
pass # ValueError means one of the negations wasn't found.
if not_count < 2:
for val in inputs:
find_solution(inputs + [~val], not_count + 1)
for val1 in inputs:
for val2 in inputs:
find_solution(inputs + [val1 & val2], not_count)
find_solution(inputs + [val1 | val2], not_count)
i1 = Schrodinger(1)
i2 = Schrodinger(2)
i3 = Schrodinger(4)
find_solution([i1, i2, i3], 0)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Programming puzzle with boolean circuits Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-12-09 12:49 +0100
Re: Programming puzzle with boolean circuits Chris Angelico <rosuav@gmail.com> - 2013-12-10 00:25 +1100
Re: Programming puzzle with boolean circuits Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-12-11 14:52 +0100
Re: Programming puzzle with boolean circuits Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-09 15:19 -0500
Spoiler alert? (Re: Programming puzzle with boolean circuits) John Ladasky <john_ladasky@sbcglobal.net> - 2013-12-09 12:39 -0800
Re: Spoiler alert? (Re: Programming puzzle with boolean circuits) Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-09 15:45 -0500
Re: Programming puzzle with boolean circuits Chris Angelico <rosuav@gmail.com> - 2013-12-10 12:41 +1100
Fwd: Programming puzzle with boolean circuits Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-09 21:03 -0500
Re: Programming puzzle with boolean circuits Chris Angelico <rosuav@gmail.com> - 2013-12-10 13:21 +1100
Re: Programming puzzle with boolean circuits Chris Angelico <rosuav@gmail.com> - 2013-12-10 19:50 +1100
Re: Programming puzzle with boolean circuits Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-12-10 15:25 +0100
csiph-web