gdf
jh
k
k
o
python怎么用栈从英语文章中判断回文单词,有没有大ge知道
望采纳
下面是一个简单的 Python 代码示例,用于判断一个文章是否是回文:
def is_palindrome(text):
# 将文本中的所有单词压入栈中
stack = []
for word in text.split():
stack.append(word)
# 弹出栈中的单词并与原来的单词进行比较
while stack:
if stack.pop() != text.pop():
return False
return True
# 测试
text = "A man a plan a canal Panama"
print(is_palindrome(text)) # 输出 True