4 Replies - 357 Views - Last Post: 07 February 2012 - 11:55 AM Rate Topic: -----

Topic Sponsor:

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-January 10

writing code for logic conditions

Posted 07 February 2012 - 06:17 AM

We can show conditions for AND, OR by simple codes:

def funAnd():
if condition1 and condition2:
return True
#######
0 | 0 -> 0
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1

def funOr():

if condition1 or condition2:
return True
#######
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
#######################################
what about generating conditional tests for XOR, NAND, NOR, XNOR logics.
########## XOR
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1

########## NAND
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0

########## NOR
0 | 0 -> 1
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 0

########## XNOR
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0

How can we test for these logic?

Is This A Good Question/Topic? 0
  • +

Replies To: writing code for logic conditions

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 621
  • View blog
  • Posts: 1,038
  • Joined: 21-June 11

Re: writing code for logic conditions

Posted 07 February 2012 - 06:23 AM

By rewriting them in terms of not, and and or. For example x nor y can be trivially expressed as not(x or y).
Was This Post Helpful? 1
  • +
  • -

#3 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-January 10

Re: writing code for logic conditions

Posted 07 February 2012 - 08:02 AM

thanks sepp2K.

what about other logics??
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: writing code for logic conditions

Posted 07 February 2012 - 08:28 AM

First your truth table for xor and xnor appear incorrect you have:

Quote

########## XOR
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
########## XNOR
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0

It should be:

Quote

########## XOR
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
########## XNOR
0 | 0 -> 1
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1


Jim
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 3756
  • View blog
  • Posts: 9,707
  • Joined: 16-October 07

Re: writing code for logic conditions

Posted 07 February 2012 - 11:55 AM

No code? Sad.

Here's a simple tester:
def showTable(symbol, func):
	print(symbol)
	for c1 in (0,1):
		for c2 in (0,1):
			result = int(func(c1==1, c2==1))
			print('{0} | {1} -> {2}'.format(c1, c2, result))




Here's a simple example:
>>> showTable('AND', lambda c1, c2: c1 and c2)
AND
0 | 0 -> 0
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1
>>> 



Have at!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1