diff --git a/.idea/APTP2022.iml b/.idea/APTP2022.iml index d0876a7..74d515a 100644 --- a/.idea/APTP2022.iml +++ b/.idea/APTP2022.iml @@ -1,7 +1,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index d56657a..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/2by2.py b/2by2.py new file mode 100644 index 0000000..78f9958 --- /dev/null +++ b/2by2.py @@ -0,0 +1,45 @@ +# t 라는 list가 입력되었다고 간주 +# 2 * 2 list임 + +def get_2by2(t): + did = [['yet', 'yet'], + ['yet', 'yet']] + ans = [] + + for i in range(2): + for j in range(2): + if t[i][j] == 1 and did[i][j] == 'yet': + did[i][j] = 'done' + small_ans = [[i, j]] + + # 상하좌우 + if t[(i-1+2) % 2][j] == 1: + if [(i-1+2) % 2, j] not in small_ans: + small_ans.append([(i-1+2) % 2, j]) + did[(i-1+2) % 2][j] = 'done' + if t[(i+1) % 2][j] == 1: + if [(i+1) % 2, j] not in small_ans: + small_ans.append([(i+1) % 2, j]) + did[(i+1) % 2][j] = 'done' + if t[i][(j-1+2) % 2] == 1: + if [i, (j-1+2) % 2] not in small_ans: + small_ans.append([i, (j-1+2) % 2]) + did[i][(j-1+2) % 2] = 'done' + if t[i][(j+1) % 2] == 1: + if [i, (j+1) % 2] not in small_ans: + small_ans.append([i, (j+1) % 2]) + did[i][(j+1) % 2] = 'done' + + if len(small_ans) & (len(small_ans) - 1) == 0: + ans.append(small_ans) + else: + while len(small_ans) & (len(small_ans) - 1) != 0: + index = small_ans.pop() + did[index[0]][index[1]] = 'yet' + if small_ans is not []: + ans.append(small_ans) + + return ans + + +print(get_2by2([[1, 1], [1, 1]])) diff --git "a/3\352\260\234\354\247\234\353\246\254.py" "b/3\352\260\234\354\247\234\353\246\254.py" new file mode 100644 index 0000000..eb263dd --- /dev/null +++ "b/3\352\260\234\354\247\234\353\246\254.py" @@ -0,0 +1,30 @@ +def get_3by3(t): + did = [['yet', 'yet'], + ['yet', 'yet'], + ['yet', 'yet'], + ['yet', 'yet']] + ans = [] + + for i in range(4): + for j in range(2): + if t[i][j] == 1 and did[i][j] == 'yet': + did[i][j] = 'done' + small_ans = [[i, j]] + + # 상하좌우 + if t[i - 1][j] == 1: + small_ans.append([i - 1, j]) + did[i-1][j] = 'done' + if t[i + 1][j] == 1: + small_ans.append([i + 1, j]) + did[i+1][j] = 'done' + if t[i][j - 1] == 1: + small_ans.append([i, j-1]) + did[i][j - 1] = 'done' + if t[i][j + 1] == 1: + small_ans.append([i, j+1]) + did[i][j + 1] = 'done' + + ans.append(small_ans) + + return ans diff --git a/GUI.py b/GUI.py new file mode 100644 index 0000000..bf9e1cd --- /dev/null +++ b/GUI.py @@ -0,0 +1,129 @@ +import pygame +import sys +import TABLE + +color = { + 'white': (255, 255, 255), + 'black': (0, 0, 0), + 'red': (255, 0, 0), + 'green': (0, 255, 0), + 'blue': (0, 0, 255) +} + +alphabet = {pygame.K_a: 'a', pygame.K_b: 'b', pygame.K_c: 'c', pygame.K_d: 'd', pygame.K_e: 'e', + pygame.K_f: 'f', pygame.K_g: 'g', pygame.K_h: 'h', pygame.K_i: 'i', pygame.K_j: 'j', + pygame.K_k: 'k', pygame.K_l: 'l', pygame.K_m: 'm', pygame.K_n: 'n', pygame.K_o: 'o', + pygame.K_p: 'p', pygame.K_q: 'q', pygame.K_r: 'r', pygame.K_s: 's', pygame.K_t: 't', + pygame.K_u: 'u', pygame.K_v: 'v', pygame.K_w: 'w', pygame.K_x: 'x', pygame.K_y: 'y', + pygame.K_z: 'z'} + +number = {pygame.K_0: '0', pygame.K_1: '1', pygame.K_2: '2', pygame.K_3: '3', pygame.K_4: '4', + pygame.K_5: '5', pygame.K_6: '6', pygame.K_7: '7', pygame.K_8: '8', pygame.K_9: '9'} + +command = {pygame.K_BACKSPACE: 'backspace', pygame.K_RETURN: 'return', pygame.K_SPACE: ' '} + +direction = {pygame.K_UP: 'up', pygame.K_DOWN: 'down', pygame.K_LEFT: 'left', pygame.K_RIGHT: 'right'} + +key_l = [alphabet, number, command, direction] + + +class Screen(TABLE.Table): + """ + Responsibility on Displaying Screen + """ + + def __init__(self, screen_size, table, table_size, cell_size, cell_border): + """ + Screen Information + :param screen_size: tuple[int, int] + :param table: list[list[int]] + :param table_size: tuple[int, int] + """ + super().__init__(table, table_size) + pygame.init() + self.screen_size = screen_size + self.screen = pygame.display.set_mode(screen_size) + self.cell_size = cell_size + self.cell_border = cell_border + + def ShowText(self, word, text_size, font_color, text_position): + """ + Function to Show Text on Screen + :param word: str + :param text_size: int + :param font_color: str + :param text_position: tuple[int, int] + :return: none + """ + font = pygame.font.SysFont("notosanscjkk", text_size) + word = font.render(str(word), True, color[font_color]) + self.screen.blit(word, text_position) + + def ShowScreen(self, screen_color): + """ + Function to Show Screen + :param screen_color: str + :return: none + """ + self.screen.fill(color[screen_color]) + + def ShowTable(self, cell_color): + """ + Function to Show Table + :param cell_color: str + :return: none + """ + # print('table screen') + start_position = (self.screen_size[0]/2 - self.table_size[1]*self.cell_size/2.0, + self.screen_size[1]/2 - self.table_size[0]*self.cell_size/2.0) + + for i in range(self.table_size[0]): + for j in range(self.table_size[1]): + adjust_position = (start_position[0] + j * self.cell_size - self.cell_border * j, + start_position[1] + i * self.cell_size - self.cell_border * i) + pygame.draw.rect(self.screen, color[cell_color], + (adjust_position[0], + adjust_position[1], + self.cell_size, + self.cell_size), + self.cell_border) + self.ShowText(self.table[i][j], self.cell_size, cell_color, + (adjust_position[0] + self.cell_size/3, + adjust_position[1] + self.cell_size/6)) + + def ShowDecisionPage(self, cell_color, start_position): + # print('decision page') + for i in range(1, 4): + adjust_position = (start_position[0] + i * self.cell_size - self.cell_border * i - 5*self.cell_size/2, + start_position[1] - self.cell_size) + pygame.draw.rect(self.screen, color[cell_color], + (adjust_position[0], + adjust_position[1], + self.cell_size, + self.cell_size), + self.cell_border) + self.ShowText(i + 1, self.cell_size, cell_color, + (adjust_position[0] + self.cell_size/3, + adjust_position[1] + self.cell_size/6)) + self.ShowText('Choose number of inputs', int(self.cell_size/2), cell_color, + (start_position[0] / 3, + start_position[1]/3)) + + +if __name__ == '__main__': + t = [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]] + a = Screen((1000, 1000), t, (4, 4), 150, 3) + + while True: + # for event in pygame.event.get(): + # if event.type == pygame.QUIT: + # sys.exit() + a.ShowScreen('white') + a.ShowDecisionPage('black', (a.screen_size[0] / 2, a.screen_size[1]/2)) + + # a.ShowTable(150, 'black', 3, (100, 100)) + # a.ShowText("Hi There", 90, 'black', (20, 20)) + pygame.display.flip() diff --git a/INTERACTION.py b/INTERACTION.py new file mode 100644 index 0000000..79678ca --- /dev/null +++ b/INTERACTION.py @@ -0,0 +1,168 @@ +from GUI import Screen, pygame, sys, color + + +class Interaction(Screen): + """ + Responsibility on Interaction with User + """ + + def __init__(self, screen_size, table, table_size, cell_size, cell_border): + super().__init__(screen_size, table, table_size, cell_size, cell_border) + + def MouseClick(self, table_position): + (mouseX, mouseY) = pygame.mouse.get_pos() + for i in self.table_size[0]: + for j in self.table_size[1]: + adjust_position = (table_position[0] + j * self.cell_size - j * self.cell_border, + table_position[1] + i * self.cell_size - i * self.cell_border) + if 0 < mouseX - adjust_position[0] < self.cell_size and \ + 0 < mouseY - adjust_position[1] < self.cell_size: + self.table[i][j] = 1 + + def GetDecision(self, start_position): + (mouseX, mouseY) = pygame.mouse.get_pos() + for i in range(1, 4): + adjust_position = (start_position[0] + i * self.cell_size - self.cell_border * i - 5 * self.cell_size / 2, + start_position[1] - self.cell_size) + if 0 < mouseX - adjust_position[0] < self.cell_size and 0 < mouseY - adjust_position[1] < self.cell_size: + return i + 1 + return -1 + + def GetInput(self, start_position): + (mouseX, mouseY) = pygame.mouse.get_pos() + for i in range(self.table_size[0]): + for j in range(self.table_size[1]): + adjust_position = (start_position[0] + j * self.cell_size - self.cell_border * j, + start_position[1] + i * self.cell_size - self.cell_border * i + self.cell_size/2) + if 0 < mouseX - adjust_position[0] < self.cell_size and 0 < mouseY - adjust_position[1] < self.cell_size: + if self.table[i][j] == 1: + self.table[i][j] = 0 + elif self.table[i][j] == 0: + self.table[i][j] = 1 + + def IsEvent(self, mode, start_position, event_list): + # print('get event') + for input_event in event_list: + if input_event.type == pygame.MOUSEBUTTONDOWN: + if mode == 'number': + return self.GetDecision(start_position) + if mode == 'table': + self.GetInput(start_position) + # return self.MouseClick(start_position) + + def BackButton(self): + # print('show back button') + pygame.draw.rect(self.screen, color['black'], + (30, + 30, + self.cell_size / 2, + self.cell_size / 2), + self.cell_border) + pygame.draw.polygon(self.screen, color['black'], + ((30 + 10, 30 + self.cell_size / 4), + (30 + self.cell_size / 2 - 10, 30 + 10), + (30 + self.cell_size / 2 - 10, 30 + self.cell_size / 2 - 10) + )) + + def AnswerButton(self, event_list_ans): + pygame.draw.rect(self.screen, color['black'], + (self.screen_size[0] - 230, + self.screen_size[1] - 100, + 190, + 60), + self.cell_border) + self.ShowText("Answer", 60, 'black', (self.screen_size[0] - 230 + 10, self.screen_size[1] - 100 + 10)) + + for ans_event in event_list_ans: + if ans_event.type == pygame.MOUSEBUTTONDOWN: + (mouseX, mouseY) = pygame.mouse.get_pos() + if 0 < mouseX - (self.screen_size[0] - 230) < 190 and 0 < mouseY - (self.screen_size[1] - 100) < 60: + return True + + return False + + +# if __name__ == '__main__': +# t = [ +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0] +# ] +# a = Interaction((800, 800), t, (4, 4), 130, 3) +# screen_type = 'decide' +# +# while True: +# el = pygame.event.get() +# +# for end_event in el: +# if end_event.type == pygame.QUIT: +# print('program ended') +# sys.exit() +# a.ShowScreen('white') +# +# if screen_type == 'decide': +# a.ShowDecisionPage('black', (a.screen_size[0] / 2, a.screen_size[1] / 2)) +# decision = a.IsEvent('number', (a.screen_size[0] / 2, a.screen_size[1] / 2), el) +# if decision is not None: +# print(decision) +# if decision != -1: +# if decision == 2: +# a.table = [ +# [0, 0], +# [0, 0] +# ] +# a.table_size = (2, 2) +# if decision == 3: +# a.table = [ +# [0, 0], +# [0, 0], +# [0, 0], +# [0, 0] +# ] +# a.table_size = (4, 2) +# if decision == 4: +# a.table = [ +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0] +# ] +# a.table_size = (4, 4) +# if decision == 5: +# a.table = [ +# [ +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0] +# ], +# [ +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0], +# [0, 0, 0, 0] +# ] +# ] +# a.table_size = (4, 4) +# screen_type = 'done' +# elif screen_type == 'done': +# a.BackButton() +# input_done = a.AnswerButton(el) +# if input_done: +# pass +# +# stp = (a.screen_size[0] / 2 - a.table_size[1] * a.cell_size / 2.0, +# a.screen_size[1] / 2 - a.table_size[0] * a.cell_size / 2.0 - a.cell_size / 2) +# +# for back_event in el: +# if back_event.type == pygame.MOUSEBUTTONDOWN: +# (mx, my) = pygame.mouse.get_pos() +# if 30 < mx < 30 + 150 / 2 and 30 < my < 30 + 150 / 2: +# screen_type = 'decide' +# decision = None +# +# a.IsEvent('table', stp, el) +# a.ShowTable('black') +# +# pygame.display.flip() diff --git a/Junseo.py b/Junseo.py new file mode 100644 index 0000000..4adf683 --- /dev/null +++ b/Junseo.py @@ -0,0 +1,35 @@ +def is_impli_two(a, b): #minterm 2개가 implicant인지 판별 + A8 = a // 8 + A4 = (a % 8) // 4 + A2 = (a % 4) // 2 + A1 = (a % 2) // 1 + + B8 = b // 8 + B4 = (b % 8) // 4 + B2 = (b % 4) // 2 + B1 = (b % 2) // 1 + print(A8, A4, A2, A1, B8, B4, B2, B1) + + dif = abs(A8 - B8) + abs(A4 - B4) + abs(A2 - B2) + abs(A1 - B1) + if dif == 1: + return 1 + else: + return 0 + +def get_minterm_4vari(): #변수가 4개일 때 값이 1인 minterm 받기 + minterm = [] # 1인 minterm의 list + impli = [] # implicant가 모여있는 list + + print("값이 1인 midterm을 차례대로 입력하시오(종료시 -1) :") + while True: + i = int(input()) + if i == -1: + break + elif (i < 0) or (i > 15): + print("0~16" + "5값을 다시 입력하세요") + continue + minterm.append(i) + impli.append(i) # 1개만으로 이미 implicant이므로 바로 추가한다. + + diff --git a/LAST_RESORT.py b/LAST_RESORT.py new file mode 100644 index 0000000..3296b69 --- /dev/null +++ b/LAST_RESORT.py @@ -0,0 +1,351 @@ + +from INTERACTION import Interaction, pygame, sys + +def mul(x,y): # Multiply 2 minterms + res = [] + for i in x: + if i+"'" in y or (len(i)==2 and i[0] in y): + return [] + else: + res.append(i) + for i in y: + if i not in res: + res.append(i) + return res + +def multiply(x,y): # Multiply 2 expressions + res = [] + for i in x: + for j in y: + tmp = mul(i,j) + res.append(tmp) if len(tmp) != 0 else None + return res + +def refine(my_list,dc_list): # Removes don't care terms from a given list and returns refined list + res = [] + for i in my_list: + if int(i) not in dc_list: + res.append(i) + return res + +def findEPI(x): # Function to find essential prime implicants from prime implicants chart + res = [] + for i in x: + if len(x[i]) == 1: + res.append(x[i][0]) if x[i][0] not in res else None + return res + +def findVariables(x): # Function to find variables in a meanterm. For example, the minterm --01 has C' and D as variables + var_list = [] + for i in range(len(x)): + if x[i] == '0': + var_list.append(chr(i+65)+"'") + elif x[i] == '1': + var_list.append(chr(i+65)) + return var_list + +def flatten(x): # Flattens a list + flattened_items = [] + for i in x: + flattened_items.extend(x[i]) + return flattened_items + +def findminterms(a): #Function for finding out which minterms are merged. For example, 10-1 is obtained by merging 9(1001) and 11(1011) + gaps = a.count('-') + if gaps == 0: + return [str(int(a,2))] + x = [bin(i)[2:].zfill(gaps) for i in range(pow(2,gaps))] + temp = [] + for i in range(pow(2,gaps)): + temp2,ind = a[:],-1 + for j in x[0]: + if ind != -1: + ind = ind+temp2[ind+1:].find('-')+1 + else: + ind = temp2[ind+1:].find('-') + temp2 = temp2[:ind]+j+temp2[ind+1:] + temp.append(str(int(temp2,2))) + x.pop(0) + return temp + +def compare(a,b): # Function for checking if 2 minterms differ by 1 bit only + c = 0 + for i in range(len(a)): + if a[i] != b[i]: + mismatch_index = i + c += 1 + if c>1: + return (False,None) + return (True,mismatch_index) + +def removeTerms(_chart,terms): # Removes minterms which are already covered from chart + for i in terms: + for j in findminterms(i): + try: + del _chart[j] + except KeyError: + pass + + +####################################### + + +t = [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] +] +a = Interaction((800, 800), t, (4, 4), 130, 3) +screen_type = 'decide' + +while True: + el = pygame.event.get() + + for end_event in el: + if end_event.type == pygame.QUIT: + print('program ended') + sys.exit() + a.ShowScreen('white') + + decision = 0 + if screen_type == 'decide': + a.ShowDecisionPage('black', (a.screen_size[0] / 2, a.screen_size[1] / 2)) + decision = a.IsEvent('number', (a.screen_size[0] / 2, a.screen_size[1] / 2), el) + if decision is not None: + print(decision) + if decision != -1: + if decision == 2: + a.table = [ + [0, 0], + [0, 0] + ] + a.table_size = (2, 2) + if decision == 3: + a.table = [ + [0, 0], + [0, 0], + [0, 0], + [0, 0] + ] + a.table_size = (4, 2) + if decision == 4: + a.table = [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] + ] + a.table_size = (4, 4) + if decision == 5: + a.table = [ + [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] + ], + [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] + ] + ] + a.table_size = (4, 4) + screen_type = 'done' + elif screen_type == 'done': + a.BackButton() + input_done = a.AnswerButton(el) + if input_done: + screen_type = 'answer' + continue + + stp = (a.screen_size[0] / 2 - a.table_size[1] * a.cell_size / 2.0, + a.screen_size[1] / 2 - a.table_size[0] * a.cell_size / 2.0 - a.cell_size / 2) + + for back_event in el: + if back_event.type == pygame.MOUSEBUTTONDOWN: + (mx, my) = pygame.mouse.get_pos() + if 30 < mx < 30 + 150 / 2 and 30 < my < 30 + 150 / 2: + screen_type = 'decide' + decision = None + + a.IsEvent('table', stp, el) + a.ShowTable('black') + + elif screen_type == 'answer': + a.BackButton() + + for back_event in el: + if back_event.type == pygame.MOUSEBUTTONDOWN: + (mx, my) = pygame.mouse.get_pos() + if 30 < mx < 30 + 150 / 2 and 30 < my < 30 + 150 / 2: + screen_type = 'done' + + if a.table_size == (2, 2): + from sukyoung import get_2by2 + ans = get_2by2(a.table) + qwer = 0 + for asdf in ans: + a.ShowText(asdf, 50, 'black', (100, 100 + qwer * 40)) + qwer += 1 + print(ans) + continue + + mt = [] + + for i in range(a.table_size[0]): + for j in range(a.table_size[1]): + if a.table[i][j] == 1: + if i == 0 and j == 0: + mt.append(0) + if i == 0 and j == 1: + mt.append(4) + if i == 0 and j == 2: + mt.append(12) + if i == 0 and j == 3: + mt.append(8) + if i == 1 and j == 0: + mt.append(1) + if i == 1 and j == 1: + mt.append(5) + if i == 1 and j == 2: + mt.append(13) + if i == 1 and j == 3: + mt.append(9) + if i == 2 and j == 0: + mt.append(3) + if i == 2 and j == 1: + mt.append(7) + if i == 2 and j == 2: + mt.append(15) + if i == 2 and j == 3: + mt.append(11) + if i == 3 and j == 0: + mt.append(2) + if i == 3 and j == 1: + mt.append(6) + if i == 3 and j == 2: + mt.append(14) + if i == 3 and j == 3: + mt.append(10) + + dc = [] + mt.sort() + minterms = mt + dc + minterms.sort() + size = len(bin(minterms[-1])) - 2 + groups, all_pi = {}, set() + + # Primary grouping starts + for minterm in minterms: + try: + groups[bin(minterm).count('1')].append(bin(minterm)[2:].zfill(size)) + except KeyError: + groups[bin(minterm).count('1')] = [bin(minterm)[2:].zfill(size)] + # Primary grouping ends + + # Primary group printing starts + # print("\n\n\n\nGroup No.\tMinterms\tBinary of Minterms\n%s" % ('=' * 50)) + # for i in sorted(groups.keys()): + # print("%5d:" % i) # Prints group number + # for j in groups[i]: + # print("\t\t %-20d%s" % (int(j, 2), j)) # Prints minterm and its binary representation + # print('-' * 50) + # Primary group printing ends + + # Process for creating tables and finding prime implicants starts + while True: + tmp = groups.copy() + groups, m, marked, should_stop = {}, 0, set(), True + l = sorted(list(tmp.keys())) + for i in range(len(l) - 1): + for j in tmp[l[i]]: # Loop which iterates through current group elements + for k in tmp[l[i + 1]]: # Loop which iterates through next group elements + res = compare(j, k) # Compare the minterms + if res[0]: # If the minterms differ by 1 bit only + try: + groups[m].append(j[:res[1]] + '-' + j[res[1] + 1:]) if j[:res[1]] + '-' + j[res[ + 1] + 1:] not in \ + groups[ + m] else None # Put a '-' in the changing bit and add it to corresponding group + except KeyError: + groups[m] = [j[:res[1]] + '-' + j[res[ + 1] + 1:]] # If the group doesn't exist, create the group at first and then put a '-' in the changing bit and add it to the newly created group + should_stop = False + marked.add(j) # Mark element j + marked.add(k) # Mark element k + m += 1 + local_unmarked = set(flatten(tmp)).difference(marked) # Unmarked elements of each table + all_pi = all_pi.union(local_unmarked) # Adding Prime Implicants to global list + # print("Unmarked elements(Prime Implicants) of this table:", None if len(local_unmarked) == 0 else ', '.join( + # local_unmarked)) # Printing Prime Implicants of current table + if should_stop: # If the minterms cannot be combined further + # print('--==-=-=-=-=-=123123123123123123123') + print(all_pi) + + # i = 0 + # for answers in all_pi: + # a.ShowText(answers, 50, 'black', (100, 150 + 50 * i)) + # i += 1 + + # print('329875904238720398534523452849357092435') + + # print("\n\nAll Prime Implicants: ", + # None if len(all_pi) == 0 else ', '.join(all_pi)) # Print all prime implicants + break + # Printing of all the next groups starts + # print("\n\n\n\nGroup No.\tMinterms\tBinary of Minterms\n%s" % ('=' * 50)) + # for i in sorted(groups.keys()): + # print("%5d:" % i) # Prints group number + # for j in groups[i]: + # print( + # "\t\t%-24s%s" % (','.join(findminterms(j)), j)) # Prints minterms and its binary representation + # print('-' * 50) + # Printing of all the next groups ends + # Process for creating tables and finding prime implicants ends + + sz = len(str(mt[-1])) # The number of digits of the largest minterm + chart = {} + # print('\n\n\nPrime Implicants chart:\n\n Minterms |%s\n%s' % ( + # ' '.join((' ' * (sz - len(str(i)))) + str(i) for i in mt), '=' * (len(mt) * (sz + 1) + 16))) + for i in all_pi: + merged_minterms, y = findminterms(i), 0 + # print("%-16s|" % ','.join(merged_minterms), end='') + for j in refine(merged_minterms, dc): + x = mt.index(int(j)) * (sz + 1) # The position where we should put 'X' + # print(' ' * abs(x - y) + ' ' * (sz - 1) + 'X', end='') + y = x + sz + try: + chart[j].append(i) if i not in chart[j] else None # Add minterm in chart + except KeyError: + chart[j] = [i] + # print('\n' + '-' * (len(mt) * (sz + 1) + 16)) + # Printing and processing of Prime Implicant chart ends + + EPI = findEPI(chart) # Finding essential prime implicants + print("\nEssential Prime Implicants: " + ', '.join(str(i) for i in EPI)) + removeTerms(chart, EPI) # Remove EPI related columns from chart + + if (len(chart) == 0): # If no minterms remain after removing EPI related columns + final_result = [findVariables(i) for i in EPI] # Final result with only EPIs + else: # Else follow Petrick's method for further simplification + P = [[findVariables(j) for j in chart[i]] for i in chart] + while len(P) > 1: # Keep multiplying until we get the SOP form of P + P[1] = multiply(P[0], P[1]) + P.pop(0) + final_result = [min(P[0], key=len)] # Choosing the term with minimum variables from P + final_result.extend(findVariables(i) for i in EPI) # Adding the EPIs to final solution + print('\n\nSolution: F = ' + ' + '.join(''.join(i) for i in final_result)) + final_answer = 'Solution:' + ' + '.join(''.join(i) for i in final_result) + a.ShowText(final_answer, 50, 'black', (100, 100)) + + pygame.display.flip() + + + +############################### + + + diff --git a/README.md b/README.md index c08480c..cb22ac6 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,45 @@ # APTP 2022 - **{팀명}** ###### Asterisk Python Team Project 2022 -구성원: 아스터 | 리스크 | 홍길동 | Change +구성원: 김덕용 | 김가현 | 송준서 | 배정윤 | 윤석영 + +2022.05.04 ~ 2022.06.02 ## 1. 주제 -{주제 작성} +Implicant 탐색 프로그램 ## 2. 동기 -{동기 작성} +기초논리회로 수업을 듣던 중 Implicant를 찾아주는 프로그램이 있으면 좋다고 생각했다. ## 3. 프로그램 사용 대상 -{사용 대상 작성} +기초논리회로 수업을 수강하는 학생 / Implicant를 알고싶은 사람 ## 4. 목적 -{목적 작성} +Implicant를 찾아준다 ## 5. 주요기능 -1. 주요한 기능 -2. 두번째로 주요한 기능 -3. 등등등 +1. Prime Implicant 찾아줄 수 있다. +2. 입력 갯수 (2, 3, 4, 5)에 따른 Implicant 찾아줄 수 있다. +3. UI를 통해 이를 그림으로 보여줄 수 있다. +4. UI를 통해 입력값을 설정할 수 있다. +5. Essential Prime Implicant를 표시할 수 있다. ## 6. 프로젝트 핵심 -1. 첫번째 핵심 -2. 두번째 핵심 -3. 세번째 핵심 +1. 2차원 배열 해석 ( 백준 연구소 ) +2. UI 구현 +3. Essential Prime Implicant 판정 ## 7. 구현에 필요한 라이브러리나 기술 -{pygame, sys, socket, ...} +pygame ## 8. **분업 계획** -홍길동: GUI 구현, board 구현 -아스터: 상자 Class 구현 -리스크: 서버 및 클라이언트 구현 +김덕용: GUI 구현 +김가현: 5개 Implicant 구현 +송준서: 4개 Implicant 구현 +배정윤: 3개 Implicant 구현 +윤석영: 2개 Implicant 구현 ## 9. 기타 -1. 기타적인 무엇 -2. 등등 작성
diff --git a/TABLE.py b/TABLE.py new file mode 100644 index 0000000..b5cd66a --- /dev/null +++ b/TABLE.py @@ -0,0 +1,13 @@ +class Table: + """ + Class for Table + """ + + def __init__(self, table, table_size): + """ + Basic Information + :param table: list + :param table_size: Tuple + """ + self.table = table + self.table_size = table_size diff --git a/jeongyun.py b/jeongyun.py new file mode 100644 index 0000000..2681ae9 --- /dev/null +++ b/jeongyun.py @@ -0,0 +1,596 @@ +# K_Map for 2 inputs. +def inp2_k_map(mt, nip): + for i in range(50): print("-", end='') + print() + prnt = input("Enter Variables with single space(eg:A B) : ").split(" ") + for i in range(len(mt)): + mt[i] = '0b' + bin(mt[i])[2:].lstrip('0') + op = '' + ans = [[0, 0], [0, 0]] + ansmx = [[0, 0], [0, 0]] + flag = 0 + temp = [] + for i in range(2): + for j in range(2): + p = '0b' + (bin(i)[2:] + bin(j)[2:]).lstrip('0') + if p in mt: + ans[i][j] = 1 + for i in range(50): print("-", end='') + print() + print("The kmap plotted : ") + if nip == 1: + for each in ans: print(*each) + elif nip == 2: + for i in range(2): + for j in range(2): + if ans[i][j] == 1: + ansmx[i][j] = 0 + else: + ansmx[i][j] = 1 + for each in ansmx: print(*each) + if ans == [[1, 1], [1, 1]]: + flag = 1 + op = '1' + + if flag == 0: + for i in range(2): + if ans[i] == [1, 1]: + if nip == 1: + op = 'A ' if i == 1 else "A' " + elif nip == 2: + op = "(A') " if i == 1 else "(A) " + temp.extend([(i, 0), (i, 1)]) + + if flag == 0: + if ans[0][0] == 1 and ans[1][0] == 1: + if nip == 1: + op = op + "B' " + elif nip == 2: + op = op + "(B) " + temp.extend([(0, 0), (1, 0)]) + elif ans[0][1] == 1 and ans[1][1] == 1: + if nip == 1: + op = op + "B " + elif nip == 2: + op = op + "(B') " + temp.extend([(0, 1), (1, 1)]) + if nip == 1: + vr = ["A'B' ", "A'B ", "AB' ", "AB "] + elif nip == 2: + vr = ["(A+B) ", "(A+B') ", "(A'+B) ", "(A'+B') "] + + if flag == 0: + for i in range(2): + for j in range(2): + if ans[i][j] == 1 and (i, j) not in temp: + op = op + vr[int('0b' + bin(i)[2:] + bin(j)[2:], 2)] + op = op.rstrip(" ") + if nip == 1: + op = op.replace(" ", "+") + op = op.replace("A", prnt[0]) + op = op.replace("B", prnt[1]) + for i in range(50): print("*", end='') + print() + print("The simplified equation is :", op) + for i in range(50): print("*", end='') + print() + + +# K_Map for 4 inputs. +def inp4_k_map(mt, nip): + import copy + for _ in range(50): print("-", end='') + print() + pr_v = input("Enter variables single space(eg:A B C D) : ").split(" ") + an = [] + anmx = [] + (tmp, flag) = (0, 0) + op = '' + for i in range(4): + an.append([0] * 4) + anmx.append([0] * 4) + + for i in range(4): + for j in range(4): + if i < 2: + bi = '0' + bin(i)[2:] + else: + bi = bin(i)[2:] + if j < 2: + bj = '0' + bin(j)[2:] + else: + bj = bin(j)[2:] + p = int('0b' + bi + bj, 2) + if p in mt: + an[i][j] = 1 + for i in range(4): + (an[i][2], an[i][3]) = (an[i][3], an[i][2]) + for i in range(4): + (an[2][i], an[3][i]) = (an[3][i], an[2][i]) + + for _ in range(50): print("-", end='') + print() + print("The K-Map plotted : ") + if nip == 1: + for each in an: + print(*each) + elif nip == 2: + for i in range(4): + for j in range(4): + if an[i][j] == 1: + anmx[i][j] = 0 + else: + anmx[i][j] = 1 + for each in anmx: print(*each) + + octa = [] + qrd = [] + qrd_ref = [] + qrd_rep = [] + dul = [] + sngl = [] + if nip == 1: + octa_val = [["C' ", "D ", "C ", "D' "], ["A' ", "B ", "A ", "B' "]] # 0 for vert and 1 for horz + qrd_val = [["C'D' ", "C'D ", "CD ", "CD' "], ["A'B' ", "A'B ", "AB ", "AB' "]] # 0 for vert and 1 for horz + qrd_val_4 = [["A'C' ", "A'D ", "A'C ", "A'D' "], ["BC' ", "BD ", "BC ", "BD' "], ["AC' ", "AD ", "AC ", "AD' "], + ["B'C' ", "B'D ", "B'C ", "B'D' "]] + dul_vert = [["A'C'D' ", "A'C'D ", "A'CD ", "A'CD' "], ["BC'D' ", "BC'D ", "BCD ", "BCD' "], + ["AC'D' ", "AC'D ", "ACD ", "ACD' "], ["B'C'D' ", "B'C'D ", "B'CD ", "B'CD' "]] + dul_horz = [["A'B'C' ", "A'B'D ", "A'B'C ", "A'B'D' "], ["A'BC' ", "A'BD ", "A'BC ", "A'BD' "], + ["ABC' ", "ABD ", "ABC ", "ABD' "], ["AB'C' ", "AB'D ", "AB'C ", "AB'D' "]] + sngl_val = [["A'B'C'D' ", "A'B'C'D ", "A'B'CD ", "A'B'CD' "], ["A'BC'D' ", "A'BC'D ", "A'BCD ", "A'BCD' "], + ["ABC'D' ", "ABC'D ", "ABCD ", "ABCD' "], ["AB'C'D' ", "AB'C'D ", "AB'CD ", "AB'CD' "]] + elif nip == 2: + octa_val = [["(C) ", "(D') ", "(C') ", "(D) "], ["(A) ", "(B') ", "(A') ", "(B) "]] # 0 for vert and 1 for horz + qrd_val = [["(C+D) ", "(C+D') ", "(C'+D') ", "(C'+D) "], + ["(A+B) ", "(A+B') ", "(A'+B') ", "(A'+B) "]] # 0 for vert and 1 for horz + qrd_val_4 = [["(A+C) ", "(A+D') ", "(A+C') ", "(A+D) "], ["(B'+C) ", "(B'+D') ", "(B'+C') ", "(B'+D) "], + ["(A'+C) ", "(A'+D') ", "(A'+C') ", "(A'+D) "], ["(B+C) ", "(B+D') ", "(B+C') ", "(B+D) "]] + dul_vert = [["(A+C+D) ", "(A+C+D') ", "(A+C'+D') ", "(A+C'+D) "], + ["(B'+C+D) ", "(B'+C+D') ", "(B'+C'+D') ", "(B'+C'+D) "], + ["(A'+C+D) ", "(A'+C+D') ", "(A'+C'+D') ", "(A'+C'+D) "], + ["(B+C+D) ", "(B+C+D') ", "(B+C'+D') ", "(B+C'+D) "]] + dul_horz = [["(A+B+C) ", "(A+B+D') ", "(A+B+C') ", "(A+B+D) "], + ["(A+B'+C) ", "(A+B'+D') ", "(A+B'+C') ", "(A+B'+D) "], + ["(A'+B'+C) ", "(A'+B'+D') ", "(A'+B'+C') ", "(A'+B'+D) "], + ["(A'+B+C) ", "(A'+B+D') ", "(A'+B+C') ", "(A'+B+D) "]] + sngl_val = [["(A+B+C+D) ", "(A+B+C+D') ", "(A+B+C'+D') ", "(A+B+C'+D) "], + ["(A+B'+C+D) ", "(A+B'+C+D') ", "(A+B'+C'+D') ", "(A+B'+C'+D) "], + ["(A'+B'+C+D) ", "(A'+B'+C+D') ", "(A'+B'+C'+D') ", "(A'+B'+C'+D) "], + ["(A'+B+C+D) ", "(A'+B+C+D') ", "(A'+B+C'+D') ", "(A'+B+C'+D) "]] + + if an == [[1] * 4, [1] * 4, [1] * 4, [1] * 4]: + op = '1' + else: + for i in range(-1, 3): + if an[i][0] == 1 and an[i][1] == 1 and an[i][2] == 1 and an[i][-1] == 1 and an[i + 1][0] == 1 and an[i + 1][ + 1] == 1 and an[i + 1][2] == 1 and an[i + 1][-1] == 1: + op = op + octa_val[1][i] + octa.append([(i, 0), (i, 1), (i, 2), (i, -1)]) + if i < 2: + octa.append([(i + 1, 0), (i + 1, 1), (i + 1, 2), (i + 1, -1)]) + else: + octa.append([(-1, 0), (-1, 1), (-1, 2), (-1, -1)]) + if i < 2: + octa.append([(i, 0), (i + 1, 0), (i, 1), (i + 1, 1)]) + octa.append([(i, 1), (i + 1, 1), (i, 2), (i + 1, 2)]) + octa.append([(i, 2), (i + 1, 2), (i, -1), (i + 1, -1)]) + octa.append([(i, -1), (i + 1, -1), (i, 0), (i + 1, 0)]) + else: + octa.append([(i, 0), (-1, 0), (i, 1), (-1, 1)]) + octa.append([(i, 1), (-1, 1), (i, 2), (-1, 2)]) + octa.append([(i, 2), (-1, 2), (i, -1), (-1, -1)]) + octa.append([(i, -1), (-1, -1), (i, 0), (-1, 0)]) + + for i in range(-1, 3): + if an[0][i] == 1 and an[1][i] == 1 and an[2][i] == 1 and an[-1][i] == 1 and an[0][i + 1] == 1 and an[1][ + i + 1] == 1 and an[2][i + 1] == 1 and an[-1][i + 1] == 1: + op = op + octa_val[0][i] + octa.append([(0, i), (1, i), (2, i), (-1, i)]) + if i < 2: + octa.append([(0, i + 1), (1, i + 1), (2, i + 1), (-1, i + 1)]) + else: + octa.append([(0, -1), (1, -1), (2, -1), (-1, -1)]) + if i < 2: + octa.append([(0, i), (1, i), (0, i + 1), (1, i + 1)]) + octa.append([(1, i), (2, i), (1, i + 1), (2, i + 1)]) + octa.append([(2, i), (-1, i), (2, i + 1), (-1, i + 1)]) + octa.append([(-1, i), (0, i), (-1, i + 1), (0, i + 1)]) + else: + octa.append([(0, i), (1, i), (0, -1), (1, -1)]) + octa.append([(1, i), (2, i), (1, -1), (2, -1)]) + octa.append([(2, i), (-1, i), (2, -1), (-1, -1)]) + octa.append([(-1, i), (0, i), (-1, -1), (0, -1)]) + + for i in range(-1, 3): + if an[i][0] == 1 and an[i][1] == 1 and an[i][2] == 1 and an[i][-1] == 1: + qrd_ref.append([(i, 0), (i, 1), (i, 2), (i, -1)]) + if an[0][i] == 1 and an[1][i] == 1 and an[2][i] == 1 and an[-1][i] == 1: + qrd_ref.append([(0, i), (1, i), (2, i), (-1, i)]) + + for i in range(-1, 3): + for j in range(-1, 3): + if an[i][j] == 1 and an[i][j + 1] == 1 and an[i + 1][j] == 1 and an[i + 1][j + 1] == 1: + if i == 2 and j == 2: + temp = [(i, j), (-1, j), (i, -1), (-1, -1)] + elif i == 2 and j < 2: + temp = [(i, j), (-1, j), (i, j + 1), (-1, j + 1)] + elif j == 2 and i < 2: + temp = [(i, j), (i + 1, j), (i, -1), (i + 1, -1)] + else: + temp = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)] + qrd_ref.append(temp) + + for i in range(-1, 3): + if an[i][0] == 1 and an[i][1] == 1 and an[i][2] == 1 and an[i][-1] == 1: + if [(i, 0), (i, 1), (i, 2), (i, -1)] not in octa: + op = op + qrd_val[1][i] + qrd.append([(i, 0), (i, 1)]) + qrd.append([(i, 1), (i, 2)]) + qrd.append([(i, 2), (i, -1)]) + qrd.append([(i, -1), (i, 0)]) + + for i in range(-1, 3): + if an[0][i] == 1 and an[1][i] == 1 and an[2][i] == 1 and an[-1][i] == 1: + if [(0, i), (1, i), (2, i), (-1, i)] not in octa: + op = op + qrd_val[0][i] + qrd.append([(0, i), (1, i)]) + qrd.append([(1, i), (2, i)]) + qrd.append([(2, i), (-1, i)]) + qrd.append([(-1, i), (0, i)]) + + for i in range(-1, 3): + for j in range(-1, 3): + if an[i][j] == 1 and an[i][j + 1] == 1 and an[i + 1][j] == 1 and an[i + 1][j + 1] == 1: + if i == 2 and j == 2: + temp = [(i, j), (-1, j), (i, -1), (-1, -1)] + elif i == 2 and j < 2: + temp = [(i, j), (-1, j), (i, j + 1), (-1, j + 1)] + elif j == 2 and i < 2: + temp = [(i, j), (i + 1, j), (i, -1), (i + 1, -1)] + else: + temp = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)] + if temp not in octa: + op = op + qrd_val_4[i][j] + if i < 2 and j < 2: + qrd.append([(i, j), (i, j + 1)]) + qrd.append([(i + 1, j), (i + 1, j + 1)]) + qrd.append([(i, j), (i + 1, j)]) + qrd.append([(i, j + 1), (i + 1, j + 1)]) + if j == 2 and i < 2: + qrd.append([(i, j), (i, -1)]) + qrd.append([(i + 1, j), (i + 1, -1)]) + qrd.append([(i, j), (i + 1, j)]) + qrd.append([(i, -1), (i + 1, -1)]) + if j < 2 and i == 2: + qrd.append([(i, j), (i, j + 1)]) + qrd.append([(-1, j), (-1, j + 1)]) + qrd.append([(i, j), (-1, j)]) + qrd.append([(i, j + 1), (-1, j + 1)]) + if i == 2 and j == 2: + qrd.append([(i, j), (i, -1)]) + qrd.append([(-1, j), (-1, -1)]) + qrd.append([(i, j), (-1, j)]) + qrd.append([(i, -1), (-1, -1)]) + + for i in range(-1, 3): + for j in range(-1, 3): + if an[i][j] == 1 and an[i][j + 1] == 1: + if j == 2: + temp = [(i, j), (i, -1)] + else: + temp = [(i, j), (i, j + 1)] + if temp not in qrd: + op = op + dul_horz[i][j] + if j == 2: + dul.append([(i, j), (i, -1)]) + else: + dul.append([(i, j), (i, j + 1)]) + if an[i][j] == 1 and an[i + 1][j] == 1: + if i == 2: + temp = [(i, j), (-1, j)] + else: + temp = [(i, j), (i + 1, j)] + if temp not in qrd: + op = op + dul_vert[i][j] + if i == 2: + dul.append([(i, j), (-1, j)]) + else: + dul.append([(i, j), (i + 1, j)]) + + for each in octa: + sngl.extend(each) + for each in qrd: + sngl.extend(each) + for each in dul: + sngl.extend(each) + for i in range(-1, 3): + for j in range(-1, 3): + if an[i][j] == 1: + if (i, j) not in sngl: + op = op + sngl_val[i][j] + + op = op.strip() + opl = op.split(" ") + for i in range(len(opl)): + opl[i] = opl[i] + " " + + dulref = copy.deepcopy(dul) + + for each in dul: + (d1, d2) = (each[0], each[1]) + (cntd1, cntd2) = (0, 0) + for each in dulref: + if d1 in each: + cntd1 += 1 + if d2 in each: + cntd2 += 1 + for each in qrd_ref: + if d1 in each: + cntd1 += 1 + if d2 in each: + cntd2 += 1 + if cntd1 > 1 and cntd2 > 1: + try: + if d1[0] == d2[0]: + opl.remove(dul_horz[d1[0]][d1[1]]) + if d1[1] == d2[1]: + opl.remove(dul_vert[d1[0]][d1[1]]) + dulref.remove([(d1[0], d1[1]), (d2[0], d2[1])]) + except ValueError: + continue + + for each in qrd_ref: + (d1, d2, d3, d4) = (each[0], each[1], each[2], each[3]) + (d1cnt, d2cnt, d3cnt, d4cnt) = (0, 0, 0, 0) + for each1 in dul: + if d1 in each1: + d1cnt += 1 + if d2 in each1: + d2cnt += 1 + if d3 in each1: + d3cnt += 1 + if d4 in each1: + d4cnt += 1 + for each2 in qrd_ref: + if each != each2: + if d1 in each2: + d1cnt += 1 + if d2 in each2: + d2cnt += 1 + if d3 in each2: + d3cnt += 1 + if d4 in each2: + d4cnt += 1 + if d1cnt > 0 and d2cnt > 0 and d3cnt > 0 and d4cnt > 0: + try: + if d1[0] != d2[0] and d1[1] != d3[1]: + opl.remove(qrd_val_4[d1[0]][d1[1]]) + except ValueError: + continue + + for each in qrd_ref: + (d1, d2, d3, d4) = (each[0], each[1], each[2], each[3]) + (d1cnt, d2cnt, d3cnt, d4cnt) = (0, 0, 0, 0) + for each1 in qrd_ref: + if each1 != each: + if d1 in each1: + d1cnt += 1 + if d2 in each1: + d2cnt += 1 + if d3 in each1: + d3cnt += 1 + if d4 in each1: + d4cnt += 1 + if d1cnt > 0 and d2cnt > 0 and d3cnt > 0 and d4cnt > 0: + try: + if d1[0] == d2[0] == d3[0] == d4[0]: + opl.remove(qrd_val[1][d1[0]]) + elif d1[1] == d2[1] == d3[1] == d4[1]: + opl.remove(qrd_val[0][d1[1]]) + except ValueError: + continue + + op = ''.join(opl) + for _ in range(50): print("*", end='') + print() + op = op.strip(" ") + if nip == 1: + op = op.replace(" ", " + ") + op = op.replace("A", pr_v[0]) + op = op.replace("B", pr_v[1]) + op = op.replace("C", pr_v[2]) + op = op.replace("D", pr_v[3]) + print("Simplified equation is :", op) + + for _ in range(50): print("*", end='') + print() + + +# K_Map for 3 inputs. +def inp3_k_map(mt, nip): + for i in range(50): print("-", end='') + print() + var_re = input("Enter variables with single space(eg:A B C) : ").split(" ") + ansg = [[0, 0, 0, 0], [0, 0, 0, 0]] + ansgmx = [[0, 0, 0, 0], [0, 0, 0, 0]] + op = '' + flag = 0 + qrd = [] + dul = [] + sngl = [] + if nip == 1: + qrd_var_2_2 = ["B' ", "C ", "B ", "C' "] + qrd_var_1_4 = ["A' ", "A "] + dul_vert = ["B'C' ", "B'C ", "BC ", "BC' "] + dul_horz = [["A'B' ", "A'C ", "A'B ", "A'C' "], ["AB' ", "AC ", "AB ", "AC'"]] + sngl_val = [["A'B'C' ", "A'B'C ", "A'BC ", "A'BC' "], ["AB'C' ", "AB'C ", "ABC ", "ABC' "]] + elif nip == 2: + qrd_var_2_2 = ["(B) ", "(C') ", "(B') ", "(C) "] + qrd_var_1_4 = ["(A) ", "(A') "] + dul_vert = ["(B+C) ", "(B+C') ", "(B'+C') ", "(B'+C) "] + dul_horz = [["(A+B) ", "(A+C') ", "(A+B') ", "(A+C) "], ["(A'+B) ", "(A'+C') ", "(A'+B') ", "(A'+C) "]] + sngl_val = [["(A+B+C) ", "(A+B+C') ", "(A+B'+C') ", "(A+B'+C) "], + ["(A'+B+C) ", "(A'+B+C') ", "(A'+B'+C') ", "(A'+B'+C) "]] + for i in range(2): + for j in range(4): + p = int('0b' + bin(i)[2:] + bin(j)[2:], 2) + if (i == 1) and (j == 0 or j == 1): + p = int('0b' + bin(i)[2:] + '0' + bin(j)[2:], 2) + if p in mt: + ansg[i][j] = 1 + + for i in range(2): + (ansg[i][2], ansg[i][3]) = (ansg[i][3], ansg[i][2]) + for i in range(50): print("-", end='') + print() + print("K-Map plotted : ") + if nip == 1: + for each in ansg: print(*each) + elif nip == 2: + for i in range(2): + for j in range(4): + if ansg[i][j] == 1: + ansgmx[i][j] = 0 + else: + ansgmx[i][j] = 1 + for each in ansgmx: print(*each) + + if ansg == [[1] * 4, [1] * 4]: + op = op + '1' + flag = 1 + if flag == 0: + for j in range(-1, 3): + if ansg[0][j] == 1 and ansg[-1][j] == 1 and ansg[0][j + 1] == 1 and ansg[-1][j + 1] == 1: + qrd.append([(0, j), (-1, j)]) + if j < 2: + qrd.append([(0, j + 1), (-1, j + 1)]) + qrd.append([(0, j), (0, j + 1)]) + qrd.append([(-1, j), (-1, j + 1)]) + else: + qrd.append([(0, -1), (-1, -1)]) + qrd.append([(0, j), (0, -1)]) + qrd.append([(-1, j), (-1, -1)]) + op = op + qrd_var_2_2[j] + if flag == 0: + for i in range(-1, 1): + if ansg[i] == [1, 1, 1, 1]: + qrd.append([(i, -1), (i, 0)]) + qrd.append([(i, 0), (i, 1)]) + qrd.append([(i, 1), (i, 2)]) + qrd.append([(i, 2), (i, -1)]) + op = op + qrd_var_1_4[i] + if flag == 0: + for j in range(-1, 3): + if ansg[0][j] == 1 and ansg[1][j] == 1: + temp = 0 + if [(0, j), (-1, j)] in qrd: + temp = 1 + elif [(-1, j), (0, j)] in qrd: + temp = 1 + if temp == 0: + dul.append([(0, j), (-1, j)]) + op = op + dul_vert[j] + + if flag == 0: + for i in range(-1, 1): + for j in range(-1, 3): + if ansg[i][j] == 1 and ansg[i][j + 1] == 1: + temp = 0 + if j == 2: + if [(i, j), (i, -1)] in qrd: + temp = 1 + elif [(i, -1), (i, j)] in qrd: + temp = 1 + else: + if [(i, j), (i, j + 1)] in qrd: + temp = 1 + elif [(i, j + 1), (i, j)] in qrd: + temp = 1 + if temp == 0: + if j == 2: + dul.append([(i, 2), (i, -1)]) + else: + dul.append([(i, j), (i, j + 1)]) + op = op + dul_horz[i][j] + + op = op.rstrip(" ") + opl = op.split(" ") + for i in range(len(opl)): + opl[i] = opl[i] + " " + + for each in dul: + d1cnt = 0 + d2cnt = 0 + (d1, d2) = (each[0], each[1]) + for each1 in dul: + if d1 in each1: + d1cnt += 1 + if d2 in each1: + d2cnt += 1 + if d1cnt > 1 and d2cnt > 1: + (d1i, d1j) = d1 + (d2i, d2j) = d2 + if d1i == d2i: + p = dul_horz[d1i][d1j] + opl.remove(p) + else: + p = dul_vert[d1j] + opl.remove(p) + dul.remove([d1, d2]) + op = "".join(opl) + + for _ in qrd: + for each in _: + sngl.append(each) + for _ in dul: + for each in _: + sngl.append(each) + + if flag == 0: + for i in range(-1, 1): + for j in range(-1, 3): + if ansg[i][j] == 1: + if (i, j) not in sngl: + op = op + sngl_val[i][j] + + op = op.rstrip(" ") + if nip == 1: + op = op.replace(' ', ' + ') + + op = op.replace("A", var_re[0]) + op = op.replace("B", var_re[1]) + op = op.replace("C", var_re[2]) + for i in range(50): print("*", end='') + print() + print("The simplified equation is :", op) + for i in range(50): print("*", end='') + print() + + +# main +ask = 'y' +while ask == 'y': + for _ in range(50): print("-", end='') + print() + nfinp = int(input("Enter no of Inputs(2,3,4) : ")) + mimastr = input("Enter 'SOP' for SOP(Minterms) or 'POS' for POS(Maxterms) : ").lower() + if mimastr == 'pos': + mima = 2 + elif mimastr == 'sop': + mima = 1 + if mima == 1: + mt = list(map(int, input("Enter Minterms : ").split())) + else: + mt = list(map(int, input("Enter Maxterms : ").split())) + if nfinp == 2: + inp2_k_map(mt, mima) + elif nfinp == 3: + inp3_k_map(mt, mima) + elif nfinp == 4: + inp4_k_map(mt, mima) + else: + print("You committed some mistake, please check the inputs next time.") + ask = input("Enter 'y' for another K-Map or 'n' to exit : ").lower() + for i in range(25): print("--", end='') + print() \ No newline at end of file diff --git a/kmap_3 b/kmap_3 new file mode 100644 index 0000000..e69de29 diff --git a/kmap_5.py b/kmap_5.py new file mode 100644 index 0000000..6d2531b --- /dev/null +++ b/kmap_5.py @@ -0,0 +1,37 @@ +imp = + +for i in range(2): + for j in range(4): + for k in range(4): + if arr[i][j][k] == 1: + if arr[i+1][j][k] == 1: + + if arr[i-1][j][k] == 1: + + if arr[i][j+1][k] == 1: + + if arr[i][j-1][k] == 1: + + if arr[i][j][k+1] == 1: + + + + + +""" +# 길이,0:가로/1:세로,좌표 +n = int(input()) +for i in range(n): + l, d, x, y = map(int, input().split()) + for j in range(l):#격자판 좌표 원점은 1,1 / 리스트 원점은 0,0 // d가 0이면 가로로 증가 1이면 세로로 증가 + if d == 0: + m[x-1][y-1+j] = 1 + else: + m[x-1+j][y-1] = 1 + +#격자판 출력 +for i in range(h): + for j in range(w): + print(m[i][j], end=' ') + print() +""" \ No newline at end of file diff --git a/sukyoung.py b/sukyoung.py new file mode 100644 index 0000000..b7997e6 --- /dev/null +++ b/sukyoung.py @@ -0,0 +1,75 @@ +# Wrong implement +# I just make my mind to GIVE UP with 2 by 2 problem + +def get_2by2(table): + ans = [] + + if table == [[0, 0], + [0, 0]]: + ans = [] + + elif table == [[0, 0], + [0, 1]]: + ans = [[[1, 1]]] + + elif table == [[0, 0], + [1, 0]]: + ans = [[table[1][0]]] + + elif table == [[0, 0], + [1, 1]]: + ans = [[table[1][0], table[1][1]]] + + elif table == [[0, 1], + [0, 0]]: + ans = [[table[0][1]]] + + elif table == [[0, 1], + [0, 1]]: + ans = [[table[0][1], table[1][1]]] + + elif table == [[0, 1], + [1, 1]]: + ans = [[table[0][1], table[1][1]], [table[1][0], table[1][1]]] + + elif table == [[0, 1], + [1, 0]]: + ans = [[table[0][1]], [table[1][0]]] + + elif table == [[1, 0], + [0, 0]]: + ans = [[table[0][0]]] + + elif table == [[1, 0], + [0, 1]]: + ans = [[table[0][0]], [table[1][1]]] + + elif table == [[1, 0], + [1, 0]]: + ans = [[table[0][0], table[0][1]]] + + elif table == [[1, 0], + [1, 1]]: + ans = [[table[0][0], table[1][0]], [table[1][0], table[1][1]]] + + elif table == [[1, 1], + [0, 0]]: + ans = [[table[0][0], table[0][1]]] + + elif table == [[1, 1], + [0, 1]]: + ans = [[table[0][0], table[0][1]], [table[0][1], table[1][1]]] + + elif table == [[1, 1], + [1, 0]]: + ans = [[table[0][0], table[0][1]], [table[0][0], table[1][0]]] + + elif table == [[1, 1], + [1, 1]]: + ans = [[table[0][0], table[0][1], table[1][0], table[1][1]]] + + return ans + +print(get_2by2([[0,1],[1,1]])) + + diff --git a/test.py b/test.py new file mode 100644 index 0000000..28e0e87 --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ +def multiple(a: int, b: int) -> int: + return a * b + + +print(multiple(1, True))