def create_lists_1(r): q=deque([]) level=deque([]) lists=[] q.append(r) level.append(0) level_list=LinkList() level_list.append_node(r) while q: current_node=q.popleft() current_level=level.popleft() if current_node.left: q.append(current_node.left) level.append(current_level+1) if current_node.right: q.append(current_node.right) level.append(current_level+1) if q: # if this node is at the same level of the previous node, then add this node to the list if level[0]==current_level: level_list.append_node(q[0]) # else, then pack the list to lists, create a new list, and add this node to the list else: lists.append(level_list) level_list=LinkList() level_list.append_node(q[0]) # if it's the last element, pack to lists else: lists.append(level_list) return lists
def __init__(self): self.eliminatedPlayers = [] ##check if eliminated, check if p1.hasTurn self.removedCards = [] self.currentStack = LinkList() self.players = [] #arranged by card rank self.deck = None self.currTurn = 0 self.sevenflag = None #high or low
def create_lists_2(r): lists=[] current=LinkList() if r: current.append_node(r) while len(current)>0: lists.append(current) # pack to lists parents=current # go one level down current=LinkList() # create a new list for parent in parents: if parent.left: current.append_node(parent.left) if parent.right: current.append_node(parent.right) return lists
class Deck: suits = ['S', 'D', 'C', 'H'] numbers = [ 'a', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k' ] deck = LinkList() def __init__(self): ##new deck order for a in self.suits: for b in self.numbers: self.deck.add(b + a) #self.deck.append(b+a) def shuffle(self): for i in range(0, 51): random = randint(i + 1, 51) self.deck.swap(i, random) def swap(self, a, b): temp = self.deck[a] self.deck[a] = self.deck[b] self.deck[b] = temp def show(self): self.deck.show() def draw(self): return self.deck.removeHead()
def main(): print "main" linkList = LinkList() for item in data: linkList.addData(item) print "\n\ndisplay list as inserted" linkList.dump() print "\n\ndispaly list reversed" linkList.reverse() linkList.dump()
class Player: def __init__(self, n): #use `self` to attach variable to this player #otherwise its like a static variable #every inst of player points to the same variable/memory self.name = n self.hand = LinkList() self.hiddenStack = LinkList() self.shownStack = LinkList() self.hasTurn = False def hide(self, c): self.hiddenStack.add(c) #3 cards to be shown def place(self, c): self.shownStack.add(c) def pickup(self, stack): for i in stack: self.hand.add(i) def play(self, card): return self.hand.removeAt(card) def isReady(self): if self.hiddenStack.length is 3 and self.shownStack is 3 and self.hand.length is 3: return True return False
def __init__(self, n): #use `self` to attach variable to this player #otherwise its like a static variable #every inst of player points to the same variable/memory self.name = n self.hand = LinkList() self.hiddenStack = LinkList() self.shownStack = LinkList() self.hasTurn = False
def test_link_list(self): lst = LinkList() a = (3, 35, -5, 7, 10) lst.create(a) lst.head.print() p = lst.head for b in a: assert b == p.val p = p.next new = 55 lst.insert(1, new) p = lst.head p = p.next assert new == p.val
def create_lists_3_helper(r, lists, level): if not r: return # if the level is not visited before, create a new list if level>len(lists)-1: current_list=LinkList() current_list.append_node(r) lists.append(current_list) # else, get the list, append the tree node else: current_list=lists[level] current_list.append_node(r) create_lists_3_helper(r.left, lists, level+1) create_lists_3_helper(r.right, lists, level+1)
from LinkList import LinkList l1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] l2 = [2] list1 = LinkList() list2 = LinkList() list1.from_list(l1) list2.from_list(l2) # print list2 def find(headA, headB): if not headA or not headB: return None nodeA = headA while nodeA.next: nodeA = nodeA.next print nodeA nodeA.next = headA slow = fast = headB while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: break if not fast or not fast.next: nodeA.next = None return None
def __init__(self, max_size): self.table = [LinkList() for i in range(max_size)] self.size = 0
class Game: def __init__(self): self.eliminatedPlayers = [] ##check if eliminated, check if p1.hasTurn self.removedCards = [] self.currentStack = LinkList() self.players = [] #arranged by card rank self.deck = None self.currTurn = 0 self.sevenflag = None #high or low def addPlayer(self, p): #how many players? -- 2-4? #add players in as they join the server self.players.append(Player(p)) def start(self): self.deck = Deck() self.deck.shuffle() #deal out 9 cards to each player for p in self.players: count = 0 while count < 9: if count <= 2: p.hiddenStack.add(self.deck.draw()) else: p.hand.add(self.deck.draw()) count += 1 self.players[0].shownStack.add(self.players[0].hand.removeHead()) self.players[0].shownStack.add(self.players[0].hand.removeHead()) self.players[0].shownStack.add(self.players[0].hand.removeHead()) self.players[1].shownStack.add(self.players[1].hand.removeHead()) self.players[1].shownStack.add(self.players[1].hand.removeHead()) self.players[1].shownStack.add(self.players[1].hand.removeHead()) #determine and order players with the lowest card ASC #self.sortPlayers() def sortPlayers(self): newOrder = {} for i in self.players: mini = 'aS' curr = i.hand.head while curr != None: if Game.getValue(curr.val) < Game.getValue(mini): mini = curr.val curr = curr.next newOrder[Game.getValue(mini)] = i print(newOrder) self.players = list(newOrder.values()) print(self.players) self.players[0].hand.show() print() self.players[1].hand.show() #i.hand.show() def rotateTurn(self): if self.currTurn is 3: self.currTurn = 0 else: self.currTurn += 1 def calcCards(self, prevCard, card, sevenFlag=''): ##returns true from following these rules:: #if fourOfAKind #if a seven was previously played #if card is a 7 #if a '10' was played #if a '2' was played #if the card played is higher than the previous card value = False if self.isFourofAKind(card): curr = self.currentStack.head while curr != None: self.removedCards.append(self.currentStack.removeHead()) curr = curr.next self.removedCards.append(card) value = True #player goes again elif Game.getValue(prevCard) is 7: if self.sevenflag is 'high': value = card > 7 else: value = card < 7 elif Game.getValue(card) is 7: if self.sevenflag is not None: self.sevenflag = sevenFlag else: print('error') #rotateTurn +1 elif Game.getValue(card) is 10: curr = self.currentStack.head while curr != None: self.removedCards.append(self.currentStack.removeHead()) curr = curr.next self.removedCards.append(card) value = True #player goes again elif Game.getValue(card) is 2: #player plays again pass print('static') else: value = Game.getValue(card) > Game.getValue(prevCard) return value def isFourofAKind(self, card): #look at the previous 3 cards curr = self.currentStack.head value = False count = 0 while curr != None: if card is curr.val: count += 1 if count is 3: value = True break curr = curr.next return value def searchPlayer(self, name): for p in self.players: if name == p.name: return True return False @staticmethod def getValue(card): val = 0 if len(card) is 3: val = int(card[:2]) else: try: val = int(card[0]) except Exception: print('here') val = values[card[0]] return val
from LinkList import LinkList, ListNode list1 = LinkList() list1.append(ListNode(1)) list1.append(ListNode(2)) list1.append(ListNode(3)) list1.append(ListNode(3)) list1.append(ListNode(4)) list1.append(ListNode(4)) list1.append(ListNode(5)) list1.append(ListNode(6)) list1.append(ListNode(7)) list1.append(ListNode(7)) list1.append(ListNode(8)) list1.append(ListNode(9)) list1.append(ListNode(9)) list1.append(ListNode(9)) def removeDuplicate(head): lastVal = None current = head.next while current: nextNode = current.next if current.val == lastVal: current.next = nextNode.next else: lastVal = current.val current = nextNode return head
#-*-coding:utf-8 -*- # 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 from LinkList import LinkList, ListNode list1 = LinkList() list2 = LinkList() list1.append(ListNode(1)) list1.append(ListNode(3)) list1.append(ListNode(5)) list1.append(ListNode(7)) list1.append(ListNode(9)) list2.append(ListNode(2)) list2.append(ListNode(4)) list2.append(ListNode(6)) list2.append(ListNode(8)) list2.append(ListNode(10)) def mergeLinkList(list1, list2): head = list1.head current = head current1 = list1.head.next current2 = list2.head.next while current1 and current2: if current1.val < current2.val: