Python“;至于;循环:打印“循环”的程序;2“;“的字眼;“头晕”;及;合子;

Python“;至于;循环:打印“循环”的程序;2“;“的字眼;“头晕”;及;合子;,python,Python,我对单词之间的相互关系以及算法中如何计算这个数字感到困惑。这里是完整的问题(我应该主要用于循环。我认为我应该避免内置函数。) " 对于此任务,我们将向您提供几个示例,说明此程序应如何运行。我们不会确切地告诉您程序代码应包含哪些内容:但您的程序应按如下方式工作: 用户输入两个字符串(在两个单独的行上)“眩晕”和“受精卵”。您的程序输出:2 用户输入两个字符串(在两个单独的行上)“essentialness”和“Essentials”。您的程序输出:3 用户输入两个字符串(分别在两行上)“鸭嘴兽”和

我对单词之间的相互关系以及算法中如何计算这个数字感到困惑。这里是完整的问题(我应该主要用于循环。我认为我应该避免内置函数。)

" 对于此任务,我们将向您提供几个示例,说明此程序应如何运行。我们不会确切地告诉您程序代码应包含哪些内容:但您的程序应按如下方式工作:

用户输入两个字符串(在两个单独的行上)“眩晕”和“受精卵”。您的程序输出: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

"

以下是我尝试过的(不完整+没有给出答案):


打印(计数)

我会这样做:

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
    步骤中,定义一个索引
    i
    ,该索引从两个公共字符串的最大长度开始,以0结束(在
    范围中
    停止
    ,因此必须使用
    -1
    作为第二个参数)
  • 检查第一个字符串的最后一个
    i
    字符是否与第二个字符串的第一个
    i
    字符匹配:如果匹配,我们可以退出循环
  • 循环末尾的
    i
    值将告诉我们字符的共同点
注:

  • 我使用了一个函数来更好地将实际执行计算的代码与输入/输出部分分开
  • 我在中为您提供了一个像样的docstring(附在
    “”“
    ”)文档(最好总是有一个文档)
  • 示例
    部分中的代码可以是

(1)我必须承认,第一个“谜语”充其量也很烦人。(2)看起来函数需要输出最后一个字符串结尾和第二个字符串开头之间的共同字符数。(3)正如您所说,该程序将无法运行,但您能否提供您编写的代码背后的一些基本原理?首先,它要求用户输入两个不同的单词。然后,我放入一个变量,使第一个单词向后,以便可以从后到前进行分析,而另一个单词则从前到后进行分析(我猜这就是应该发生的事?)。然后它找到长度。我想让for循环在每个字符上比较两个单词,但后来我被搞糊涂了……基本的想法似乎是正确的,但例如,我不太明白为什么需要第一个单词向后,以及像
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))