online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
from random import * from stack import * from queue import * from tests import * from task3 import * from task4 import * from task5 import * ''' This module will give you an introduction to stacks and queues as well as give you some more practice with class objects. ''' def main(): print("Begin main") #before moving to task 2 make sure to make a stack and test it here #for syntax remember that it is a class object #before moving to task 3 make sure to make a queue and test it here #for syntax remember that it is a class object #task1_check() #task2_check() #task3_check() #task4_check() #TASK 5 #simple_game() main()
class Stack: def __init__(self, size): self.data = [0] * size self.end = 0 self.size = size def push(self, item): if self.end >= len(self.data): print("Full!") return self.data[self.end] = item self.end += 1 def pop(self): if self.end <= 0: print("Empty!") return self.end -= 1 return self.data[self.end] def dump(self): for i in range(self.end - 1, -1, -1): print(self.data[i])
class Queue: def __init__(self, size): self.data = ["<EMPTY>"] * size self.end = 0 self.start = 0 self.size = size def enqueue(self, item): if self.end >= len(self.data): print("Full!") return self.data[self.end] = item self.end += 1 def dequeue(self): if self.start == self.end: print("Empty!") return item = self.data[0] self.end -= 1 for i in range(0, self.end): self.data[i] = self.data[i + 1] return item def dump(self): print(self.data, "Start:", self.start, "End:", self.end)
def task1_check(): '''TASK 1 : count, peek, and clear tests for stack''' s_stack = Stack(5) print("Expected 0 : ", s_stack.count) s_stack.push(1) print("Expected 1 : ", s_stack.count) s_stack.push(2) s_stack.push(3) print("Expected 3 : ", s_stack.count) print("Expected 3 : ", s_stack.peek()) s_stack.pop() s_stack.pop() print("Expected 1 : ", s_stack.count) s_stack.clear() print("After clear : ", s_stack.count) def task2_check(): '''TASK 2 : count, peek, and clear tests for queue''' s_queue = Queue(5) print("Expected 0 : ", s_queue.count) s_queue.enqueue(1) print("Expected 1 : ", s_queue.count) s_queue.enqueue(2) s_queue.enqueue(3) print("Expected 3 : ", s_queue.count) print("Expected 3 : ", s_queue.peek()) s_queue.dequeue() s_queue.dequeue() print("Expected 1 : ", s_queue.count) s_queue.clear() print("After clear : ", s_queue.count) def task3_check(): '''TASK 3 : palindrome_check tests ''' print("Expected True : ", palindrome_check("tacocat")) print("Expected True : ", palindrome_check("taco cat")) print("Expected True : ", palindrome_check("Taco cat")) print("Expected True : ", palindrome_check("bob")) print("Expected True : ", palindrome_check("dogod")) print("Expected True : ", palindrome_check("Doc note I dissent A fast never prevents a fatness I diet on cod")) print("Expected False : ", palindrome_check("alpha")) print("Expected False : ", palindrome_check("tagurit")) def task4_check(): '''TASK 4 : paren_check tests''' print("Expected True : ", paren_check("4+3")) print("Expected True : ", paren_check("(4+3)")) print("Expected False : ", paren_check("(4+3")) print("Expected False : ", paren_check("4+3)")) print("Expected True : ", paren_check("()")) print("Expected False :", paren_check("())()"))
''' TASK 1: Introduction to Stacks A stack is a structure with a FILO access structure. It's useful for all kinds of applications where you want to force FILO behavior. Can you name a few? stack.py is one of the implementations of a stack presented in the module. First, review the code and make sure you understand what it is doing. I've removed the comments. I don't want to make it too easy. Answer the following questions: What is the purpose of each method? If you pushed 123, what would be the first thing to pop? What is the stack using as a "backend?" What happens if you try to add something after the stack is full? What are some real world uses for a stack? Add the following functionality then un-comment the test function for task1 in main below to see if you got it correct: A count variable to check the size of the stack. (Tip: You'll need to alter three methods) A peek function. (Hint: put back what you took) A clear function.(Tip: you'll need to "remember" the sent size) Use main file to test the functionality of your stack. '''
''' TASK 2: Introduction to queues A queue is a data structure that forces FIFO behavior. This is a very common structure we use when we want to enforce this type of behavior. Can you think of some examples? This is the implementation of a queue presented in the module. I've removed the comments. I don't want to make it too easy. First, review the code and make sure you understand what is going on. Answer the following questions: What is the purpose of each method? If you enqueued 123, what would be the first thing to dequeue? What is the queue using as a "backend?" What are some real world uses for a queue? What are the key code differences between this and stacks? Add the following functionality then un-comment the tests below to see if you got it correct: A count variable to check the size of the queue. (Tip: You'll need to alter three methods) A peek function. A clear function.(Tip: you'll need to "remember" the sent size) Use the main file to test the functionality of your queue. '''
''' TASK 3 : making use of stacks and queues - palindromes Now I want you to actually use a stack and Queue for something. Check to see if a sent string is a palindrome. That is, does it read the same backwards and forwards. Make sure to ignore spaces and caps. Tip: Think about the order things are entered and removed from a stack and a queue. Tip: Consider how you'd know when you've reached the center. ''' def palindrome_check(sent_str): result = True count = 0
''' TASK 4 : making use of stacks and queues - ??? Check to see if an equation has correctly balanced parenthesis. That is, for every '(' there must be a ')' in the correct order. Return true if it is correct and false if it is not. Step 1 is deciding on what kind of a structure to use. ''' def paren_check(sent_str): return True
''' TASK 5 : You must defeat a revolving line of Zombies. Each zombie will attack you and then head to the back of the line to try again. Review the below code and add the required changes to make the game work. Each line I removed starts with a ###. Sometimes I left part of the line there as a hint ''' #Zombie object for use in "simple_game" class Zombie: def __init__(self): self.hitPoints = randint(5,15) self.strength = randint(1,5) def attack(self): return randint(1,self.strength) def take_damage(self,damage): self.hitPoints -= damage ''' def simple_game(): #create a line of size 10 for our zombies to wait in ###line_o_zombies = hitPoints = 20 #your hitpoints #load up a single zombie to get us started ###Zombie() #game loop while True: chance = randint(1,10) #have a 50% chance of another zombie being added ##if chance < ?: Zombie() #if the line fills up game over, you lose if line_o_zombies.count == 10: print("You are overrun with zombies") break #if there are no more zombies game over, you win if line_o_zombies.count == 0: print("Congratulations you won!!") break print("Current Zombie Count :", line_o_zombies.count) #pull the first zombie in line to fight ###zombie = line_o_zombies print("Current zombie hitPoints : ", zombie.hitPoints, " strength : ", zombie.strength) #adjust your hitPoints by the current zombie attack ###hitPoints #check to see if you survived. If you didn't, game over if hitPoints <= 0: print("You have perished") break selection = int(input("Enter: 1. to attack, 2. to heal :")) #if the user selects an attack have the current zombie take damage if selection == 1: damage = randint(1,10) print("You damage the zombie by : ", damage) ### #If the user wants to heal increase hit points 1 - 5 if selection == 2: hitPoints += randint(1,5) #if the zombie is still alive add it to the back of the line ### print("Your remaining hitPoints are : ", hitPoints) '''

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue