回文码
Palindrome Code
今天实习面试,他们让我谈谈如何使用python判断一个字符串是否为回文。我列出了我的基本结构和一些伪代码,一切都很顺利......现在我回家了,我想实际编写代码。
一切就绪,我对字符串中的每个字母进行比较,判断是否是回文。代码有效,我只有一个小问题。按照我的结构,它会为每个字母比较输出 'The string, ~whatever~, is NOT a Palindrome.' 直到最后一个,它将输出是否为回文的最终决定。
我明白为什么会这样,但我对如何解决它有点困惑。有人可以指导我如何修复下面代码中的 if 语句,以便它只输出是否是回文一次吗?
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] == sa[n/2 - x]:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] == sa[n - a]:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
如果我 运行 代码的一些输出示例是:
Enter your string here: abba
This string has an even number of letters.
The string, abba, is NOT a palindrome!
The string, abba, is a palindrome!
Enter your string here: racecar
This string has an odd number of letters.
The string, racecar, is NOT a palindrome!
The string, racecar, is NOT a palindrome!
The string, racecar, is a palindrome!
Enter your string here: travel
This string has an even number of letters.
The string, travel, is NOT a palindrome!
The string, travel, is NOT a palindrome!
The string, travel, is NOT a palindrome!
此外,如果您对如何改进代码有任何建议,请随时将它们发送给我。我一直远离其他例子,因为我希望这种尝试是有机的,而且是我自己做的。
尝试以下操作,您可能需要修复一些语法,因为我不是 Python 开发人员。 :)
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
bool isPalindrome =false
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] == sa[n/2 - x]:
isPalindrome = true
else:
//should be in a block
isPalindrome = false
break;
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] == sa[n - a]:
isPalindrome = true
else:
//should be in a block
isPalindrome = false
break;
if isPalindrome:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
在迭代过程中跟踪标志。在开始时将其分配为 True
,然后在测试无效字母时将其分配为 False
。
或者更好的是,也许这样的事情会稍微简化一下:
string = 'nurses run'.replace(' ','')
gnirts = string[::-1]
if all( [ a==b for a,b in zip(string,gnirts) ] ):
print("It's a palindrome.")
else:
print("It's not a palindrome.")
这是挽救代码的最直接方法:
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
palindrome = True
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] != sa[n - x - 1]:
palindrome = False
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] != sa[n - x - 1]:
palindrome = False
if palindrome:
print 'The string, ' + string + ', is a palindrome!'
else:
print 'The string, ' + string + ', is NOT a palindrome!'
首先,您不想每次检查字符对时都报告它是否是回文。假设它是一个带有布尔变量的回文,如果发现不是,则将其更改为 False。
接下来,您的循环和索引存在一些问题。在你的偶数情况下,你应该从字符串的末尾开始倒数,而不是中间。同样,在您的奇怪情况下,您应该相对于循环变量 x
而不是静态中点进行倒计时。在这两种情况下,您都必须将索引调整一个以避免在 x = 0
.
时出现错误
最后,虽然您采用的方法是手动执行此操作的好方法,但您可以使用语言内置的更简洁的方法。简单地将输入字符串与 reversed(string)
或 string[::-1]
进行比较将产生一个布尔值,其中包含是否为回文的答案。
今天实习面试,他们让我谈谈如何使用python判断一个字符串是否为回文。我列出了我的基本结构和一些伪代码,一切都很顺利......现在我回家了,我想实际编写代码。
一切就绪,我对字符串中的每个字母进行比较,判断是否是回文。代码有效,我只有一个小问题。按照我的结构,它会为每个字母比较输出 'The string, ~whatever~, is NOT a Palindrome.' 直到最后一个,它将输出是否为回文的最终决定。
我明白为什么会这样,但我对如何解决它有点困惑。有人可以指导我如何修复下面代码中的 if 语句,以便它只输出是否是回文一次吗?
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] == sa[n/2 - x]:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] == sa[n - a]:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
如果我 运行 代码的一些输出示例是:
Enter your string here: abba
This string has an even number of letters.
The string, abba, is NOT a palindrome!
The string, abba, is a palindrome!
Enter your string here: racecar
This string has an odd number of letters.
The string, racecar, is NOT a palindrome!
The string, racecar, is NOT a palindrome!
The string, racecar, is a palindrome!
Enter your string here: travel
This string has an even number of letters.
The string, travel, is NOT a palindrome!
The string, travel, is NOT a palindrome!
The string, travel, is NOT a palindrome!
此外,如果您对如何改进代码有任何建议,请随时将它们发送给我。我一直远离其他例子,因为我希望这种尝试是有机的,而且是我自己做的。
尝试以下操作,您可能需要修复一些语法,因为我不是 Python 开发人员。 :)
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
bool isPalindrome =false
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] == sa[n/2 - x]:
isPalindrome = true
else:
//should be in a block
isPalindrome = false
break;
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] == sa[n - a]:
isPalindrome = true
else:
//should be in a block
isPalindrome = false
break;
if isPalindrome:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
在迭代过程中跟踪标志。在开始时将其分配为 True
,然后在测试无效字母时将其分配为 False
。
或者更好的是,也许这样的事情会稍微简化一下:
string = 'nurses run'.replace(' ','')
gnirts = string[::-1]
if all( [ a==b for a,b in zip(string,gnirts) ] ):
print("It's a palindrome.")
else:
print("It's not a palindrome.")
这是挽救代码的最直接方法:
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
palindrome = True
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] != sa[n - x - 1]:
palindrome = False
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] != sa[n - x - 1]:
palindrome = False
if palindrome:
print 'The string, ' + string + ', is a palindrome!'
else:
print 'The string, ' + string + ', is NOT a palindrome!'
首先,您不想每次检查字符对时都报告它是否是回文。假设它是一个带有布尔变量的回文,如果发现不是,则将其更改为 False。
接下来,您的循环和索引存在一些问题。在你的偶数情况下,你应该从字符串的末尾开始倒数,而不是中间。同样,在您的奇怪情况下,您应该相对于循环变量 x
而不是静态中点进行倒计时。在这两种情况下,您都必须将索引调整一个以避免在 x = 0
.
最后,虽然您采用的方法是手动执行此操作的好方法,但您可以使用语言内置的更简洁的方法。简单地将输入字符串与 reversed(string)
或 string[::-1]
进行比较将产生一个布尔值,其中包含是否为回文的答案。