Karnaugh map builder added
This commit is contained in:
parent
d79ea316a9
commit
a18d2012f2
|
@ -2,4 +2,5 @@
|
||||||
## Material que pode ser util ao longo do curso
|
## Material que pode ser util ao longo do curso
|
||||||
---
|
---
|
||||||
#### Lista de ferramentas:
|
#### Lista de ferramentas:
|
||||||
1.  [Latex CheatSheet](https://github.com/TiagoRG/uaveiro-leci/blob/master/tools/latex-cheatsheet)   ·   PDF Download (Não disponível)
|
1.  [Latex CheatSheet](https://github.com/TiagoRG/uaveiro-leci/blob/master/tools/latex-cheatsheet)   ·   PDF Download (Não disponível)
|
||||||
|
2.  [Karnaugh Map Builder](https://github.com/TiagoRG/uaveiro-leci/blob/master/tools/karnaugh-builder)
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
# Karnaugh Map Builder
|
||||||
|
### Script to build the karnaugh map for a given function.
|
||||||
|
---
|
||||||
|
## Installing
|
||||||
|
#### 1. Head over to [Release](https://github.com/TiagoRG/uaveiro-leci/releases/tag/kmb) and download the python file.
|
||||||
|
#### 2. Save it on any directory and then open the directory.
|
||||||
|
#### 3. Inside the directory, right click and choose "Open in terminal"
|
||||||
|
![- Unable to load image -](https://github.com/TiagoRG/uaveiro-leci/blob/karnaugh-map-builder/openInTerminal.png)
|
||||||
|
#### 4. Run the following command:
|
||||||
|
`python3 karnaugh.py bash`
|
||||||
|
#### 5. Relaunch the terminal and you will have the `karnaugh` command
|
||||||
|
---
|
||||||
|
#### Use `karnaugh usage` to know better how to use this tool.
|
||||||
|
#### If you want to change the location of the `karnaugh.py` file, move it to another directory, open that directory in terminal and run
|
||||||
|
`python3 karnaugh.py bash --reset`
|
|
@ -0,0 +1,120 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) == 1 or sys.argv[1] == 'usage':
|
||||||
|
print("""Usage:
|
||||||
|
|
||||||
|
karnaugh '<function>':
|
||||||
|
Prints the karnaugh map of the function.
|
||||||
|
Valid characters:
|
||||||
|
> Variables: a-z
|
||||||
|
> Operators: +, *, ~, (, ); You can't use ~ before ()
|
||||||
|
|
||||||
|
karnaugh bash:
|
||||||
|
Adds the command karnaugh to bash, so you can use it from anywhere.
|
||||||
|
|
||||||
|
karnaugh bash --reset:
|
||||||
|
Resets the command karnaugh in bash in case it's broken, so you can use it from anywhere.
|
||||||
|
|
||||||
|
karnaugh usage:
|
||||||
|
Prints this message.""")
|
||||||
|
return
|
||||||
|
if sys.argv[1] == "bash":
|
||||||
|
username = os.getlogin()
|
||||||
|
if len(sys.argv) == 2:
|
||||||
|
with open(f'/home/{username}/.bashrc', 'r') as bashrc:
|
||||||
|
for line in bashrc:
|
||||||
|
if line.startswith('alias karnaugh='):
|
||||||
|
print('Command already exists, try using, after opening a new terminal, karnaugh <function>')
|
||||||
|
return
|
||||||
|
os.system(f'echo "\n\nalias karnaugh=\'python3 {pathlib.Path(__file__).parent.absolute()}/karnaugh.py\'\n" >> \'/home/{username}/.bashrc\'')
|
||||||
|
print('Command added to bash, try using karnaugh <function>.\nIf it doesn\'t work, do karnaugh bash --reset.')
|
||||||
|
elif sys.argv[2] == '--reset':
|
||||||
|
with open(f'/home/{username}/.bashrc', 'r') as bashrc:
|
||||||
|
lines = bashrc.readlines()
|
||||||
|
linesDict = {}
|
||||||
|
index = 1
|
||||||
|
for line in lines:
|
||||||
|
linesDict[index] = line
|
||||||
|
index += 1
|
||||||
|
for key in linesDict:
|
||||||
|
if linesDict[key].startswith('alias karnaugh='):
|
||||||
|
del linesDict[key]
|
||||||
|
break
|
||||||
|
string = ''
|
||||||
|
for key in linesDict:
|
||||||
|
string += linesDict[key]
|
||||||
|
string += f'\n\nalias karnaugh=\'python3 {pathlib.Path(__file__).parent.absolute()}/karnaugh.py\'\n'
|
||||||
|
with open(f'/home/{username}/.bashrc', 'w') as bashrc:
|
||||||
|
bashrc.write(string)
|
||||||
|
print('Command readded to bash, try using, after opening a new terminal, karnaugh <function>.')
|
||||||
|
else:
|
||||||
|
function = sys.argv[1]
|
||||||
|
|
||||||
|
validVariables = r"[a-z]"
|
||||||
|
validOperators = ['+', '*', '(', ')', '~', ' ', '0', '1']
|
||||||
|
|
||||||
|
for char in function:
|
||||||
|
if not (re.match(validVariables, char) or char in validOperators):
|
||||||
|
print("Invalid function, use 'karnaugh usage' to see the valid characters.")
|
||||||
|
return
|
||||||
|
|
||||||
|
variables = re.findall(validVariables, function)
|
||||||
|
variables = list(dict.fromkeys(variables))
|
||||||
|
variables.sort()
|
||||||
|
|
||||||
|
karnaughMap = getMap(variables, function)
|
||||||
|
printMap(variables, karnaughMap)
|
||||||
|
|
||||||
|
|
||||||
|
def getMap(variables, function):
|
||||||
|
karnaughMap = {}
|
||||||
|
for n in range(2 ** len(variables)):
|
||||||
|
tempFunction = function
|
||||||
|
binary = bin(n)[2:]
|
||||||
|
binary = '0' * (len(variables) - len(binary)) + binary
|
||||||
|
index = 0
|
||||||
|
for var in tempFunction:
|
||||||
|
if index == len(tempFunction):
|
||||||
|
break
|
||||||
|
if var in "01" and tempFunction[index - 1] == '~':
|
||||||
|
tempFunction = tempFunction.replace('~' + var, str(1 - int(var)))
|
||||||
|
if var in variables:
|
||||||
|
tempFunction = tempFunction.replace(tempFunction[index], binary[variables.index(var)]) if tempFunction[index - 1] != '~' else tempFunction.replace(f"~{tempFunction[index]}", str(int(not int(binary[variables.index(tempFunction[index])])))+" ")
|
||||||
|
index += 1
|
||||||
|
try:
|
||||||
|
karnaughMap[binary] = eval(tempFunction)
|
||||||
|
except:
|
||||||
|
print("Invalid function, use 'karnaugh usage' to see the valid characters.")
|
||||||
|
return
|
||||||
|
if karnaughMap[binary] > 1:
|
||||||
|
karnaughMap[binary] = 1
|
||||||
|
|
||||||
|
return karnaughMap
|
||||||
|
|
||||||
|
|
||||||
|
def printMap(variables, karnaughMap):
|
||||||
|
for var in variables:
|
||||||
|
print("|---", end='')
|
||||||
|
print('|-----|')
|
||||||
|
for var in variables:
|
||||||
|
print("| " + var, end=' ')
|
||||||
|
print(f"{'| f() |'}")
|
||||||
|
for var in variables:
|
||||||
|
print("|---", end='')
|
||||||
|
print('|-----|')
|
||||||
|
for key, value in karnaughMap.items():
|
||||||
|
for char in key:
|
||||||
|
print("| " + char, end=' ')
|
||||||
|
print(f"| {value} |")
|
||||||
|
for var in variables:
|
||||||
|
print("|---", end='')
|
||||||
|
print('|-----|')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Binary file not shown.
After Width: | Height: | Size: 47 KiB |
Loading…
Reference in New Issue