0% found this document useful (0 votes)
41 views2 pages

Datastructure Stacks

aait

Uploaded by

Yeabsira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Datastructure Stacks

aait

Uploaded by

Yeabsira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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