def binary(num): rem_stack = practic1.Stack() while num > 0: rem = num % 2 rem_stack.push(rem) num = num // 2 binary_string = '' while not rem_stack.is_empty(): binary_string = binary_string + str(rem_stack.pop()) return binary_string
def post_evaluation(exp): exp_split = exp.split() s = practic1.Stack() for t in exp_split: if t in "0123456789": s.push(int(t)) else: o1 = int(s.pop()) o2 = int(s.pop()) result = do_math(t, o2, o1) s.push(result) return int(s.pop())
def binary(num, base): digits = "0123456789ABCDEF" rem_stack = practic1.Stack() while num > 0: rem = num % base rem_stack.push(rem) num = num // base binary_string = '' while not rem_stack.is_empty(): binary_string = binary_string + digits[rem_stack.pop()] return binary_string
def par_checker(string_brakets): b = True s = practic1.Stack() i = 0 while i < len(string_brakets) and b: sy = string_brakets[i] if sy == '(': s.push(sy) else: if s.is_empty(): b = False else: s.pop() i = i + 1 if b and s.is_empty(): return 'The Input Parentheses is Balanced' else: return 'The Input Parenthesis is Not Balanced'
def balanced_bracket(symbol): b = True i = 0 s = practic1.Stack() while i < len(symbol) and b: sy = symbol[i] if sy in '{[(': s.push(sy) else: if s.is_empty(): b = False else: top = s.pop() if not matches(top, sy): b = False i = i + 1 if b and s.is_empty(): return 'It is Balanced' else: return 'It is not Balanced'
def infix_postfix(exp): s = practic1.Stack() prec = {'*': 3, '/': 3, '+':2, '-': 2, '(': 1} postfix_list = [] split_exp = exp.split() for t in split_exp: if t in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or t in "0123456789": postfix_list.append(t) elif t == '(': s.push(t) elif t == ')': top_token = s.pop() while top_token != '(': postfix_list.append(top_token) top_token = s.pop() else: while (not s.is_empty()) and (prec[s.peek()] >= prec[t]): postfix_list.append(s.pop()) s.push(t) while not s.is_empty(): postfix_list.append(s.pop()) return ' '.join(postfix_list)