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

Datastructure Stacks

The document provides Python code for two tasks: converting an integer to binary using a stack and implementing parenthesis matching using a stack. The first function, divby2, takes an integer input and returns its binary representation, while the second function, check, verifies if the parentheses in a given string are properly matched. Both implementations utilize stack data structures to achieve their respective functionalities.

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)
53 views2 pages

Datastructure Stacks

The document provides Python code for two tasks: converting an integer to binary using a stack and implementing parenthesis matching using a stack. The first function, divby2, takes an integer input and returns its binary representation, while the second function, check, verifies if the parentheses in a given string are properly matched. Both implementations utilize stack data structures to achieve their respective functionalities.

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