Well I would like to make a little program that given a certain
logical expression gives the complete truth table.
It's not too difficult in fact, I just have some doubts on how to
design it.
I thought something like that:
class Term:
class Table:
def and(...
def or(...
But I'm not convinced...
I would like something like that more or less:
a,b,c = Term()
table([a,b,c], impl(and(or(a,b )),c))
Any idea??
Thanks 5 6594
2008/9/26 andrea <ke******@gmail .com>:
Well I would like to make a little program that given a certain
logical expression gives the complete truth table.
It's not too difficult in fact, I just have some doubts on how to
design it.
I thought something like that:
class Term:
class Table:
def and(...
def or(...
As a quick and dirty solution, I'd write a function that takes a
function as a parameter.
Starting with a helper function to separate the bits of an integer
into a list of bools:
def int_to_bool(i, bits):
# Extract the bits of i to a boolean array.
# 'bits' is the number of signifcant bits.
result = []
for j in range(0, bits):
result.append(i & 1)
i >>= 1
result.reverse( )
return result
Now I'd define a function such as:
def table(f, n):
# Calculate a truth table for function f taking n boolean parameters
result = []
for i in range(0, math.pow(2, n)):
for j in range(0, n):
params = int_to_bool(i, n)
result.append(p arams + [(f(*params))])
return result
Now you could define the function you want as a separate function, or
just use a lambda:
table(lambda a, b, c:(a or b) and c, 3)
[[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 1, 1, 1], [1, 0, 0, 0],
[1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 1, 1]]
Each element in the result is the state of the parameters followed by
the result of the function.
I stress that this is quick and dirty -- I'm sure somebody will be
along with something better soon!
--
Tim Rowe
On Sep 26, 11:40*am, "Tim Rowe" <digi...@gmail. comwrote:
2008/9/26 andrea <kerny...@gmail .com>:
Well I would like to make a little program that given a certain
logical expression gives the complete truth table.
It's not too difficult in fact, I just have some doubts on how to
design it.
I thought something like that:
class Term:
class Table:
* def and(...
* def or(...
As a quick and dirty solution, I'd write a function that takes a
function as a parameter.
Starting with a helper function to separate the bits of an integer
into a list of bools:
def int_to_bool(i, bits):
* * * * # Extract the bits of i to a boolean array.
* * * * # 'bits' is the number of signifcant bits.
* * * * result = []
* * * * for j in range(0, bits):
* * * * * * * * result.append(i & 1)
* * * * * * * * i >>= 1
* * * * result.reverse( )
* * * * return result
Now I'd define a function such as:
def table(f, n):
* * * * # Calculate a truth table for function f taking n booleanparamete rs
* * * * result = []
* * * * for i in range(0, math.pow(2, n)):
* * * * * * * * for j in range(0, n):
* * * * * * * * * * * * params = int_to_bool(i,n )
* * * * * * * * result.append(p arams + [(f(*params))])
* * * * return result
Now you could define the function you want as a separate function, or
just use a lambda:
table(lambda a, b, c:(a or b) and c, 3)
[[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 1, 1, 1], [1, 0, 0, 0],
[1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 1, 1]]
Each element in the result is the state of the parameters followed by
the result of the function.
I stress that this is quick and dirty -- I'm sure somebody will be
along with something better soon!
--
Tim Rowe
Good idea. If you want prefixed operators: 'and( a, b )' instead of
'a and b', you'll have to write your own. ('operator.and_ ' is bitwise
only.) It may be confusing to mix prefix with infix: 'impl( a and b,
c )', so you may want to keep everything prefix, but you can still use
table( f, n ) like Tim said.
On 26 Set, 20:01, "Aaron \"Castironpi \" Brady" <castiro...@gma il.com>
wrote:
>
Good idea. *If you want prefixed operators: 'and( a, b )' instead of
'a and b', you'll have to write your own. *('operator.and _' is bitwise
only.) *It may be confusing to mix prefix with infix: 'impl( a and b,
c )', so you may want to keep everything prefix, but you can still use
table( f, n ) like Tim said.
After a while I'm back, thanks a lot, the truth table creator works,
now I just want to parse some strings to make it easier to use.
Like
(P \/ Q) -S == S
Must return a truth table 2^3 lines...
I'm using pyparsing and this should be really simple, but it doesn't
allow me to recurse and that makes mu stuck.
The grammar BNF is:
Var :: = [A..Z]
Exp ::= Var | !Exp | Exp \/ Exp | Exp -Exp | Exp /\ Exp | Exp ==
Exp
I tried different ways but I don't find a smart way to get from the
recursive bnf grammar to the implementation in pyparsing...
Any hint?
On Oct 24, 5:53*am, andrea <kerny...@gmail .comwrote:
On 26 Set, 20:01, "Aaron \"Castironpi \" Brady" <castiro...@gma il.com>
wrote:
Good idea. *If you want prefixed operators: 'and( a, b )' instead of
'a and b', you'll have to write your own. *('operator.and _' is bitwise
only.) *It may be confusing to mix prefix with infix: 'impl( a and b,
c )', so you may want to keep everything prefix, but you can still use
table( f, n ) like Tim said.
After a while I'm back, thanks a lot, the truth table creator works,
now I just want to parse some strings to make it easier to use.
Like
(P \/ Q) -S == S
Must return a truth table 2^3 lines...
I'm using pyparsing and this should be really simple, but it doesn't
allow me to recurse and that makes mu stuck.
The grammar BNF is:
Var :: = [A..Z]
Exp ::= Var | !Exp *| Exp \/ Exp | Exp -Exp | Exp /\ Exp | Exp ==
Exp
I tried different ways but I don't find a smart way to get from the
recursive bnf grammar to the implementation in pyparsing...
Any hint?
Use Forward to create a recursive grammar. Look at the examples page
on the pyparsing wiki, and there should be several samples of
recursive grammars.
Here is a very simple recursive grammar, with no precedence to your
operators:
from pyparsing import oneOf, alphas, Forward, ZeroOrMore, Group,
Optional
var = oneOf(list(alph as))
op = oneOf(r"\/ /\ -==")
expr = Forward()
expr << Optional('!') + ( var | Group('(' + expr + ')') ) +
ZeroOrMore(op + expr)
test = "(P \/ Q) -S == S"
print expr.parseStrin g(test).asList( )
prints:
[['(', 'P', '\\/', 'Q', ')'], '->', 'S', '==', 'S']
Since these kinds of expressions are common, pyparsing includes a
helper method for defining precedence of operations infix notation:
from pyparsing import operatorPrecede nce, opAssoc
expr = operatorPrecede nce(var,
[
(r'!', 1, opAssoc.RIGHT),
(r'\/', 2, opAssoc.LEFT),
(r'/\\', 2, opAssoc.LEFT),
(r'->', 2, opAssoc.LEFT),
(r'==', 2, opAssoc.LEFT),
])
print expr.parseStrin g(test).asList( )
prints:
[[[['P', '\\/', 'Q'], '->', 'S'], '==', 'S']]
HTH,
-- Paul
On Oct 24, 5:53*am, andrea <kerny...@gmail .comwrote:
On 26 Set, 20:01, "Aaron \"Castironpi \" Brady" <castiro...@gma il.com>
wrote:
Good idea. *If you want prefixed operators: 'and( a, b )' instead of
'a and b', you'll have to write your own. *('operator.and _' is bitwise
only.) *It may be confusing to mix prefix with infix: 'impl( a and b,
c )', so you may want to keep everything prefix, but you can still use
table( f, n ) like Tim said.
After a while I'm back, thanks a lot, the truth table creator works,
now I just want to parse some strings to make it easier to use.
Like
(P \/ Q) -S == S
Must return a truth table 2^3 lines...
I'm using pyparsing and this should be really simple, but it doesn't
allow me to recurse and that makes mu stuck.
The grammar BNF is:
Var :: = [A..Z]
Exp ::= Var | !Exp *| Exp \/ Exp | Exp -Exp | Exp /\ Exp | Exp ==
Exp
I tried different ways but I don't find a smart way to get from the
recursive bnf grammar to the implementation in pyparsing...
Any hint?
Tell you what. At the risk of "carrot-and-stick, jump-how-high"
tyranny, I'll show you some output of a walk-through. It should give
you an idea of the process. You can always ask for more hints.
( ( ( !( R ) /\ ( !( P \/ Q ) ) ) -S ) == S )
(((!(R)/\(!(P\/Q)))->S)==S)
(((!R/\(!(P\/Q)))->S)==S)
n1 := !R
(((n1/\(!(P\/Q)))->S)==S)
n2 := P\/Q
(((n1/\(!(n2)))->S)==S)
(((n1/\(!n2))->S)==S)
n3 := !n2
(((n1/\(n3))->S)==S)
(((n1/\n3)->S)==S)
n4 := n1/\n3
(((n4)->S)==S)
((n4->S)==S)
n5 := n4->S
((n5)==S)
(n5==S)
n6 := n5==S
(n6)
n6
{'n1': (<function not_ at 0x00A04070>, '!R', ('R',)),
'n2': (<function or_ at 0x00A040F0>, 'P\\/Q', ('P', 'Q')),
'n3': (<function not_ at 0x00A04070>, '!n2', ('n2',)),
'n4': (<function and_ at 0x00A040B0>, 'n1/\\n3', ('n1', 'n3')),
'n5': (<function imp_ at 0x00A04130>, 'n4->S', ('n4', 'S')),
'n6': (<function eq_ at 0x00A04170>, 'n5==S', ('n5', 'S'))}
{'Q': True, 'P': True, 'S': True, 'R': True} True
{'Q': True, 'P': True, 'S': False, 'R': True} False
{'Q': True, 'P': True, 'S': True, 'R': False} True
{'Q': True, 'P': True, 'S': False, 'R': False} False
{'Q': False, 'P': True, 'S': True, 'R': True} True
{'Q': False, 'P': True, 'S': False, 'R': True} False
{'Q': False, 'P': True, 'S': True, 'R': False} True
{'Q': False, 'P': True, 'S': False, 'R': False} False
{'Q': True, 'P': False, 'S': True, 'R': True} True
{'Q': True, 'P': False, 'S': False, 'R': True} False
{'Q': True, 'P': False, 'S': True, 'R': False} True
{'Q': True, 'P': False, 'S': False, 'R': False} False
{'Q': False, 'P': False, 'S': True, 'R': True} True
{'Q': False, 'P': False, 'S': False, 'R': True} False
{'Q': False, 'P': False, 'S': True, 'R': False} True
{'Q': False, 'P': False, 'S': False, 'R': False} True
Before you trust me too much, you might want to check at least some of
these, to see if the starting (complicated) expression is evaluated
correctly. I didn't. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Good Man |
last post by:
Hi there
I'm in the planning stages of creating a database, and I have two options
here. Which makes more sense, and/or provides better performance for
queries - a single table with hundreds of thousands of rows, or many
tables with less rows?
GOAL:
Recording information about buildings, storey by storey. Basically I
will be recording six bits of information about each storey in each
|
by: rsd |
last post by:
how do you specify the "colspan" attribut of a <td> cell when building
the table/rows/cells dynamicly?
ie: lets say we have the follwoing code:
table = document.all;
// header row
tr1 = table.insertRow();
td1 = tr1.insertCell();
td1.innerHTML = "this is the header row";
|
by: Mac MCCall |
last post by:
I am doing work for a community that currently has four different MS-Access
databases which are at four separate locations but have a great deal of
overlapping data.
There is a residents database used by the website
These is a billing website used by the management company
There is an access (to the community, not type of) database used by the
security company
There is a residents database used by the homeowners association
|
by: matt |
last post by:
(first -- does anyone know of a good CR group?)
hello,
i am new to CR. i have created one report that queries the db and
presents the data to the user in a tabular-like format.
however, i could find no "table designer" in CR (im on 9, its our
corporate standard still). thus i had to draw a box, and manually draw
lines inbetween the details fields and rows in order to make it look
|
by: Franck |
last post by:
Hi,
I'd like to know if it's possible to get server side relationship when
building Link Tables in my AccessFront End.
Basic Sample :
In my Sql Server 2005 DataBase, I've got Table2 which is linked to
Table1 with a foreign key.
When I add them to my Access Front End, I can't see the relationship
in the... relationship diagram.
| |
by: wujtehacjusz |
last post by:
Dear All
I am very new to MS SQL Server and I am wondering is there some tool
which would allow me to build pivot tables in SQL more easily. At the
moment writing a query can be quite challenging and difficult.
Is there any software which allows you to do it more intuitively and
gives you some visual feedback about query you are building?
I would be very grateful for any help with this.
|
by: ambanks04 |
last post by:
ok taking computer science class anybody know what to do i am not computer literate
CoSc 111.003 Spring 2008 Assignment 4
1) Each student shall design, develop, code, test and submit an
Original computer program which uses a function or functions to display the TRUTH TABLES for the Logical And, the Logical Or, and the Logical Not operators., as defined in Section 5.8, of the text, 5Th/6Th Edition.
2) The program shall:
...
|
by: papacologne |
last post by:
Given the truth values of the propositions p and q, display the truth values of the conjunction, disjunction, exclusive or, conditional statement, and biconditional of these propositions.
Can you help me with this problem? Is this easy? If possible can you write the code? or if it's too long can you just help me what should i do? Thanks. I really need it badly. =D Thanks. Much Appreciated.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |