I am trying to create "truth tables" for use in a Karnaugh map solver. The Karnaugh Map solver takes in the settings of the variables ABCD as a string of 4 numbers representing what they are set to. For example, ABCD could be anything from "0000" to "1111." The solver takes in the minterms (settings of ABCD which equal 1 in the truth table), but it outputs "*000" or other examples with * to represent that the Kmap has removed the first variable in the resulting equation. I am using this for an iterative process, so I need to be able to convert lists of these asterix-filled strings to a list of fully expanded strings :
[*000, 0*00] -> [0000, 1000, 0100]
The way I am currently doing it (with while loops) works but is very slow for the application I am using it for. It takes at least 10 minutes to get through, because I need to do this process around 1000 times (one for each piece of the dataset I am using) It goes quickly until around 430, when it slows significantly because of how long it takes to expand these complicated sequences. I can also confirm that it is not the Karnaugh map implementation I am using, because that solves the same long sequences instantly.
I'm not sure of a more pythonic way to do this which also accounts for things like "*11*", which have multiple spots where the expression needs to be expanded:
[*11*] -> [011*, 111*] -> [0110, 0111, 1110, 1111]
Any ideas which are efficient and pythonic?
My Code:
ast = True
while ast:
new_string_result = copy.deepcopy(string_result)
for i in range(len(string_result)):
for c, char in enumerate(string_result[i]):
if char == "*":
# replace with 0 and 1
new_string_result.append(string_result[i][:c] + "0" + string_result[i][c+1:])
new_string_result.append(string_result[i][:c] + "1" + string_result[i][c+1:])
if "*" in string_result[i]:
# remove original (only once, even if multiple *)
# print("Removing ", string_result[i])
new_string_result.remove(string_result[i])
# print("Kmap result during fix iter: ", new_string_result)
ast_found = False
for i in range(len(new_string_result)):
if "*" in new_string_result[i]:
ast_found = True
# print(ast_found)
ast = ast_found
string_result = new_string_result