码迷,mamicode.com
首页 > 其他好文 > 详细

Algorithm - Stack Exercise

时间:2016-06-29 01:11:13      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

Simple Balanced Parentheses

write an algorithm that will read a string of parentheses from left to right and decide whether the symbols are balanced.

 

技术分享

 

 

Algorithm:

1. Create an empty stack.

2. if a symbol is an opening parenthesis, Push it on the Stack and wait an closing parenthesis to pop.

3. if symbol is closing parenthesis, if the stack not empty, pop the open parenthesis.

4. If at any time there is no opening symbol on the stack to match a closing symbol, the string is not balanced properly. 

5. At the end of the string, when all symbols have been processed, the stack should be empty. 

 

 

 1 from pythonds.basic.stack import Stack
 2 
 3 def parChecker(symbolString):
 4     s = Stack()        //Step 1
 5     balanced = True
 6     index = 0
 7     while index < len(symbolString) and balanced:
 8         symbol = symbolString[index]
 9         if symbol == "(":
10             s.push(symbol)
11         else:
12             if s.isEmpty():
13                 balanced = False
14             else:
15                 s.pop()
16 
17         index = index + 1
18 
19     if balanced and s.isEmpty():
20         return True
21     else:
22         return False
23 
24 print(parChecker(((()))))
25 print(parChecker((()))

 

 

Balanced Symbols(A General Case)

How can judge the mixed Symbols in a single program?

技术分享

 

 1 from pythonds.basic.stack import Stack
 2 
 3 def parChecker(symbolString):
 4     s = Stack()
 5     balanced = True
 6     index = 0
 7     while index < len(symbolString) and balanced:
 8         symbol = symbolString[index]
 9         if symbol in "([{":
10             s.push(symbol)
11         else:
12             if s.isEmpty():
13                 balanced = False
14             else:
15                 top = s.pop()
16                 if not matches(top,symbol):
17                        balanced = False
18         index = index + 1
19     if balanced and s.isEmpty():
20         return True
21     else:
22         return False
23 
24 def matches(open,close):
25     opens = "([{"
26     closers = ")]}"
27     return opens.index(open) == closers.index(close)
28 
29 
30 print(parChecker({{([][])}()}))
31 print(parChecker([{()]))

 

 

Decimal Numbers to Binary Numbers(Divid by 2)

技术分享

 

The Divide by 2 algorithm assumes that we start with an integer greater than 0. A simple iteration then continually divides the decimal number by 2 and keeps track of the remainder. The first division by 2 gives information as to whether the value is even or odd. An even value will have a remainder of 0. It will have the digit 0 in the ones place. An odd value will have a remainder of 1 and will have the digit 1 in the ones place. We think about building our binary number as a sequence of digits; the first remainder we compute will actually be the last digit in the sequence. 

 

from pythonds.basic.stack import Stack

def divideBy2(decNumber):
    remstack = Stack()

    while decNumber > 0:
        rem = decNumber % 2
        remstack.push(rem)
        decNumber = decNumber // 2

    binString = ""
    while not remstack.isEmpty():
        binString = binString + str(remstack.pop())

    return binString

print(divideBy2(42))

 

 

 

 

Divid by Base

 1 from pythonds.basic.stack import Stack
 2 
 3 def baseConverter(decNumber,base):
 4     digits = "0123456789ABCDEF"
 5 
 6     remstack = Stack()
 7 
 8     while decNumber > 0:
 9         rem = decNumber % base
10         remstack.push(rem)
11         decNumber = decNumber // base
12 
13     newString = ""
14     while not remstack.isEmpty():
15         newString = newString + digits[remstack.pop()]
16 
17     return newString
18 
19 print(baseConverter(25,2))
20 print(baseConverter(25,16))

 

Algorithm - Stack Exercise

标签:

原文地址:http://www.cnblogs.com/elewei/p/5625410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!