def baseConverter(n, base): """ Returns a decimal number into another base between 1 and 16 """ digits = "0123456789ABCDEF" s = Stack() while n != 0: reminder = n % base s.push(reminder) n = n / base baseString = "" while not s.isEmpty(): baseString += digits[s.pop()] return baseString # test #print(baseConverter(25, 2)) #print(baseConverter(256, 16))
def reverse(str): str2 = [] s = Stack() for i in str: s.push(i) while (str != []): t = s.pop() str2.append(t) return str2
def dec_to_bin(dec_num): rs = Stack() while dec_num > 0: r = dec_num % 2 rs.push(r) dec_num = dec_num // 2 return_str = "" while not rs.isEmpty(): return_str = return_str + str(rs.pop()) return return_str
def dec_to_other_base(dec_num, base): digits = "0123456789ABCDEF" rs = Stack() while dec_num > 0: r = dec_num % base rs.push(r) dec_num = dec_num // base return_str = "" while not rs.isEmpty(): return_str = return_str + digits[rs.pop()] return return_str #print(dec_to_bin(233)) #print(dec_to_other_base(255,16))
def revString(myStr): """ Reverses a String """ s = Stack() for c in myStr: s.push(c) newStr = "" while not s.isEmpty(): newStr += s.pop() return newStr # test #print(revString("hello"))
def tirpleParenthesesChecker(symbolString): """ Checks whether or not a set of parentheses, square brackets and curly braces is correctly balanced """ s = Stack() balanced = True index = 0 while index < len(symbolString) and balanced: symbol = symbolString[index] if symbol in "([{": s.push(symbol) else: if s.isEmpty(): balanced = False else: top = s.pop() if not matches(top, symbol): balanced = False index = index + 1 if balanced and s.isEmpty(): return True else: return False
from stack_implementation import Stack """Import the stack class to create objects, test its functions.""" a = Stack() a.push(4) a.push(3) a.display() a.pop() a.pop() a.display()