Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack:
- def __init__(self):
- self.stack = []
- def push(self, item):
- self.stack.append(item)
- def pop(self):
- if not self.is_empty():
- return self.stack.pop()
- def peek(self):
- if not self.is_empty():
- return self.stack[-1]
- def is_empty(self):
- return len(self.stack) == 0
- def size(self):
- return len(self.stack)
- # Zadanie dodatkowe
- def is_equal(self, other):
- if self.size() != other.size():
- return False
- for i in range(self.size()):
- if self.stack[i] != other.stack[i]:
- return False
- return True
- stack = Stack()
- stack.push(1)
- stack.push(2)
- stack.push(3)
- print(stack.pop()) # 3
- print(stack.peek()) # 2
- print(stack.is_empty()) # False
- print(stack.size()) # 2
- # =======================================================================
- class Queue:
- def __init__(self):
- self.queue = []
- def is_empty(self):
- return len(self.queue) == 0
- def enqueue(self, item):
- self.queue.append(item)
- def dequeue(self):
- if not self.is_empty():
- return self.queue.pop(0)
- def peek(self):
- if not self.is_empty():
- return self.queue[0]
- queue = Queue()
- queue.enqueue(1)
- queue.enqueue(2)
- queue.enqueue(3)
- print(queue.dequeue()) # 1
- print(queue.dequeue()) # 2
- print(queue.is_empty()) # False
- print(queue.dequeue()) # 3
- print(queue.is_empty()) # True
- # =======================================================================
- class BrowserHistory:
- def __init__(self):
- self.history = Stack()
- self.current_page = None
- def go_to_page(self, url):
- self.history.push(self.current_page)
- self.current_page = url
- def go_back(self):
- previous_page = self.history.pop()
- if previous_page is not None:
- self.current_page = previous_page
- def print_history(self):
- print("Current page:", self.current_page)
- print("History:")
- for page in reversed(self.history.stack):
- print(page)
- # =======================================================================
- class Customer:
- def __init__(self, name, order):
- self.name = name
- self.order = order
- class CinemaQueue:
- def __init__(self):
- self.queue = Queue()
- def is_empty(self):
- return self.queue.is_empty()
- def add_customer(self, customer):
- self.queue.enqueue(customer)
- def remove_customer(self):
- if not self.is_empty():
- return self.queue.dequeue()
- def next_customer_order(self):
- if not self.is_empty():
- next_customer = self.queue.peek()
- return next_customer.order
- # =======================================================================
- def is_palindrome(string):
- stack = Stack()
- for char in string:
- stack.push(char)
- reversed_string = ""
- while not stack.is_empty():
- reversed_string += stack.pop()
- return string == reversed_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement