Skip to content

Instantly share code, notes, and snippets.

@paridin
Created November 12, 2015 05:48
Show Gist options
  • Save paridin/b7d5a4e8551d5ae48715 to your computer and use it in GitHub Desktop.
Save paridin/b7d5a4e8551d5ae48715 to your computer and use it in GitHub Desktop.
Ejemplo para representar las tablas de verdad.
"""
Tablas de verdad en python
Replicaremos las tablas de la verdad de la siguiente tabla
https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fhtml.rincondelvago.com%2F000264780.jpg&f=1
para la creación de las tablas para su representación gráfica utilizare pandas
tablas de verdad a programar AND, NAND, OR, NOR, EXOR
Desarrollado 11/11/15
Roberto Estrada
Salida
A B AND NAND OR NOR EXOR
0 0 0 F V F V F
1 0 1 F V V F V
2 1 0 F V V F V
3 1 1 V F V F F
funciona con python3 y pandas
brew install python3
pip3 install pandas
"""
import pandas as pd
from collections import OrderedDict
class Tabla:
def int2bool(self, numero):
"""
Recibe un numero 0, 1 y retorna un booleano
"""
if numero == 0:
return False
elif numero == 1:
return True
def int2str(self, numero):
"""
Recibe un numero 0, 1 y retorna un String F, V
"""
if numero == 0:
return 'F'
elif numero == 1:
return 'V'
def bool2str(self, boolean):
if boolean:
return 'V'
return 'F'
def salida_and(self, *args):
suma = 0
for i in args:
suma += i
if suma // len(args) == 0:
return False
return True
def salida_nand(self, *args):
suma = 0
for i in args:
suma += i
if suma // len(args) == 0:
return True
return False
def salida_or(self, *args):
suma = 0
for i in args:
suma += i
if suma == 0:
return False
return True
def salida_nor(self, entrada1, entrada2):
if not entrada1 and not entrada2:
return True
return False
def salida_exor(self, *args):
suma = 0
for i in args:
suma += i
if suma % len(args) == 1:
return True
return False
def crear_tabla(self):
"""
Creación de tabla de la verdad con dos entradas
"""
tabla = OrderedDict()
tabla['A'] = []
tabla['B'] = []
tabla['AND'] = []
tabla['NAND'] = []
tabla['OR'] = []
tabla['NOR'] = []
tabla['EXOR'] = []
for i in range(2):
for j in range(2):
# print(type(i), type(self.numero2binario(i)))
tabla['A'].append(i)
tabla['B'].append(j)
tabla['AND'].append(self.bool2str(self.salida_and(i, j)))
tabla['NAND'].append(self.bool2str(self.salida_nand(i, j)))
tabla['OR'].append(self.bool2str(self.salida_or(i, j)))
tabla['NOR'].append(self.bool2str(self.salida_nor(i, j)))
tabla['EXOR'].append(self.bool2str(self.salida_exor(i, j)))
return tabla
if __name__ == '__main__':
t = Tabla()
print(pd.DataFrame(t.crear_tabla()))
# funciona para n argumentos el cálculo.
print(t.bool2str(t.salida_and(1,1,1,1,1,0)))
print(t.bool2str(t.salida_and(1,1,1,1,1,1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment