""" CS3B, stack implementation Based on Prof. Loceff's implementation, modified by ZB @ 2020 Zi-Bin Yang """ import numpy class MyStack: # Constants MAX_CAPACITY = 100000 DEFAULT_CAPACITY = 10 #...

Ödevinizi Kopyalayın ve Buraya Yapıştırın


""" CS3B, stack implementation Based on Prof. Loceff's implementation, modified by ZB @ 2020 Zi-Bin Yang """ import numpy class MyStack: # Constants MAX_CAPACITY = 100000 DEFAULT_CAPACITY = 10 # Initializer method def __init__(self, default_item, capacity=DEFAULT_CAPACITY): # If the capacity is bad, fail right away if not self.validate_capacity(capacity): raise ValueError("Capacity " + str(capacity) + " is invalid") self.capacity = capacity self.default_item = default_item # Make room in the stack and make sure it's empty to begin with self.clear() def clear(self): # Allocate storage the storage and initialize top of stack self.stack = [self.default_item for _ in range(self.capacity)] self.top_of_stack = 0 @classmethod def validate_capacity(cls, capacity): return 0 <= capacity=""><= cls.max_capacity def push(self, item_to_push): if self.is_full(): raise overflowerror("push failed - capacity reached") elif type(item_to_push) != type(self.default_item): raise typeerror("push failed - wrong type for item") self.stack[self.top_of_stack] = item_to_push self.top_of_stack += 1 def pop(self): if self.is_empty(): raise indexerror("pop failed - stack is empty") self.top_of_stack -= 1 return self.stack[self.top_of_stack] def is_empty(self): return self.top_of_stack == 0 def is_full(self): return self.top_of_stack == self.capacity def get_capacity(self): return self.capacity def mystack_test(): # instantiate two empty stacks, one of 50 ints, another of 15 strings s1 = mystack(-1, 50) s2 = mystack("undefined") # and one more with bad argument try: s3 = mystack(none, -100) print("failed test: expected __init()__ to reject negative capcity but it didn't") except exception as e: print("successful test: handled negative capacity: " + str(e)) # confirm the stack capacities print("------ stack sizes -------\n s1: {} s2: {}\n". format(s1.get_capacity(), s2.get_capacity())) # pop empty stack print("------ test stack ------\n") try: s1.pop() print("failed test: expected pop() to raise empty-stack exception but it didn't") except exception as e: print("successful test: handled popping empty s1: " + str(e)) # push some items s1.push(44) s1.push(123) s1.push(99) s1.push(10) s1.push(1000) # try to put a square peg into a round hole try: s1.push("should not be allowed into an int stack") print("failed test: expected push() to reject due to type incompatibility but it didn't") except exception as e: print("successful test: rejected due to type incompatibility: " + str(e)) try: s2.push(444) print("failed test: expected push() to reject due to type incompatibility but it didn't") except exception as e: print("successful test: rejected due to type incompatibility: " + str(e)) try: s1.push(44.4) print("failed test: expected push() to reject due to type incompatibility but it didn't") except exception as e: print("successful test: rejected due to type incompatibility: " + str(e)) # push to s2 s2.push("bank") s2.push("-34") s2.push("should be okay") s2.push("a penny earned") s2.push("item #9277") s2.push("where am i?") s2.push("4") s2.push("4") s2.push("4") s2.push("4") try: s2.push("this is when stack is full") print("failed test: expected push() to throw exception but it didn't") except exception as e: print("successful test: handled pushing when stack is full: " + str(e)) print("\n--------- first stack ---------\n") # pop and inspect the items for k in range(0, 10): try: print("[" + str(s1.pop()) + "]") except exception as e: print("successful test: handled popping empty stack s1: " + str(e)) print("\n--------- second stack ---------\n") for k in range(0, 10): print("[" + str(s2.pop()) + "]") if __name__ == "__main__": mystack_test() cls.max_capacity="" def="" push(self,="" item_to_push):="" if="" self.is_full():="" raise="" overflowerror("push="" failed="" -="" capacity="" reached")="" elif="" type(item_to_push)="" !="type(self.default_item):" raise="" typeerror("push="" failed="" -="" wrong="" type="" for="" item")="" self.stack[self.top_of_stack]="item_to_push" self.top_of_stack="" +="1" def="" pop(self):="" if="" self.is_empty():="" raise="" indexerror("pop="" failed="" -="" stack="" is="" empty")="" self.top_of_stack="" -="1" return="" self.stack[self.top_of_stack]="" def="" is_empty(self):="" return="" self.top_of_stack="=" 0="" def="" is_full(self):="" return="" self.top_of_stack="=" self.capacity="" def="" get_capacity(self):="" return="" self.capacity="" def="" mystack_test():="" #="" instantiate="" two="" empty="" stacks,="" one="" of="" 50="" ints,="" another="" of="" 15="" strings="" s1="MyStack(-1," 50)="" s2="MyStack("undefined")" #="" and="" one="" more="" with="" bad="" argument="" try:="" s3="MyStack(None," -100)="" print("failed="" test:="" expected="" __init()__="" to="" reject="" negative="" capcity="" but="" it="" didn't")="" except="" exception="" as="" e:="" print("successful="" test:="" handled="" negative="" capacity:="" "="" +="" str(e))="" #="" confirm="" the="" stack="" capacities="" print("------="" stack="" sizes="" -------\n="" s1:="" {}="" s2:="" {}\n".="" format(s1.get_capacity(),="" s2.get_capacity()))="" #="" pop="" empty="" stack="" print("------="" test="" stack="" ------\n")="" try:="" s1.pop()="" print("failed="" test:="" expected="" pop()="" to="" raise="" empty-stack="" exception="" but="" it="" didn't")="" except="" exception="" as="" e:="" print("successful="" test:="" handled="" popping="" empty="" s1:="" "="" +="" str(e))="" #="" push="" some="" items="" s1.push(44)="" s1.push(123)="" s1.push(99)="" s1.push(10)="" s1.push(1000)="" #="" try="" to="" put="" a="" square="" peg="" into="" a="" round="" hole="" try:="" s1.push("should="" not="" be="" allowed="" into="" an="" int="" stack")="" print("failed="" test:="" expected="" push()="" to="" reject="" due="" to="" type="" incompatibility="" but="" it="" didn't")="" except="" exception="" as="" e:="" print("successful="" test:="" rejected="" due="" to="" type="" incompatibility:="" "="" +="" str(e))="" try:="" s2.push(444)="" print("failed="" test:="" expected="" push()="" to="" reject="" due="" to="" type="" incompatibility="" but="" it="" didn't")="" except="" exception="" as="" e:="" print("successful="" test:="" rejected="" due="" to="" type="" incompatibility:="" "="" +="" str(e))="" try:="" s1.push(44.4)="" print("failed="" test:="" expected="" push()="" to="" reject="" due="" to="" type="" incompatibility="" but="" it="" didn't")="" except="" exception="" as="" e:="" print("successful="" test:="" rejected="" due="" to="" type="" incompatibility:="" "="" +="" str(e))="" #="" push="" to="" s2="" s2.push("bank")="" s2.push("-34")="" s2.push("should="" be="" okay")="" s2.push("a="" penny="" earned")="" s2.push("item="" #9277")="" s2.push("where="" am="" i?")="" s2.push("4")="" s2.push("4")="" s2.push("4")="" s2.push("4")="" try:="" s2.push("this="" is="" when="" stack="" is="" full")="" print("failed="" test:="" expected="" push()="" to="" throw="" exception="" but="" it="" didn't")="" except="" exception="" as="" e:="" print("successful="" test:="" handled="" pushing="" when="" stack="" is="" full:="" "="" +="" str(e))="" print("\n---------="" first="" stack="" ---------\n")="" #="" pop="" and="" inspect="" the="" items="" for="" k="" in="" range(0,="" 10):="" try:="" print("["="" +="" str(s1.pop())="" +="" "]")="" except="" exception="" as="" e:="" print("successful="" test:="" handled="" popping="" empty="" stack="" s1:="" "="" +="" str(e))="" print("\n---------="" second="" stack="" ---------\n")="" for="" k="" in="" range(0,="" 10):="" print("["="" +="" str(s2.pop())="" +="" "]")="" if="" __name__="=" "__main__":="">
Jan 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here