Skip to content

Instantly share code, notes, and snippets.

@avivajpeyi
Created April 30, 2020 03:18
Show Gist options
  • Save avivajpeyi/91dda5afc37c02dbccb474cf80d0e0cc to your computer and use it in GitHub Desktop.
Save avivajpeyi/91dda5afc37c02dbccb474cf80d0e0cc to your computer and use it in GitHub Desktop.
from stack import Stack
def lecuture_test_1():
out_string = ""
int_stack = Stack()
int_stack.push(1)
int_stack.push(2)
out_string += f"{int_stack.pop()} , "
int_stack.push(3)
out_string += f"{int_stack.pop()} , "
out_string += f"{int_stack.pop()} "
print(out_string)
def lecuture_test_2():
int_stack = Stack()
int_stack.push(1)
int_stack.push(2)
print(int_stack.pop(), end=",")
print(int_stack.pop(), end=",")
print(int_stack.pop())
int_stack.push(3)
def main():
print("TEST 1")
lecuture_test_1()
# print("TEST 2")
# lecuture_test_2()
if __name__ == '__main__':
main()
import logging
logging.basicConfig(level=logging.DEBUG)
class Stack:
def __init__(self):
self.the_stack = []
self.count = 0
logging.debug(f"Initialised stack: {self}")
def __str__(self):
return f"Stack: {self.the_stack} (len:{self.count})"
def __len__(self):
return self.count
def is_empty(self):
return self.count == 0
def push(self, item):
self.the_stack.append(item)
self.count += 1
logging.debug(f"Pushed {item} to stack: {self}")
def pop(self):
assert not self.is_empty()
self.count -= 1
popped_element = self.the_stack.pop()
logging.debug(f"Popped top element {popped_element} off stack: {self}")
return popped_element
def peek(self):
assert not self.is_empty()
return self.the_stack[-1]
class Stack:
def __init__(self):
self.the_stack = []
self.count = 0
def __len__(self):
return self.count
def is_empty(self):
return self.count == 0
def push(self, item):
self.the_stack.append(item)
self.count += 1
def pop(self):
assert not self.is_empty()
self.count -= 1
popped_element = self.the_stack.pop()
return popped_element
def peek(self):
assert not self.is_empty()
return self.the_stack[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment