Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61429

Fwd: Programming puzzle with boolean circuits

References <l84aor$3mj$1@news.albasani.net> <CAPM-O+z4vuhoyKuEVdoYxJe42qhXT89GQTyQEHGjvm+c8yV3NQ@mail.gmail.com>
Date 2013-12-09 21:03 -0500
Subject Fwd: Programming puzzle with boolean circuits
From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3806.1386641004.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Chris, and all.. Since you posted yours, I post this for your pleasure.  I
couldn't figure out what you were doing.

#!/usr/bin/env python

"""
This is a puzzle brought up on the python mailing list.
The goal is to take 3 bits and invert them using boolean logic, but
restricted to only 2 NOT gates
Here is a solution I found via google:
http://www.thelowlyprogrammer.com/2008/05/not-puzzle-solution.html
"""

def invert_three(a,b,c):

    """ give three boolean values, return their inverted values
    Only 2 NOT operators are allowed
    Deduce these truths
    """
    all_ones = a and b and c
    two_or_three = (a and b) or (a and c) or (b and c)
    zero_or_one = not two_or_three
    one_one = zero_or_one and (a or b or c)
    zero_or_two = not (all_ones or one_one)
    zero_ones = zero_or_one and zero_or_two
    two_ones = zero_or_two and two_or_three

    # the output is true if all the inputs are zero, or if one of the
inputs is zero and it is either b or c
    # or two inputs are zero and they are b and c
    # ditto for other two inputs
    x = zero_ones or (one_one and (b or c)) or (two_ones and (b and c))
    y = zero_ones or (one_one and (a or c)) or (two_ones and (a and c))
    z = zero_ones or (one_one and (b or a)) or (two_ones and (b and a))
    return int(x), int(y), int(z)

if __name__ == "__main__":
    for a in range(2):
        for b in range(2):
            for c in range(2):
                print "Input: ", a, b, c,
                x, y, z = invert_three(a,b,c)
                print "Output: ", x, y, z


-- 
Joel Goldstick
http://joelgoldstick.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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