Advertisement
ada1711

Python - Lekcja 15 (21.04)

Apr 21st, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. class Stack:
  2.     def __init__(self):
  3.         self.stack = []
  4.  
  5.     def push(self, item):
  6.         self.stack.append(item)
  7.  
  8.     def pop(self):
  9.         if not self.is_empty():
  10.             return self.stack.pop()
  11.  
  12.     def peek(self):
  13.         if not self.is_empty():
  14.             return self.stack[-1]
  15.  
  16.     def is_empty(self):
  17.         return len(self.stack) == 0
  18.  
  19.     def size(self):
  20.         return len(self.stack)
  21.    
  22.     # Zadanie dodatkowe
  23.     def is_equal(self, other):
  24.         if self.size() != other.size():
  25.             return False
  26.         for i in range(self.size()):
  27.             if self.stack[i] != other.stack[i]:
  28.                 return False
  29.         return True
  30.  
  31. stack = Stack()
  32.  
  33. stack.push(1)
  34. stack.push(2)
  35. stack.push(3)
  36.  
  37. print(stack.pop())  # 3
  38. print(stack.peek())  # 2
  39. print(stack.is_empty())  # False
  40. print(stack.size())  # 2
  41.  
  42.  
  43. # =======================================================================
  44. class Queue:
  45.     def __init__(self):
  46.         self.queue = []
  47.  
  48.     def is_empty(self):
  49.         return len(self.queue) == 0
  50.  
  51.     def enqueue(self, item):
  52.         self.queue.append(item)
  53.  
  54.     def dequeue(self):
  55.         if not self.is_empty():
  56.             return self.queue.pop(0)
  57.  
  58.     def peek(self):
  59.         if not self.is_empty():
  60.             return self.queue[0]
  61.  
  62. queue = Queue()
  63.  
  64. queue.enqueue(1)
  65. queue.enqueue(2)
  66. queue.enqueue(3)
  67.  
  68. print(queue.dequeue())  # 1
  69. print(queue.dequeue())  # 2
  70. print(queue.is_empty())  # False
  71. print(queue.dequeue())  # 3
  72. print(queue.is_empty())  # True
  73.  
  74. # =======================================================================
  75. class BrowserHistory:
  76.     def __init__(self):
  77.         self.history = Stack()
  78.         self.current_page = None
  79.  
  80.     def go_to_page(self, url):
  81.         self.history.push(self.current_page)
  82.         self.current_page = url
  83.  
  84.     def go_back(self):
  85.         previous_page = self.history.pop()
  86.         if previous_page is not None:
  87.             self.current_page = previous_page
  88.  
  89.     def print_history(self):
  90.         print("Current page:", self.current_page)
  91.         print("History:")
  92.         for page in reversed(self.history.stack):
  93.             print(page)
  94.  
  95. # =======================================================================
  96. class Customer:
  97.     def __init__(self, name, order):
  98.         self.name = name
  99.         self.order = order
  100.  
  101. class CinemaQueue:
  102.     def __init__(self):
  103.         self.queue = Queue()
  104.  
  105.     def is_empty(self):
  106.         return self.queue.is_empty()
  107.  
  108.     def add_customer(self, customer):
  109.         self.queue.enqueue(customer)
  110.  
  111.     def remove_customer(self):
  112.         if not self.is_empty():
  113.             return self.queue.dequeue()
  114.  
  115.     def next_customer_order(self):
  116.         if not self.is_empty():
  117.             next_customer = self.queue.peek()
  118.             return next_customer.order
  119.  
  120. # =======================================================================
  121. def is_palindrome(string):
  122.     stack = Stack()
  123.     for char in string:
  124.         stack.push(char)
  125.     reversed_string = ""
  126.     while not stack.is_empty():
  127.         reversed_string += stack.pop()
  128.     return string == reversed_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement