def karta_karno(match, v): mas = [] mas0 = [] kstr = "" for i in range(len(v)): if v[i][-1] == True: mas.append(i) for i in range(len(v)): if v[i][-1] == False: mas0.append(i) if len(mas) == len(v): return "TRUE" elif len(mas) == 0: return "FALSE" arr = qm.qm(ones=mas, zeros=mas0) print(arr) for i in range(len(arr)): if i == len(arr) - 1: kstr = kstr + check(match, arr[i]) else: kstr = kstr + check(match, arr[i]) + " ⋁ " return kstr
def deal_msg(msg): text = msg.text.split('###') if len(text) != 2: return if text[0] == 'clear': print (msg.sender.puid + msg.text) subprocess.Popen('python wechat_clear.py', stdout=subprocess.PIPE, stderr=subprocess.PIPE) time.sleep(2) msg.sender.send_image('image') elif text[0] == 'name': print (msg.sender.puid + msg.text) parm = text[1].split(':') if len(parm) != 2: return qm.qm(parm[0], parm[1]) time.sleep(3) image_name = parm[1] + '.jpg' print ('send image start') try: msg.sender.send_image(image_name) except Exception as e: print (e) print ('send image ok') time.sleep(2)
def compute_logic_expression_from_truth_table(labels, truth_values): input_size = len(labels) - 1 if len(truth_values) < 2**input_size: return "Not fully polarized" ones = [] zeros = [] dc = [] i = 0 while i < len(truth_values): tmp_val = 0 for j in range(input_size): tmp_val += truth_values[i][j] * (2**(input_size - 1 - j)) if i < len(truth_values) - 1: if truth_values[i][:-1] == truth_values[ i + 1][:-1] and truth_values[i][-1] != truth_values[i + 1][-1]: dc.append(tmp_val) i += 2 continue if truth_values[i][-1] == 1: ones.append(tmp_val) else: zeros.append(tmp_val) i += 1 terms = qm.qm(ones=ones, zeros=zeros, dc=dc) ret = [] for term in terms: tmp = [] for i in range(input_size): if term[i] == "1": tmp.append(labels[i]) elif term[i] == "0": tmp.append(labels[i] + "^") ret.append("*".join(tmp)) return labels[-1] + " = " + " + ".join(ret)
def function_simplification(on_state, off_state): dontcares = [] pos_imp = qm2.qm(ones=on_state, zeros=off_state, dc=dontcares) return pos_imp
def get_simplified_expression(ornl=[]): result = qm.qm(ones=ornl) result_f = list( map(lambda x: list(map(lambda y: int(y) if y != 'X' else -1, x)), result)) return result_f
def proceed(): h = hashlib.md5(password.encode()) h2 = h.hexdigest() # print("Hash value is\n",h2,"\n\n") # HASH PART 1 # print("First half of hashing is\n",hash1,"\n\n") hash1 = h2[0:len(h2) // 2] # HASH PART 2 # print("Second half of hashing is\n",hash2,"\n\n") hash2 = h2[len(h2) // 2 if len(h2) % 2 == 0 else ((len(h2) // 2) + 1):] # HASH PART 1 TO BINARY binary = bin(int(hash1, 16)) binary = binary[2:] # print("Printing the binary\n",binary,"\n\n") binary = binary[2:] # TO DO Padding n = 4 splits = [(binary[i:i + n]) for i in range(0, len(binary), n)] pos = [ "2", "3", "1", "3", "2", "3", "1", "3", "2", "2", "3", "2", "1", "3", "2", "0" ] # static for now,It will be dynamic for each user final_bin = "" for i in range(0, len(splits)): temp1 = int(pos[i]) temp2 = str(splits[i]) f = temp2[temp1] final_bin = final_bin + f if len(final_bin) != 16: final_bin = final_bin + "0" # PART 2 AES ENCRYPTION(ENCRYPTING HASH 2 USING HASH1 AS KEY) passw = bytes(hash1, encoding='utf-8') # b"password" hash1 is the key salt = os.urandom(16) kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000, backend=default_backend()) key = base64.urlsafe_b64encode(kdf.derive(passw)) f = Fernet(key) # hash2 is the input to be encoded token = f.encrypt(bytes(hash2, encoding='utf-8')) temporary = token.decode() # CODE TO WRITE AES VALUE OF USER TO A TEXT FILE os.chdir(username) filename1 = "encrypt.txt" f = open(filename1, "w") f.write(token.decode()) # BYTES TO STRING f.close() fills = open("key.txt", "w") fills.write(key.decode()) # BYTES TO STRING fills.close() # PART 3 ip = int(final_bin, 2) # binary to hexa mt = [] for i in range(0, 65536): mt.append(i) mt.remove(ip) n = 128 a = [mt[i * n:(i + 1) * n] for i in range((len(mt) + n - 1))] patterns = [] dt = [] dt.append(0) dt.append(65535) count = 0 for i in range(0, 512): print("\niteration--> ", count) count = count + 1 patterns.extend(qm(a[i], dt)) filename2 = "pattern.txt" with open(filename2, 'w') as f: for item in patterns: f.write("%s\n" % item) print("\nwriting file\n") print(os.getcwd()) location = os.getcwd() # MYSQL CODE import string import mysql import mysql.connector config = { 'user': '******', 'password': '', 'host': 'localhost', 'database': 'semester-8', 'raise_on_warnings': True, } link = mysql.connector.connect(**config) mycursor = link.cursor(buffered=True) sql = "INSERT INTO manage_users(username,encrypted_string,path)VALUES(%s,%s,%s)" val = (username, temporary, location) mycursor.execute(sql, val) print(mycursor.rowcount, "record inserted") mycursor.execute("SELECT * FROM manage_users ") name = mycursor.fetchall() print(name, end="\n\n") mycursor.execute("SELECT * FROM manage_users") name2 = mycursor.fetchall() for i in range(len(name2)): print(name2[i], end="\n\n") link.commit()
x = 0 y = 0 ## Create binary values for truth-table and make sure that it can be optimized while True: x = random.randrange(0, 65535) #possible solutions for a 4 bit truth table #convert to a array of '1' and '0' y = bin(x) y = y[2:] y = y.zfill(16) #find all ones and feed it to Quine–McCluskey algorithm indices = [i for i, x in enumerate(y) if x == '1'] optimized = qm.qm(ones=indices) #find out how many optimizations possile xes = [] for i in optimized: xes.append(i.count("X")) #we want at least one possible optimization if (max(xes) == 0 or len(optimized) > 4): continue else: break ############################## ## PARAMETER SPECIFYING TASK## ##############################
def simplify(formula): ones = minterms(formula) rober_terms = qm.qm(ones=ones) newfor = robert_to_travis(rober_terms, formula.indexes_involved) return newfor
import qm print(qm.qm(ones=[1, 2, 5], dc=[0, 7]))
def qmAlgorithm(one_list, zero_list): """ perform qm algoithm to reduce the complexibility of a condition list :param xxx_list: the condition list should be (.. & ..) | (.. & .. & ..) | .., each cluase is a string item in xxx_list :return: a simplified condition_list """ def fillInStar(fill_list, mask_list): new_mask = copy.deepcopy(mask_list) index_fill = 0 for index_mask in range(len(mask_list)): if mask_list[index_mask] == '*': new_mask[index_mask] = fill_list[index_fill] index_fill = index_fill + 1 return new_mask var_list = list() for clause in one_list: var_list = var_list + clause.split(' & ') for clause in zero_list: var_list = var_list + clause.split(' & ') var_list = list(set([item[1:] if item.startswith('!') else item for item in var_list])) var_num = len(var_list) one = list() zero = list() for condition in one_list: condition_list = condition.split(' & ') mask = ['*'] * var_num for cond in condition_list: if cond.startswith('!'): mask[var_list.index(cond[1:])] = '0' else: mask[var_list.index(cond)] = '1' num_star = sum([int(entry == '*') for entry in mask]) for fill_list in itertools.product(['0', '1'], repeat=num_star): boolean_list = fillInStar(fill_list, mask) one_index = int(''.join(boolean_list), 2) one.append(one_index) for condition in zero_list: condition_list = condition.split(' & ') mask = ['*'] * var_num for cond in condition_list: if cond.startswith('!'): mask[var_list.index(cond[1:])] = '0' else: mask[var_list.index(cond)] = '1' num_star = sum([int(entry == '*') for entry in mask]) for fill_list in itertools.product(['0', '1'], repeat=num_star): boolean_list = fillInStar(fill_list, mask) zero_index = int(''.join(boolean_list), 2) zero.append(zero_index) dc = [dc_index for dc_index in range(2 ** var_num) if dc_index not in one and dc_index not in zero] one = sorted(one) zero = sorted(zero) dc = sorted(dc) mask_list = qm.qm(ones=one, zeros=zero, dc=dc) result = list() for mask in mask_list: cond_list = list() for index, value in zip(range(len(mask)), mask): if value == '1': cond_list.append(var_list[index]) elif value == '0': cond_list.append('!'+var_list[index]) cond = ' & '.join(cond_list) result.append(cond) return result
import qm import sys from math import * from minimize_final import * moduleNames = [] modules = map(__import__, moduleNames) for func in sys.stdin: (ones, dcs) = parseFunc(func) numvars = int(ceil(log(max(ones+dcs) + 1, 2))) solutions = [] # Library implementation solutions += [nicePrint(qm.qm(ones=ones, dc=dcs), numvars, sop=True)] # My implementation solutions += [nicePrint(minimize(ones, dcs, numvars), numvars, sop=True)] for module in modules: solutions += [nicePrint(module.minimize(ones, dcs, numvars), numvars, sop=True)] for i, sol in enumerate(solutions): solution = sol.split("+") solution.sort() solutions[i] = "+".join(solution) sys.stdout.write("%s (reference answer)\n" % solutions[0]) sys.stdout.write("%s (my answer)\n" % solutions[1])