Giter Site home page Giter Site logo

petarpeychev / pyboolean Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 11 KB

Parse infix boolean expressions to RPN, evaluate and generate truth tables.

License: The Unlicense

Python 100.00%
python boolean expression infix postfix evaluate solve truth table reverse

pyboolean's Introduction

pyboolean

Parse infix boolean expressions to postfix (RPN), evaluate and generate truth tables.

Compatibilities

  • Python 3.x
  • Any Operating System

Installation

pyboolean is published on PyPi, so you only need to run the following command:

$ pip install pyboolean

Usage

Creating a new boolean expression:

from pyboolean import BoolExpr
    
bool_expr = BoolExpr('1 or 0')

Printing the formatted version of the expression:

print(bool_expr)
# Output: "1 + 0"

Evaluating our boolean expression:

print(bool_expr.eval())
# Output: "1"

A boolean expression can also include variables:

bool_expr = BoolExpr('1 . m + ( 0 + ! n ) . m')

In order to evaluate an expression with variables, the eval() function can take arguments to replace them:

print(bool_expr.eval(1, 0))
# Output: "1"

For expressions with variables, one can generate a truth table like so:

print(bool_expr.truthtable())
# Output:
#╔════════════╗
#║  m  n  ┃ O ║
#║━━━━━━━━╋━━━║
#║  0  0  ┃ 0 ║
#║────────╂───║
#║  0  1  ┃ 0 ║
#║────────╂───║
#║  1  0  ┃ 1 ║
#║────────╂───║
#║  1  1  ┃ 1 ║
#╚════════════╝

Or if you need to manipulate the raw truth table data, generate a dictionary:

print(bool_expr.truthdict())
# Output: "{('0', '0'): '0', ('0', '1'): '0', ('1', '0'): '1', ('1', '1'): '1'}"

Finally, a note on formatting. When creating an expression, all spaces are ignored and many different versions of the operator symbols are accepted, so one can be very 'creative' with the input and not need to worry:

bool_expr = BoolExpr('p+1and   ¬m.(1∧p ) ∨notxand~0')
print(bool_expr)
# Output: "p + 1 . ! m . ( 1 . p ) + ! x . ! 0"

Full list of accepted operator symbols:

AND:

and . & ∧

OR:

or + | ∨

NOT:

not ! ~ ¬

pyboolean's People

Contributors

petarpeychev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.