简体   繁体   English

在不使用内置函数的情况下在python中递归合并两个字符串

[英]Merging two strings recursively in python without using built-in functions

Write a Python program, in a file called concat_strings.py , that includes the following functions:在名为concat_strings.py的文件中编写 Python 程序,其中包含以下函数:

orderedConcat , a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. orderedConcat ,一个递归函数,它接受两个按字母顺序排列的字符串并合并它们,让字母按字母顺序排列。 Note that the strings may be different lengths.请注意,字符串的长度可能不同。

A main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string.输入两个有序字符串并调用orderedConcat方法以返回结果合并的有序字符串的主要方法。 Your main method should print the two input strings and the result.您的主要方法应该打印两个输入字符串和结果。

Note: You may not use any of the built-in methods for string concatenation or ordering, but you may use the len() function and the slice operator (:).注意:您不能使用任何内置方法进行字符串连接或排序,但可以使用len()函数和切片运算符 (:)。 You may assume that the input strings are already ordered.您可以假设输入字符串已经排序。

Sample input/output:示例输入/输出:

Enter the first ordered string: DEab
Enter the second ordered string: BFxz
First string: DEab
Second string: BFxz
Result: BDEFabxz

NOTE: I am completely new to programming and have a seriously difficult time figuring this out.注意:我对编程完全陌生,并且很难弄清楚这一点。 Theoretically, the strings must be added together, but how do I sort them alphabetically?理论上,字符串必须加在一起,但我如何按字母顺序对它们进行排序? How do I deal with the uppercase and lowercase letters?如何处理大写和小写字母? Do I use a selection sort or merge sort?我使用选择排序还是归并排序? What should my base case and recursive case be?我的基本情况和递归情况应该是什么? Any help would be greatly appreciated.任何帮助将不胜感激。

What I have tried so far( It's not much, but I tried whatever I could):到目前为止我尝试过的东西(不多,但我尽我所能):

def orderedConcat(length):
    newString = string1 + string2
    length = len(newString)
    if length == 1 or length == "":
        return True
    elif length[0] <= length[1]:
        return orderedConcat(length[1:])
    else:
        return False

def main():
    string1 = "acdrt"
    string2 = "bdet"
    print("First string: ", string1)
    print("Second string: ", string2)
    print(newString)
main()

Let's go through this step by step.让我们一步一步来。 First off, our function takes in two strings, let's call them a and b :首先,我们的函数接受两个字符串,我们称它们为ab

def orderedConcat(a, b):

When writing recursive functions, we generally start off with the base case.在编写递归函数时,我们通常从基本情况开始。 In this case, there are two base cases:在这种情况下,有两种基本情况:

  1. a is empty, hence no merging is necessary and we can just return b (since b is sorted). a是空的,因此不需要合并,我们可以只返回b (因为b已排序)。
  2. b is empty, follow the same logic as in case 1 and return a . b为空,遵循与案例 1 相同的逻辑并返回a

In code:在代码中:

if len(a) == 0:
    return b
elif len(b) == 0:
    return a
else:
    # ...

Now, for the recursive case, we need to compare the first elements of both strings a[0] and b[0] .现在,对于递归情况,我们需要比较字符串a[0]b[0]的第一个元素。 If a[0] < b[0] (Python implements < on strings for us already! Note that 'A' < 'a' ), then a[0] needs to be prepended to the output string.如果a[0] < b[0] (Python 已经为我们在字符串上实现了< !注意'A' < 'a' ),那么a[0]需要被添加到输出字符串之前。 Otherwise, b[0] needs to be prepended.否则,需要预先添加b[0]

if a[0] < b[0]:
    return a[0] + ...
else:
    return b[0] + ...

We still need to fill in the ... .我们仍然需要填写... . When writing recursive functions, it's useful to pretend in your mind that you have already written a working function that you can make use of.在编写递归函数时,在脑海中假装已经编写了一个可以使用的工作函数是很有用的。 In this case, think "The result of orderedConcat is the smaller of a[0] and b[0] prepended to the orderedConcat of the rest of a / b ."在这种情况下,请考虑“ orderedConcat的结果是a[0]b[0]较小的a[0]orderedConcata / b其余部分的orderedConcat之前。

Hence:因此:

if a[0] < b[0]:
    return a[0] + orderedConcat(a[1:], b)
else:
    return b[0] + orderedConcat(a, b[1:])

I don't know how I'd deal with uppercase and lowercases recursively but this could be a quick work-around.我不知道如何递归处理大写和小写,但这可能是一个快速的解决方法。

def orderedConcat(string1, string2):
    if len(string1 + string2) == 0:
        return ''
    elif (len(string1) > 0 and len(string2) == 0) or (len(string1) == 0 and len(string2) > 0):
        return string1 + string2
    else:
        if string1[0] < string2[0]:
            return string1[0] + orderedConcat(string1[1:], string2)
        elif string2[0] < string1[0]:
            return string2[0] + orderedConcat(string1, string2[1:])
        else: # if they're equal
            return string1[0] + string2[0] + \
                orderedConcat(string1[1:], string2[1:])


def main():
    string1 = "acdrt"
    string2 = "bdet"
    print("First string: ", string1)
    print("Second string: ", string2)
    print("Result : ", end=' ')
    print(orderedConcat("acdrt", "bdet"))


main()

Looking at your function:看看你的功能:

def orderedConcat(length):
    newString = string1 + string2
    length = len(newString) #cut

First, you have to pass the strings into your function, not a length.首先,您必须将字符串传递到您的函数中,而不是长度。 So, start with the definition.所以,从定义开始。 And, to make this a bit simpler conceptually, let's declare a static variable, an empty string to hold the final concated, sorted result, using Python scope rules(global allows the string newString to persist and grow over multiple function calls, rather than being re-initialized on every call):而且,为了在概念上更简单一些,让我们声明一个静态变量,一个空字符串来保存最终的连接,排序结果,使用 Python 范围规则(全局允许字符串newString在多个函数调用中持久化和增长,而不是被在每次调用时重新初始化):

newString = ''          #outside of function
def oC(st1, st2):
    global newString    #scope modifier global

Second, the first step in any recursive problem is to identify the base case that will halt recursion so you don't recurse infinitely.其次,任何递归问题的第一步是确定将停止递归的基本情况,这样您就不会无限递归。 Since we're dealing with strings, and will be chopping them up, the base case is going to be len-dependent.由于我们正在处理字符串,并且会将它们切碎,因此基本情况将取决于 len。 Also the strings are individually ordered, so whatever is left over (if one is longer than the other) is not a problem, we'll just slap that remainder onto our concat:此外,字符串是单独排序的,所以剩下的任何东西(如果一个比另一个长)都不是问题,我们只需将剩余的部分放在我们的 concat 上:

    if len(st1) == 0:
        newString += st2
        return
    elif len(st2) == 0:
        newString += st1
        return

Having sorted out the base case, the next step in any recursive problem is to shorten the problem sequentially - break it up into smaller and smaller pieces such that the base case will eventually be reached.整理出基本情况后,任何递归问题的下一步都是按顺序缩短问题 - 将其分解成越来越小的部分,以便最终达到基本情况。 Here we can use the slice operator to sequentially compare string elements, and to shorten strings to feed back into the function:这里我们可以使用切片操作符顺序比较字符串元素,并缩短字符串以反馈给函数:

    elif st1[:1] <= st2[:1]:
        newString = newString + st1[:1]
        return oC(st1[1:], st2[:])  #shorten here
    else: 
        newString = newString + st2[:1]
        return oC(st1[:], st2[1:])

To call this, you have to say:要调用它,您必须说:

newString = ""
oC(st1, st2)

This is slightly inconvenient.这有点不方便。 See if you can figure out how to eliminate the need for the external concat string and the global in the function in this code.看看您是否可以弄清楚如何消除此代码中函数中的外部 concat 字符串和 global 的需要。

Your example isn't doing alphabetical ordering but rather ASCIIbetical ordering.您的示例不是按字母顺序排序,而是按ASCIIbetical排序。 Which is fine, as it simplifies our problem.这很好,因为它简化了我们的问题。 Rather than make the len() function an exception, let's not use it either:与其让len()函数成为例外,我们也不要使用它:

def orderedConcat(a, b):
    if a:
        if b:
            if a[:1] < b[:1]:
                return a[:1] + orderedConcat(a[1:], b)

            return b[:1] + orderedConcat(a, b[1:])
        return a
    return b

print(orderedConcat('DEab', 'BFxz'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM