ردود الذكاء الاصطناعي
سؤالك:
إجابة الشبكة العصبية:
class StringStack:
def __init__(self, size):
self.stack = []
self.size = size
def push(self, string):
if len(self.stack) < self.size:
self.stack.append(string)
else:
print("Stack is full!")
def pop(self):
if self.stack:
return self.stack.pop()
else:
print("Stack is empty!")
def count(self):
return len(self.stack)
def is_empty(self):
return len(self.stack) == 0
def is_full(self):
return len(self.stack) == self.size
def clear(self):
self.stack = []
def peek(self):
if self.stack:
return self.stack[-1]
else:
print("Stack is empty!")
# Menu
stack_size = 5
stack = StringStack(stack_size)
while True:
print("\n1. Push a string")
print("2. Pop a string")
print("3. Count strings in stack")
print("4. Check if stack is empty")
print("5. Check if stack is full")
print("6. Clear stack")
print("7. Peek top string")
print("8. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
string = input("Enter a string to push: ")
stack.push(string)
elif choice == 2:
popped_string = stack.pop()
if popped_string:
print("Popped string:", popped_string)
elif choice == 3:
print("Number of strings in stack:", stack.count())
elif choice == 4:
if stack.is_empty():
print("Stack is empty")
else:
print("Stack is not empty")
elif choice == 5:
if stack.is_full():
print("Stack is full")
else:
print("Stack is not full")
elif choice == 6:
stack.clear()
print("Stack cleared")
elif choice == 7:
top_string = stack.peek()
if top_string:
print("Top string:", top_string)
elif choice == 8:
break
else:
print("Invalid choice. Please try again.")
يرجى ملاحظة: قد لا تكون الإجابات المقدمة بواسطة الذكاء الاصطناعي دقيقة دائمًا. لا تعتمد عليها في القضايا الحساسة مثل الطب، القانون، الأمور المالية أو القضايا الأمنية. من الأفضل دائمًا الرجوع إلى المتخصصين المؤهلين عند اتخاذ القرارات المهمة. إدارة الموقع غير مسؤولة عن المحتوى الذي يتم إنتاجه تلقائيًا.