I've been doing a lot of Python lately because it maps well to work related project like Cisco ACI. I've also spent a bit of time learning about Swift and really like how it's fairly easy to switch between the two even with my limited programming capacity.

Anyway, on to the Boolean Algebra. I have implemented functions of this a few times when checking state for various programs, but didn't know it had a cool name like Boolean Algebra. While writing some code this morning in checkio.org I came across this as one of the project and thought I would do it in swift also just to see if it was easy to port the logic between the two as well as learn the other operators.

Below you will find two examples, the first in Python and the second in Swift (playground). I've done them slightly different in how I'm testing the outcome while still trying to keep it very simple. It's very possible to write these without the if statements by using a dictionary and it probably what you should do if you are going to put this entire thing in real code but I'm not here to tell you how to write code, just give you the answers on calculate Boolean Algebra.

**Python:**

#!/usr/bin/env python

operations = ("conjunction", "disjunction", "implication", "exclusive", "equivalence")

def booleanArith (operation, x, y):

if operation == "conjunction":

return x & y

if operation == "disjunction":

return (x or y)

if operation == "implication":

return (not(x) or y)

if operation == "exclusive":

return x != y

if operation == "equivalence":

return x == y

def runThem(x, y):

for operation in operations:

result = (booleanArith(operation, x, y))

print ("{} - {}, {} == {}".format(operation, x, y, result))

print ("")

# Testing code

x = False

y = False

runThem(x, y)

x = True

y = False

runThem(x, y)

x = False

y = True

runThem(x, y)

x = True

y = True

runThem(x, y)

**And then Swift:**

//: Playground - noun: a place where people can play

func booleanArith(operation: String, x: Bool, y: Bool) -> Bool {

if operation == "conjunction" {

return (x && y)

}

if operation == "disjunction"{

return (x || y)

}

if operation == "implication"{

return !x || y

}

if operation == "exclusive"{

return x != y

}

if operation == "equivalence"{

return x == y

}

return false

}

// Testing code

print (booleanArith("conjunction", x: false, y: false))

print (booleanArith("disjunction", x: false, y: false))

print (booleanArith("implication", x: false, y: false))

print (booleanArith("exclusive", x: false, y: false))

print (booleanArith("equivalence", x: false, y: false))

print (booleanArith("conjunction", x: true, y: false))

print (booleanArith("disjunction", x: true, y: false))

print (booleanArith("implication", x: true, y: false))

print (booleanArith("exclusive", x: true, y: false))

print (booleanArith("equivalence", x: true, y: false))

print (booleanArith("conjunction", x: false, y: true))

print (booleanArith("disjunction", x: false, y: true))

print (booleanArith("implication", x: false, y: true))

print (booleanArith("exclusive", x: false, y: true))

print (booleanArith("equivalence", x: false, y: true))

print (booleanArith("conjunction", x: true, y: true))

print (booleanArith("disjunction", x: true, y: true))

print (booleanArith("implication", x: true, y: true))

print (booleanArith("exclusive", x: true, y: true))

print (booleanArith("equivalence", x: true, y: true))

## No comments:

Post a Comment