You are on page 1of 2

Yeabsira Mokonnen ATE/9596/12

software engineering
1. Accept an integer from a user and convert the integer to a binary number using a stack.
(use python).

 decimalnum = int(input( “ enter an integer: “))

def divby2 (decimalnum):


a= stack ()

while decimalnum >0:


remainder =decimalnum %2
a.append(reminder)
decimalnum =decimalnum // 2

binarynum= “ “
while not a.isempty():
binarynum += str(a.pop())
return binarynum

2. Implement parenthesis matching using stack. (use python)

 open_brackets = ["[","{","("]

closed_brackets = ["]","}",")"]
  

def check(myStr):
     stack = []
    
for i in myStr:
         if i in open_brackets:
             stack.append(i)

         elif i in close_brackets:


             pos = close_brackets.index(i)

             if ((len(stack) > 0) and


                (open_brackets[pos] == stack[len(stack)-1])):
                 stack.pop()
             else:
                 return "parenthesis doesn’t match"

     if len(stack) == 0:
         return "parenthesis matches"
     else:
         return " parenthesis doesn’t match "
  

You might also like