Python“;至于;循环:打印“循环”的程序;2“;“的字眼;“头晕”;及;合子;
我对单词之间的相互关系以及算法中如何计算这个数字感到困惑。这里是完整的问题(我应该主要用于循环。我认为我应该避免内置函数。) " 对于此任务,我们将向您提供几个示例,说明此程序应如何运行。我们不会确切地告诉您程序代码应包含哪些内容:但您的程序应按如下方式工作: 用户输入两个字符串(在两个单独的行上)“眩晕”和“受精卵”。您的程序输出:2 用户输入两个字符串(在两个单独的行上)“essentialness”和“Essentials”。您的程序输出:3 用户输入两个字符串(分别在两行上)“鸭嘴兽”和“盐水鳄鱼”。您的程序输出:1 用户输入两个字符串(在两个单独的行上)“bar”和“battery”。您的程序输出:0 以上是一些示例:对于任何字符串输入,您的程序的行为都应该类似。下面是一些运行示例: 重新启动: 键入内容,然后按回车/回车键:definite 现在键入其他内容,然后再次按回车键:finite 计算的输出为6 重新启动: 键入内容,然后按回车/回车键:make 现在键入其他内容,然后再次按回车键:matt 计算的输出为0 重新启动: 键入内容,然后按回车/回车键:bassline 现在键入其他内容,然后再次按回车键:linebass 计算的输出为4 重新启动: 键入内容,然后按回车/回车键:criting 现在键入其他内容,然后再次按回车键:ingenious 计算输出为3 重新启动: 键入内容,然后按return/enter:banana 现在键入其他内容,然后再次按回车键:nanaimo 计算的输出为4 重新启动: 键入内容,然后按return/enter:多余 现在键入其他内容,然后再次按回车键:tossup 计算的输出为0 " 以下是我尝试过的(不完整+没有给出答案):Python“;至于;循环:打印“循环”的程序;2“;“的字眼;“头晕”;及;合子;,python,Python,我对单词之间的相互关系以及算法中如何计算这个数字感到困惑。这里是完整的问题(我应该主要用于循环。我认为我应该避免内置函数。) " 对于此任务,我们将向您提供几个示例,说明此程序应如何运行。我们不会确切地告诉您程序代码应包含哪些内容:但您的程序应按如下方式工作: 用户输入两个字符串(在两个单独的行上)“眩晕”和“受精卵”。您的程序输出:2 用户输入两个字符串(在两个单独的行上)“essentialness”和“Essentials”。您的程序输出:3 用户输入两个字符串(分别在两行上)“鸭嘴兽”和
打印(计数)我会这样做:
def count_back_front(
text1,
text2):
"""
Count the common chars backward for 1st and forward for 2nd strings.
Args:
text1 (str): The first input.
text2 (str): The second input.
Returns:
result (int): The number of common characters.
Examples:
>>> count_back_front('definite', 'finite')
6
>>> count_back_front('make', 'matt')
0
>>> count_back_front('bassline', 'linebass')
4
>>> count_back_front('cringing', 'ingenious')
3
>>> count_back_front('banana', 'nanaimo')
4
>>> count_back_front('superfluous', 'tossup')
0
"""
for i in range(min(len(text1), len(text2)), -1, -1):
# : uncomment the following line to see how it works
# print(i, text1[-i:], text2[:i])
if text1[-i:] == text2[:i]:
break
return i
# get the input data
text1 = input('Please enter first word: ')
text2 = input('Please enter second word: ')
# calculate the "riddle" number
count = count_back_front(text1, text2)
print('Common chars back/front: {}'.format(count))
其想法是:
- 在
步骤中,定义一个索引-1
,该索引从两个公共字符串的最大长度开始,以0结束(在i
范围中
停止
,因此必须使用
作为第二个参数)-1
- 检查第一个字符串的最后一个
字符是否与第二个字符串的第一个i
字符匹配:如果匹配,我们可以退出循环i
- 循环末尾的
值将告诉我们字符的共同点i
- 我使用了一个函数来更好地将实际执行计算的代码与输入/输出部分分开
- 我在中为您提供了一个像样的docstring(附在
”)文档(最好总是有一个文档)“”“
部分中的代码可以是示例
word1[:-1]
这样的行后面的原理是什么,因为它们没有效果。
def count_back_front(
text1,
text2):
"""
Count the common chars backward for 1st and forward for 2nd strings.
Args:
text1 (str): The first input.
text2 (str): The second input.
Returns:
result (int): The number of common characters.
Examples:
>>> count_back_front('definite', 'finite')
6
>>> count_back_front('make', 'matt')
0
>>> count_back_front('bassline', 'linebass')
4
>>> count_back_front('cringing', 'ingenious')
3
>>> count_back_front('banana', 'nanaimo')
4
>>> count_back_front('superfluous', 'tossup')
0
"""
for i in range(min(len(text1), len(text2)), -1, -1):
# : uncomment the following line to see how it works
# print(i, text1[-i:], text2[:i])
if text1[-i:] == text2[:i]:
break
return i
# get the input data
text1 = input('Please enter first word: ')
text2 = input('Please enter second word: ')
# calculate the "riddle" number
count = count_back_front(text1, text2)
print('Common chars back/front: {}'.format(count))