def testAB(self): '''Flush_testAB: Two cards different suits should return nothing''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,2)) result = hands.toMakeFlush(teststack) self.assertEqual(result,stacks.stack())
def toMakeStraight(cards): if not isinstance(cards,stacks.stack): raise NotImplementedError, "toMakeStraight only works on stacks" rset = stacks.stack() exclude = stacks.stack() rset.populate("Full Deck") if (len(cards) < 1): #No cards played yet - anything will work return rset if (len(cards) > 4): #Five cards played - nothing else will work rset.clear() return rset if len(cards) == 1 and list(cards)[0].rank() == 1: #Special case for single ace rset.clear() for rank in range(10,14)+range(2,6): rset.populate("Rank",rank) return rset if len(cards) == 4 and len(toMakeRoyalFlush(cards)) != 0: exclude = toMakeRoyalFlush(cards) invalid = set([-3,-2,-1,0,15,16,17,18]) grouped = groups(cards) wset = stacks.stack() maxgroup = max([len(v) for v in grouped]) #Calculates the number of cards in the rank with the most cards if maxgroup > 1: #If there's more than one in the maxgroup, can't continue to make a straight rset.clear() return rset if len(cards) == 4: #Could bump into a straight flush flushed = flushes(cards) maxflush = max([len(v) for v in flushed]) if maxflush >= 4: for card in cards: wset.populate("Suit",card) #Calculate max and min rank ranks = [group[0].rank() for group in grouped if len(group) != 0] mn = min(ranks) mx = max(ranks) ranks.sort() if 1 in ranks: spanlo = abs(1-max(ranks)) spanhi = abs(ranks[1]-14) if min(spanhi,spanlo) == spanlo: mx = max(ranks) mn = 1 else: mx = 14 mn = ranks[1] span = abs(mx-mn) else: span = abs(min(ranks)-max(ranks)) if span > 4: #No good - cards we have are too far apart rset.clear() return rset rset.clear() adj = 4-span for rank in set(range(mn-adj,mx+adj+1)).difference(invalid): rset.populate("Rank",rank) for card in cards: wset.populate("Rank",card.rank()) return rset.difference(wset).difference(exclude)
def setUp(self): self.fulldeck = stacks.stack() self.fulldeck.populate("Full Deck") self.emptystack = stacks.stack() self.teststack = stacks.stack() self.workstack = stacks.stack() self.goodstack = stacks.stack()
def testAAA(self): '''Pair_testAAA: Should return nothing (more than a pair)''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(1,3)) result = hands.toMakePair(teststack) self.assertEqual(result,stacks.stack())
def testABC(self): '''FourOAK_testABC: Should return nothing''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,1)) teststack.add(card(3,1)) result = hands.toMakeFourOAK(teststack) self.assertEqual(result,stacks.stack())
def testAAAB(self): '''TwoPair_testAAAB: Should return nothing''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(1,3)) teststack.add(card(2,1)) result = hands.toMakeTwoPair(teststack) self.assertEqual(result,stacks.stack())
def testA(self): '''Pair_testA: Should return full deck except A''' teststack = stacks.stack() teststack.add(card(1,1)) result = hands.toMakePair(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference([card(1,1)]) self.assertEqual(result,workstack)
def testFourCardRF(self): '''RoyalFlush_testFourcardRF: Should return the one remaining royal flush card''' teststack = stacks.stack() teststack.populate("Face Cards",1) teststack.add(card(10,1)) result = hands.toMakeRoyalFlush(teststack) goodstack = stacks.stack() goodstack.add(card(1,1)) self.assertEqual(result,goodstack)
def testNonRFCards(self): '''RoyalFlush_testNonRFCards: Should return nothing since a 2-9 can't make a royal flush''' teststack = stacks.stack() for rank in range(2,10): for suit in range(1,5): teststack.clear() teststack.add(card(rank,suit)) result = hands.toMakeRoyalFlush(teststack) self.assertEqual(result,stacks.stack())
def testA(self): '''FourOAK_testA: Should return all cards except A''' teststack = stacks.stack() teststack.add(card(1,1)) result = hands.toMakeFourOAK(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference(teststack) self.assertEqual(result,workstack)
def testABCD(self): '''FullHouse_testABCD: Should return nothing''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,1)) teststack.add(card(3,1)) teststack.add(card(4,1)) result = hands.toMakeFullHouse(teststack) self.assertEqual(result,stacks.stack())
def testA(self): '''Flush_testA: Single card should return all cards in that suit minus card given''' teststack = stacks.stack() teststack.add(card(1,1)) result = hands.toMakeFlush(teststack) goodstack = stacks.stack() goodstack.populate("Suit",card(1,1)) goodstack = goodstack.difference([card(1,1)]) self.assertEqual(result,goodstack)
def testAABB(self): '''ThreeOAK_testAABB: Should return nothing (anything from either rank makes a full house)''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(2,1)) teststack.add(card(2,2)) result = hands.toMakeThreeOAK(teststack) self.assertEqual(result,stacks.stack())
def testAAAA(self): '''ThreeOAK_testAAAA: Should return nothing''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(1,3)) teststack.add(card(1,4)) result = hands.toMakeThreeOAK(teststack) self.assertEqual(result,stacks.stack())
def testFive(self): '''Pair_testFive: Should return nothing''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,2)) teststack.add(card(3,3)) teststack.add(card(4,4)) teststack.add(card(5,5)) result = hands.toMakePair(teststack) self.assertEqual(result,stacks.stack())
def testAB(self): '''TwoPair_testAB: Should return all cards except A and B''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,2)) result = hands.toMakeTwoPair(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference(teststack) self.assertEqual(result,workstack)
def testAA(self): '''Pair_testAA: Should return full deck minus cards in A rank''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) result = hands.toMakePair(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference([card(1,1),card(1,2),card(1,3),card(1,4)]) self.assertEqual(result,workstack)
def testAB(self): '''Pair_testAB: Should return full deck minus cards given''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,2)) result = hands.toMakePair(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference(teststack) self.assertEqual(result,workstack)
def testFive(self): '''Flush_testFive: Five cards should return nothing (even if they're the same suit)''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,1)) teststack.add(card(3,1)) teststack.add(card(4,1)) teststack.add(card(5,1)) result = hands.toMakeFlush(teststack) self.assertEqual(result,stacks.stack())
def testAAB(self): '''ThreeOAK_testAAB: Should return all cards except input''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(2,1)) result = hands.toMakeThreeOAK(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference(teststack) self.assertEqual(result,workstack)
def testAB(self): '''FullHouse_testAB: Should return cards in A/B rank except A/B''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,1)) result = hands.toMakeFullHouse(teststack) workstack = stacks.stack() workstack.populate("Rank",card(1,1)) workstack.populate("Rank",card(2,1)) workstack = workstack.difference([card(1,1),card(2,1)]) self.assertEqual(result,workstack)
def testAAAB(self): '''FourOAK_testAAAB: Should only return remaining card in same rank as A's''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(1,3)) teststack.add(card(2,1)) result = hands.toMakeFourOAK(teststack) workstack = stacks.stack() workstack.add(card(1,4)) self.assertEqual(result,workstack)
def testAB(self): '''FourOAK_testAB: Should return cards from both A/B ranks except A/B''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(2,1)) result = hands.toMakeFourOAK(teststack) workstack = stacks.stack() workstack.populate("Rank",card(1,1)) workstack.populate("Rank",card(2,1)) workstack = workstack.difference(teststack) self.assertEqual(result,workstack)
def testAAA(self): '''FullHouse_testAAA: Should return all cards except those in A rank''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(1,3)) result = hands.toMakeFullHouse(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference(teststack).difference([card(1,4)]) self.assertEqual(result,workstack)
def testAABC(self): '''twoPair_testAABC: Should return cards in B or C's ranks except B or C''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(2,1)) teststack.add(card(3,1)) result = hands.toMakeTwoPair(teststack) workstack = stacks.stack() workstack.populate("Rank",card(2,1)) workstack.populate("Rank",card(3,1)) self.assertEqual(result,workstack.difference(teststack))
def testAAAB(self): '''ThreeOAK_testAAAB: Should return all cards except A/B ranks ranks (avoid full house/4OAK)''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(1,3)) teststack.add(card(2,1)) result = hands.toMakeThreeOAK(teststack) workstack = stacks.stack() workstack.populate("Full Deck") workstack = workstack.difference(teststack).difference([card(1,4),card(2,2),card(2,3),card(2,4)]) self.assertEqual(result,workstack)
def testAABC(self): '''ThreeOAK_testAABC: Should return remaining cards in rank of A''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(1,2)) teststack.add(card(2,1)) teststack.add(card(3,1)) result = hands.toMakeThreeOAK(teststack) workstack = stacks.stack() workstack.add(card(1,3)) workstack.add(card(1,4)) self.assertEqual(result,workstack)
def testAAAA(self): '''Flush_testAAAA: Four of same suit should return all cards in that suit except cards given''' teststack = stacks.stack() teststack.add(card(3,1)) teststack.add(card(11,1)) teststack.add(card(12,1)) teststack.add(card(13,1)) result = hands.toMakeFlush(teststack) goodstack = stacks.stack() goodstack.populate("Suit",card(1,1)) goodstack = goodstack.difference([card(3,1),card(11,1),card(12,1),card(13,1)]) self.assertEqual(result,goodstack)
def testFourCardRoyalFlush(self): '''Flush_testFourCardRoyalFlush: Four of same suit that are in 10-J-Q-K-A should return all in suit except one that completes the royal flush''' teststack = stacks.stack() teststack.add(card(1,1)) teststack.add(card(10,1)) teststack.add(card(11,1)) teststack.add(card(12,1)) result = hands.toMakeFlush(teststack) workstack = stacks.stack() workstack.populate("Suit",1) workstack = workstack.difference([card(1,1),card(10,1),card(11,1),card(12,1),card(13,1)]) self.assertEqual(result,workstack)
def testFourCardOutsideStraightFlush(self): '''Flush_testFourCardOutsideStraightFlush: Four of same suit in a straight should return all in suit except outside card(s)''' teststack = stacks.stack() teststack.add(card(3,1)) teststack.add(card(4,1)) teststack.add(card(5,1)) teststack.add(card(6,1)) result = hands.toMakeFlush(teststack) workstack = stacks.stack() workstack.populate("Suit",1) workstack = workstack.difference([card(2,1),card(3,1),card(4,1),card(5,1),card(6,1),card(7,1)]) self.assertEqual(result,workstack)
def main(): e0 = element(0) e1 = element(1) e2 = element(2) e3 = element(3) e4 = element(4) e5 = element(5) #Linked list check # ll = linked_list(e1) # ll.append(e2) # ll.append(e3) # ll.append(e5) # print(ll.get_position(3).value) # ll.insert(e4,3) # print(ll.get_position(3).value) # print(ll.get_position(4).value) # ll.insert(e0,0) # print(ll.get_position(0).value) # print(ll.get_position(1).value) # print() # print() # ll.delete(3) # print(ll.get_position(3).value) # print(ll.get_position(4).value) # ll.delete(0) # print(ll.get_position(0).value) #stack check ss = stack(e0) ss.push(e1) ss.push(e2) print(ss.pop()) print(ss.pop())
def infixToPostfix(infixExpr): prec = {"*":3,"/":3,"+":2,"-":2,"(":1} # prec = {} # prec["*"] = 3 # prec["/"] = 3 # prec["+"] = 2 # prec["-"] = 2 prec["("] = 1 opStack = stacks.stack() postfixList = [] tokenList = infixExpr.split() for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVIWXYZlmnop" or token in "0123456789": postfixList.append(token) elif token == '(': opStack.push(token) elif token == ')': topToken = opStack.pop() while topToken != '(': postfixList.append(topToken) topToken = opStack.pop() else: while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]): postfixList.append(opStack.pop()) opStack.push(token) while not opStack.isEmpty(): postfixList.append(opStack.pop()) return "".join(postfixList)
def __init__(self): self.bfs_list = [] self.graph = {} self.stacks = stacks.stack() self.sequences = [] self.count = 0 self.bfs = bfsPro.bfs()
def blend(A, B, levels, l, g, mask): LA = stacks.stack(A, levels=levels, mode=stacks.StackMode.Laplace, kernel=l) LB = stacks.stack(B, levels=levels, mode=stacks.StackMode.Laplace, kernel=l) GM = stacks.stack(mask, levels=levels, mode=stacks.StackMode.Gauss, kernel=g) idx = tuple([slice(None)] * len(LA.shape)) LS = np.zeros_like(LA) LS[idx] = GM[idx] * LA[idx] + (1 - GM[idx]) * LB[idx] S = np.clip(np.sum(LS, axis=len(LS.shape) - 1), 0, 1) return S, LA, LB, LS, GM
def postfixEval(postfixExpr): operandStack = stacks.stack() tokenList = postfixExpr.split() for token in tokenList: if token in "0123456789": operandStack.push(int(token)) else: operand2 = operandStack.pop() operand1 = operandStack.pop() result = doMath(token,operand1,operand2) operandStack.push(result) return operandStack.pop()
def divideBy2(decNumber): remStack = stacks.stack() while decNumber > 0: rem = decNumber % 2 remStack.push(rem) decNumber = decNumber // 2 binaryString = "" while not remStack.isEmpty(): binaryString += str(remStack.pop()) return binaryString
def baseConverter(decNumber, base): digits = "0123456789ABCDEF" remStack = stacks.stack() while decNumber > 0: rem = decNumber % base remStack.push(rem) decNumber = decNumber // base newString = "" while not remStack.isEmpty(): newString += digits[remStack.pop()] return newString
def string_balanced(strInput): st = stack() count = 1 for chars in strInput: st.push(chars) popped = st.pop() while st.peek() == popped: count += 1 popped = st.pop() if count == st.size(): return True else: return False
def sort_stack(st): # Helper stack for sorting helper_st = stack() while not st.is_empty(): # Insert each element in st into helper_st in sorted order buffer = st.pop() while not helper_st.is_empty() and helper_st.peek() > buffer: st.push(helper_st.pop()) helper_st.push(buffer) # Copy the elements back to st while not helper_st.is_empty(): st.push(helper_st.pop()) return st
def parCheckers(symbolString): S = stacks.stack() balanced = True index = 0 while balanced and index < len(symbolString): symbol = symbolString[index] if symbol in "([{": S.push(symbol) else: if S.isEmpty(): balanced = False else: top = S.pop() if not matches(top,symbol): balanced = False index += 1 if balanced and S.isEmpty(): return True else: return False
class bd: g = stacks.stack() g.push(1) print g.pop()
def __init__(self): self.q=stacks.stack() self.graphs={} self.graph_vertex=[] self.bfslist=[] self.visted_list=[]
def __init__(self): self.s1 = stack() self.s2 = stack()
from stacks import stack def sort_stack(st): # Helper stack for sorting helper_st = stack() while not st.is_empty(): # Insert each element in st into helper_st in sorted order buffer = st.pop() while not helper_st.is_empty() and helper_st.peek() > buffer: st.push(helper_st.pop()) helper_st.push(buffer) # Copy the elements back to st while not helper_st.is_empty(): st.push(helper_st.pop()) return st if __name__ == "__main__": st = stack() st.push(8) st.push(3) st.push(4) st.push(5) st.push(7) sort_stack(st).traverse()