标签:
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(‘(()‘))
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(‘[{()]‘))
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))
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))
标签:
原文地址:http://www.cnblogs.com/elewei/p/5625410.html