def test_clear(self): temp = Stack() for i in range(100): temp.push(i) self.assertEqual(temp.outputStack(), list(range(100))[::-1]) temp.clear() self.assertEqual(temp.outputStack(), [])
def infix_to_postfix(self): """ Prevod infixniho vyrazu na postfixni """ data = self.get_data() infix_arr = [] postfix_arr = [] val = "" for item in data: if self.is_operation(item): if val != "": infix_arr.append(float(val)) infix_arr.append(item) val = "" elif item == "(": infix_arr.append(item) elif item == ")": if val != "": infix_arr.append(float(val)) infix_arr.append(item) val = "" else: val += item if val != "": infix_arr.append(float(val)) stack = Stack() for item in infix_arr: if self.is_operation(item): if self.get_operation_pritorty( item) < self.get_operation_pritorty(stack.first()): for op in stack.to_list(): if op == "(": continue postfix_arr.append(op) stack.clear() stack.push(item) elif item == "(": stack.push(item) elif item == ")": while stack.first() != "(": op = stack.pop() postfix_arr.append(op) else: postfix_arr.append(item) for op in stack.to_list(): if op == "(": continue postfix_arr.append(op) return " ".join(map(str, postfix_arr))
def test_clear(self): ''' Test peek :return: ''' s = Stack() s.push("One") s.push("Two") self.assertEquals(False,s.empty()) s.clear() self.assertEquals(True,s.empty())
def test_clear(self): stack = Stack() stack.push("Ned") stack.clear() self.assertEqual(stack.get_stack(), [])
class Trashcan: def __init__(self, rounds): self.rounds = rounds self.player_1_round = 10 # A counter to keep track of which round each person is on self.player_2_round = 10 self.draw_pile = Stack() self.burn_pile = Stack() self.player_1_hand = Queue() self.player_2_hand = Queue() self.cards = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Standard suit of cards self.cards_s = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] self.card_names = [ "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" ] # card name equivalents self.player_1_field = [] self.player_2_field = [] def create_draw_pile(self): # This code was reused from War.py """ Adds cards to dealing pile :return: None """ for i in range(4): for l in self.cards: self.draw_pile.push(l) self.draw_pile.shuffle() def draw(self, hand): """ Takes a card from the top of the draw_pile and enqueues that card into the the provided player's hand :param hand: A queue for a player's cards held in their hand :return: None """ card = self.draw_pile.pop() hand.enqueue(card) def create_playing_field( self, n, hand, field): # This is the code I did BigO analysis for """ Draws 10 cards to each player from the draw_pile. Then the playing fields get the cards from each players hand :return: None """ while hand.size() < n: self.draw(hand) while hand.size() != 0: field.append(hand.dequeue()) def placing_phase(self, hand, field, player_round): """ Draws a card and places it in the appropriate spot. If a King is drawn, prompts the user to provide a slot to place it in. If a Jack or Queen is drawn, the cycle ends. If a drawn card can not be placed, the cycle ends. :param hand: The hand of the current player :param field: The field of the current player :param player_round: what round the player is on :return: None """ if self.draw_pile.size( ) == 0: # If the draw pile is empty, it is refreshed using the burn pile self.refresh() self.draw(hand) while True: card = hand.dequeue() if card == 0 or card == 7: # Code to print out what was drawn using an if the card is an Ace or 8 input("An " + self.card_names[card] + " was drawn.") else: # Code to print out what was drawn for all other cards input("A " + self.card_names[card] + " was drawn.") if player_round <= card < 12: # If the card is a Jack or Queen print("Nothing could be done. . .") break elif card == 12 or card == "King": # If the card is a King print( "Here is your current field:" ) # Just a reminder to the player of what they have so far. print(field) choice = input( "Choose a number between 1 and 10 to place the king in that spot." ) # Asks where the user wants the King while True: while choice not in self.cards_s: print("That was an invalid choice.") choice = input( "Choose a number between 1 and 10 to place the king in that spot" ) choice = int(choice) if isinstance( field[choice - 1], str ): # Checks to see if the slot has been filled by checking if the type is a string choice = input( "That spot has already been filled with the correct card. Please provide " "another number") else: hand.enqueue(field[choice - 1]) # Fills the slot with a King field[choice - 1] = "King" break elif field[ card] == "King": # Checks to see if the slot has a King so the King can be reused hand.enqueue(12) field[card] = self.card_names[card] print(" " + str(field)) input("") elif not isinstance( field[card], str ): # If it is not a King, this checks if the slot can be filled with the card hand.enqueue(field[card]) field[card] = self.card_names[card] print(" " + str(field)) input("") else: # If nothing else can be done, the card in the hand is put in the burn pile and the cycle ends print("Nothing could be done. . .") self.burn_pile.push(card) break def check_field(self, field): """ Checks a field to see if it is been completed. :param field: field to be checked :return: A boolean """ for i in field: if not isinstance(i, str): return True return False def reset(self): """ Clears all of the hands, piles, and fields. :return: None """ del self.player_1_field[:] del self.player_2_field[:] self.draw_pile.clear() self.burn_pile.clear() self.player_1_hand.clear() self.player_2_hand.clear() def refresh(self): """ Moves all cards from the burn pile to the draw pile :return: None """ for i in range(self.burn_pile.size()): self.draw_pile.push(self.burn_pile.pop()) self.draw_pile.shuffle() def end_of_round(self): """ Prints out the progress of each player and calls reset() to end the round. :return: None """ print("Player 1 has " + str(self.player_1_round) + " cards to fill and Player 2 has " + str(self.player_2_round) + " cards to fill.") self.reset() def play(self): """ Creates the drawing pile, the fields, and goes through the placing phase until a player completes their field. Whichever players completed their field move on to the next round with 1 less slot to fill. :return: A Boolean """ self.create_draw_pile() self.create_playing_field(self.player_1_round, self.player_1_hand, self.player_1_field) self.create_playing_field(self.player_2_round, self.player_2_hand, self.player_2_field) while True: input("Player 1's turn. Press enter to start.") self.placing_phase(self.player_1_hand, self.player_1_field, self.player_1_round) input("Player 2's turn. Press enter to start.") self.placing_phase(self.player_2_hand, self.player_2_field, self.player_2_round) if not self.check_field( self.player_1_field) and not self.check_field( self.player_2_field ): # Checks if both players finished self.player_1_round -= 1 self.player_2_round -= 1 break elif not self.check_field( self.player_1_field ): # If both players didn't finish, checks if Player 1 finished self.player_1_round -= 1 break elif not self.check_field( self.player_2_field): # Checks if Player 2 self.player_2_round -= 1 break if self.player_1_round == 10 - self.rounds: # Checks if player 1 has won print("Player 1 has won!") return False elif self.player_2_round == 10 - self.rounds: # Checks if player 2 has won print("Player 2 has won!") return False else: # If neither player has won yet, this ends the round self.end_of_round() return True
class TestStack(unittest.TestCase): def setUp(self): self.instance =Stack() def testClear(self): self.instance.clear() self.assertEqual(0,self.instance.size) self.assertEqual([],self.instance.Stack) def testContain(self): self.assertFalse(self.instance.contains('string')) self.assertFalse(self.instance.contains('')) self.assertFalse(self.instance.contains(None)) self.instance.push("string1") self.instance.push("string2") self.instance.push("string1") self.assertTrue(self.instance.contains("string1")) def testPeek(self): self.assertRaises(StackException,self.instance.peek) self.instance.push("string1") self.instance.push("string2") self.assertEqual("string2",self.instance.peek()) self.assertEqual(2,self.instance.size) self.assertEqual(["string1","string2"],self.instance.Stack) def testPush(self): self.instance.push("string1") self.instance.push("string2") self.instance.push("string3") self.assertEqual(["string1","string2","string3"],self.instance.Stack) self.instance.push("") self.instance.push("") self.assertEqual(["string1","string2","string3","",""],self.instance.Stack) self.instance.push(None) self.instance.push("string1") self.assertEqual(["string1","string2","string3","","",None,"string1"],self.instance.Stack) def testPop(self): self.assertRaises(StackException,self.instance.pop) self.instance.push("string1") self.instance.push("string2") popvalue= self.instance.pop() self.assertEqual(["string1"],self.instance.Stack) self.assertEqual(1,self.instance.size) self.assertEqual(popvalue,"string2") popvalue= self.instance.pop() self.assertEqual([],self.instance.Stack) self.assertEqual(0,self.instance.size) self.assertEqual(popvalue,"string1") self.assertRaises(StackException,self.instance.pop) def testIsEmpty(self): self.assertTrue(self.instance.isEmpty()) self.instance.push("string1") self.assertFalse(self.instance.isEmpty()) self.instance.pop() self.assertTrue(self.instance.isEmpty())
arr.push(5) print(arr) arr.push(7) print(arr) arr.push(8) print(arr) arr.push(10) print(arr) arr.__len__() print(arr.__len__()) arr.__str__() print(arr.__str__()) print(arr.size()) # arr.pop() # print(arr) # arr.pop() # print(arr) # arr.pop() # print(arr) # arr.pop() # print(arr) # arr.pop() # print(arr) # arr.pop() # print(arr) print(arr.peek()) print(arr.isContain(3)) print(arr.isContain(4)) print(arr) arr.clear() print(arr)
class TestStack(unittest.TestCase): def setUp(self): self.stack = Stack() def tearDown(self): self.stack = None def test_push_once(self): self.stack.push(1) self.assertEqual(self.stack.peek(), 1) self.assertEqual(len(self.stack), 1) self.assertEqual(1 in self.stack, True) self.assertEqual(2 in self.stack, False) def test_push_twice(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.peek(), 2) self.assertEqual(len(self.stack), 2) self.assertEqual(1 in self.stack, True) def test_push_pop(self): self.stack.push(1) result = self.stack.pop() self.assertEqual(self.stack.peek(), None) self.assertEqual(len(self.stack), 0) self.assertEqual(result, 1) self.assertEqual(1 in self.stack, False) def test_push_twice_pop(self): self.stack.push(1) self.stack.push(2) result = self.stack.pop() self.assertEqual(self.stack.peek(), 1) self.assertEqual(len(self.stack), 1) self.assertEqual(result, 2) self.assertEqual(1 in self.stack, True) self.assertEqual(2 in self.stack, False) def test_push_thrice_clear(self): self.stack.push(1) self.stack.push(2) self.stack.push(3) self.stack.clear() self.assertEqual(self.stack.peek(), None) self.assertEqual(len(self.stack), 0) self.assertEqual(1 in self.stack, False) def test_pop(self): with self.assertRaises(IndexError): self.stack.pop() def test_loop_through_stack(self): self.stack = Stack([1,2,3]) num_list = [i for i in self.stack] self.assertEqual(num_list, [3,2,1]) def test_arithmetic_add(self): self.stack += 1 self.assertEqual(self.stack.peek(), 1) self.assertEqual(len(self.stack), 1) self.assertEqual(1 in self.stack, True) self.assertEqual(2 in self.stack, False)