python马丁challenge18.Merging two strings into a third one

2 Merging two strings into a third one
Say that two strings s1 and s2 can be merged into a third string s3 if s3 is obtained from s1 by
inserting arbitrarily in s1 the characters in s2, respecting their order. For instance, the two strings
ab and cd can be merged into abcd, or cabd, or cdab, or acbd, or acdb, …, but not into adbc nor into
cbda. Write a program merging_strings.py that prompts the user for 3 strings and displays the
output as follows:
• If no string can be obtained from the other two by merging, then the program outputs that
there is no solution.
• Otherwise, the program outputs which of the strings can be obtained from the other two by
merging.
Here is a possible interaction:
python3 merging_strings.py

Please input the first string: ab
Please input the second string: cdab
Please input the third string: cd
The second string can be obtained by merging the other two.
$ python3 merging_strings.py

Please input the first string: abcd
Please input the second string: cd
Please input the third string: ab
The first string can be obtained by merging the other two.

$ python3 merging_strings.py
Please input the first string: ab
Please input the second string: cd
Please input the third string: adcb
No solution

$ python3 merging_strings.py
Please input the first string: aaaaa
Please input the second string: a
Please input the third string: aaaa
The first string can be obtained by merging the other two.

$ python3 merging_strings.py
Please input the first string: aaab
Please input the second string: abcab
Please input the third string: aaabcaabb
The third string can be obtained by merging the other two.

$ python3 merging_strings.py
Please input the first string: ??got
Please input the second string: ?it?go#t##
Please input the third string: it###
The second string can be obtained by merging the other two.

# Say that two strings s_1 and s_2 can be merged into a third
# string s_3 if s_3 is obtained from s_1 by inserting
# arbitrarily in s_1 the characters in s_2, respecting their
# order. For instance, the two strings ab and cd can be merged
# into abcd, or cabd, or cdab, or acbd, or acdb..., but not into
# adbc nor into cbda.
#
# Prompts the user for 3 strings and displays the output as follows:
# - If no string can be obtained from the other two by merging,
# then the program outputs that there is no solution.
# - Otherwise, the program outputs which of the strings can be obtained
# from the other two by merging.


next1=0
next2=0

def can_merge(string_1, string_2, string_3):
    #正向比或逆向比
    #逆向比:借用上一题的思路1.string_3的长度作为digits的长度len(string_3)
    #if string_1[-1]==string_3[-1]
    #can_merge(string_1[:-2],string_2,string_3[:-2])
    #if srting_2[-1]==string_3[-1]
    #can_merge(string_1,string_2[:-2],string_3[:-2])
    #if lenstring_1<2:
    #string_2[0]==string_3[0] not->false
    #if lenstring_2<2:
    if not string_1 and string_2==string_3:
        return True
    if not string_2 and string_1==string_3:
        return True
    # if not string_1 and not string_2:
    #     return False

    if string_1[-1]==string_3[-1] and can_merge(string_1[:-1],string_2,string_3[:-1]):  #因为是从后面开始切所以要注意是不取右边界值,本来以为会报index out of range的错但是试了一下边界时是会输出空字符串的          
        return True

    if string_2[-1]==string_3[-1] and can_merge(string_1,string_2[:-1],string_3[:-1]):
        return True
    return False 
        
    # Replace pass above with your code.
    # Think recursively


strings = []
ordinals = 'first', 'second', 'third'
for i in ordinals:
    strings.append(input(f'Please input the {i} string: '))

last = 0
if len(strings[1]) > len(strings[0]):
    last = 1
if len(strings[2]) > len(strings[last]):
    last = 2
if last == 0:
    first, second = 1, 2
elif last == 1:
    first, second = 0, 2
else:
    first, second = 0, 1
L=[(first,next1),(second,next2)]    
if (len(strings[last]) != len(strings[first]) + len(strings[second]) or
                                          not can_merge(strings[first], strings[second], strings[last])):
    print('No solution')
else:
    print(f'The {ordinals[last]} string can be obtained by merging the other two.')


你可能感兴趣的:(python马丁challenge18.Merging two strings into a third one)