繁体   English   中英

CodeEval艰苦的挑战6-最长的共同继承-python

[英]CodeEval Hard Challenge 6 - LONGEST COMMON SUBSEQUENCE - python

我正在尝试解决Python中最长的公共子序列。 我已经完成了它,并且工作正常,尽管我已经提交了它,并且说它已经部分完成了50%。 我不确定我在这里缺少什么,感谢您的帮助。

挑战说明:

给出两个序列。 编写程序以确定两个字符串之间的最长公共子序列(每个字符串的最大长度可以为50个字符)。 注意:此子序列不必是连续的。 输入文件可能包含空行,这些空行需要忽略。

输入样本:

第一个参数是文件名的路径,该文件名每行包含两个字符串,以分号分隔。 您可以假设每个测试用例只有一个唯一的子序列。 例如:

XMJYAUZ;MZJAWXU

输出样本:

最长的公共子序列。 确保在您打印的每一行上都没有尾随的空格。 例如:

MJAU

我的代码是

# LONGEST COMMON SUBSEQUENCE
import argparse


def get_longest_common_subsequence(strings):
    # here we will store the subsequence list
    subsequences_list = list()

    # split the strings in 2 different variables and limit them to 50 characters
    first = strings[0]
    second = strings[1]

    startpos = 0
    # we need to start from each index in the first string so we can find the longest subsequence
    # therefore we do a loop with the length of the first string, incrementing the start every time
    for start in range(len(first)):
        # here we will store the current subsequence
        subsequence = ''

        # store the index of the found character
        idx = -1

        # loop through all the characters in the first string, starting at the 'start' position
        for i in first[start:50]:
            # search for the current character in the second string
            pos = second[0:50].find(i)

            # if the character was found and is in the correct sequence add it to the subsequence and update the index
            if pos > idx:
                subsequence += i
                idx = pos

        # if we have a subsequence, add it to the subsequences list
        if len(subsequence) > 0:
            subsequences_list.append(subsequence)

        # increment the start
        startpos += 1

    # sort the list of subsequences with the longest at the top
    subsequences_list.sort(key=len, reverse=True)

    # return the longest subsequence
    return subsequences_list[0]


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('filename')
    args = parser.parse_args()

    # read file as the first argument
    with open(args.filename) as f:
        # loop through each line
        for line in f:
            # if the line is empty it means it's not valid. otherwise print the common subsequence
            if line.strip() not in ['\n', '\r\n', '']:
                strings = line.replace('\n', '').split(';')
                if len(strings[0]) > 50 or len(strings[1]) > 50:
                    break
                print get_longest_common_subsequence(strings)

    return 0


if __name__ == '__main__':
    main()

以下解决方案从分号分隔的字符串对中打印无序/未排序的最长公共子序列/子字符串。 如果该对中的字符串长于50个字符,则将跳过该对(如果需要,将其修剪为50个长度并不难)。

注意:如果需要排序/排序,可以实现(按字母顺序排序,或按第一个字符串的顺序排序,或按第二个字符串的顺序排序。

with open('filename.txt') as f:
    for line in f:
        line = line.strip()
        if line and ';' in line and len(line) <= 101:
            a, b = line.split(';')
            a = set(a.strip())
            b = set(b.strip())
            common = a & b  # intersection
            if common:
                print ''.join(common)

还要注意:如果子字符串具有内部公共空格(即ABC DE; ZM YCA ),则它将成为输出的一部分,因为不会被剥离。 如果不希望这样,则可以将行a = set(a.strip())替换为a = {char for char in a if char.strip()} b

暂无
暂无

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

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