高级农民
- 积分
- 2569
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-3-10
- 最后登录
- 1970-1-1
|
Reverse Polish Notation
- from abc import abstractmethod
- class Token(object):
- @abstractmethod
- def isValue(self):
- pass
- @abstractmethod
- def execute(self, stack):
- pass
- class Operand(Token):
- def __init__(self, value=None):
- self.value = value
- def isValue(self):
- return True
- def getValue(self):
- return self.value
- def setValue(self, x):
- self.value = x
- def execute(self, stack):
- stack.append(self.value)
- class Operator(Token):
- def __init__(self, mark=None, cal=None):
- self.mark = mark
- self.cal = cal
- def isValue(self):
- return False
- def getOperation(self):
- return self.mark
- def execute(self, stack):
- try:
- num2 = stack.pop()
- num1 = stack.pop()
- except:
- print 'Invalid notation!'
- raise
- res = self.cal(num1, num2)
- stack.append(res)
- class OperationFactory(object):
- def __init__(self):
- self.map = {}
- self.map['+'] = lambda x, y: x+y
- self.map['-'] = lambda x, y: x-y
- self.map['*'] = lambda x, y: x*y
- self.map['/'] = lambda x, y: x/y if x*y>0 else -(abs(x)/abs(y))
- def create(self, s):
- if s not in self.map:
- return Operand(int(s))
- else:
- return Operator(s, self.map[s])
- if __name__ == "__main__":
- inputList = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
- # inputList = ['10', '3', '+', '7', '-']
- factory = OperationFactory()
- stack = []
- for i in inputList:
- token = factory.create(i)
- token.execute(stack)
- print stack
复制代码
Delete substree
- class Node:
- def __init__(self, index, value, valid=True):
- self.parentIndex = index
- self.value = value
- self.valid = valid
- self.visited = False
- def deleteNode(nodes, index):
- nodes[index].valid = False
- nodes[index].visited = True
- for i in range(len(nodes)):
- if nodes[i].visited:
- continue
- search(nodes, i)
- def search(nodes, index):
- if nodes[index].parentIndex == -1 or nodes[index].visited:
- nodes[index].visited = True
- return nodes[index].valid
- nodes[index].visited = True
- nodes[index].valid = search(nodes, nodes[index].parentIndex)
- return nodes[index].valid
- A = Node(-1, 'A')
- B = Node(4, 'B')
- C = Node(1, 'C')
- D = Node(1, 'D')
- E = Node(4, 'E')
- F = Node(6, 'F')
- G = Node(3, 'G')
- nodes = [C, B, G, D, A, F, E]
- deleteNode(nodes, 0)
- for i in range(len(nodes)):
- print nodes[i].value, nodes[i].valid
复制代码
Timestamp Stream Pairs
- from threading import Thread
- import threading
- class Stream:
- def __init__(self, nums):
- self.nums = nums
- self.index = 0
- def getNext(self):
- if self.index < len(self.nums):
- self.index += 1
- return self.nums[self.index-1]
- else:
- raise
- res = []
- q1 = []
- q2 = []
- threadLock = threading.Lock()
- def calculatePairs(q1, q2, value):
- q1.append(value)
- # print q1, q2
- while q2 and value - q2[0] >= 1:
- q2.pop(0)
- for num in q2:
- if abs(num - value) < 1:
- res.append((value, num))
- s1 = Stream([0.2, 1.4, 3.0])
- s2 = Stream([2.0, 2.1, 4.5])
- def Q1Thead(s):
- while True:
- try:
- value = s.getNext()
- threadLock.acquire()
- calculatePairs(q1, q2, value)
- threadLock.release()
- except:
- break
- def Q2Thead(s):
- while True:
- try:
- value = s.getNext()
- threadLock.acquire()
- calculatePairs(q2, q1, value)
- threadLock.release()
- except:
- break
- try:
- new_thread1=Thread(target = Q1Thead, args = (s1,))
- new_thread1.setDaemon(True)
- new_thread1.start()
- new_thread2=Thread(target = Q2Thead, args = (s2,))
- new_thread2.setDaemon(True)
- new_thread2.start()
- except:
- print "Error: unable to start thread"
- print res
复制代码
Wildcard Matching
- class Solution(object):
- def isMatch(self, s, p):
- """
- :type s: str
- :type p: str
- :rtype: bool
- """
- sIndex = pIndex = 0
- sMatch = 0
- pStar = -1
- while sIndex < len(s):
- if pIndex < len(p) and (p[pIndex] == s[sIndex] or p[pIndex] == '?'):
- sIndex += 1
- pIndex += 1
- elif pIndex < len(p) and p[pIndex] == '*':
- sMatch = sIndex
- pStar = pIndex
- pIndex += 1
- elif pStar != -1:
- sMatch += 1
- sIndex = sMatch
- pIndex = pStar + 1
- else:
- return False
-
- while pIndex < len(p) and p[pIndex] == '*':
- pIndex += 1
-
- return pIndex == len(p)
复制代码 |
|