我试图从以前制作的代码中导入回文功能。最初,回文密码工作正常,但是导入后,它没有显示正确的答案。
const doSomething = (props) => {
const {
standing_bid_amt = null,
min_increment = null,
starting_price = null
} = props.auction.auction || {};
console.log(min_increment);
};
doSomething({ auction: {auction: null }});
doSomething({ auction: {auction: {
standing_bid_amt: 50,
min_increment: 50,
starting_price: 50
}}});
这是我正在使用的功能:
import re
def check(string):
if (string==string[::-1]):
print ("{} is palindorme".format(string))
else:
print ("{} is not palindorme".format(string))
def palindrome(text):
c=re.sub('["?",",",".","/","@","#","%","&","*","!"," "]',"",text)
check(c)
输出应为:
from pal_func import palindrome
f=open("C:\\Users\\hp\\Desktop\\test file.txt",'r')
c=f.readline().lower()
print(c)
palindrome(c)
但它显示:
was it a cat i saw?
wasitacatisaw is palindrome
答案 0 :(得分:1)
使用以下命令分隔尾随换行符:
c=f.readline().lower().strip()
您的问题似乎不是源于代码,而是源于您读取文件的方式。
函数readline
从文件中读取一行,但不会删除尾随的换行符。提示是答案
wasitacatisaw
is not palindrom
有一个换行符,您不是自己写的
因此,函数检查将获得字符串wasitacatisaw<newline>
,它不是回文。
最后一个单词:下次,还请提供您要处理的文件(当然,不包含任何个人信息或密码),以便SO用户可以重现该错误:)< / p>