简体   繁体   English

如何拆分字符串列表并使用递归返回元组?

[英]How do you split a string list and return tuples using recursion?

One function that I am having trouble with is split_list, where I have to split a list into three tuples, with each containing a strlist, through recursion.我遇到问题的一个 function 是 split_list,我必须通过递归将一个列表分成三个元组,每个元组都包含一个 strlist。 The first tuple with strings must start with a vowel, the second tuple with strings must start with a consonant, and the third tuple with strings should not start with an alpha character.第一个字符串元组必须以元音开头,第二个字符串元组必须以辅音开头,第三个字符串元组不应以字母字符开头。

class Node:
    def __init__(self, value, rest):
        self.value = value
        self.rest = rest
    def __eq__(self, other):
        return ((type(other) == Node)
          and self.value == other.value
          and self.rest == other.rest
        )
    def __repr__(self):
        return ("Node({!r}, {!r})".format(self.value, self.rest))

# a StrList is one of
# - None, or
# - Node(string, StrList)

def split_list(strlist):
    if strlist is None:
       return (None, None, None)
    res = split_list(strlist.rest)
    if strlist.value or res(strlist.value) == 'AEIOUaeiou':
       return (Node(strlist.value, res[0]), res[1], res[2])
    if strlist.value or res(strlist.value) != 'AEIOUaeiou':
       return (res[0], Node(strlist.value, res[1]), res[2])
    if strlist.value.isalpha() or res(strlist.value) == False:
       return (res[0], res[1], Node(strlist.value, res[2]))

This is what I have currently, but the main problem is that I am not getting the correct output when I run my unit tests.这是我目前所拥有的,但主要问题是我在运行单元测试时没有得到正确的 output。 The problem I have is AssertionError.我遇到的问题是 AssertionError。

Examples:例子:

strlist = Node("xyz", Node("Abc", Node("49ers", None)))
self.assertEqual(split_list(strlist),(Node('Abc', None), Node('xyz', None), Node('49ers', None)))

strlist = Node("Yellow", Node("abc", Node("$7.25", Node("lime", Node("42", Node("Ethan", None))))))
self.assertEqual(split_list(strlist),(Node('abc', Node("Ethan", None)), Node('Yellow', Node("lime", None)), Node('$7.25', Node("42", None))))

Output: Output:

AssertionError: Tuples differ: (Node('Yellow', Node('abc', Node('$7.25', Node('[50 chars]None) != (Node('abc', Node('Ethan', None)), Node('Yellow'[50 chars]ne)))

Can somebody figure out the problem?有人能找出问题所在吗? I would be thankful.我会很感激的。

You should use recursion only for the traversal part.您应该只对遍历部分使用递归。 The recursive function should not contain 3 specific and distinct process conditions.递归 function 不应包含 3 个特定且不同的过程条件。

What you could do is write generic a node filter function that will build a new node chain and use it to assemble your tuple.您可以做的是编写一个通用的节点过滤器 function,它将构建一个新的节点链并使用它来组装您的元组。 Without the specific conditions, this function would be very simple and easy to test:如果没有特定条件,这个 function 将非常简单易测:

def filter(self,condition): # on the Node class
    nextFound = self.rest.filter(condition) if self.rest else None
    return Node(self.value,nextFound) if condition(self) else nextFound 

Then you can build your tuple using this filter function with different parameters:然后,您可以使用具有不同参数的过滤器 function 构建您的元组:

def split_list(strlist):
    return ( strlist.filter(lambda n:n.value[:1].upper() in "AEIOU"), 
             strlist.filter(lambda n:n.value[:1].upper() in "BCDFGHJKLMNPQRSTVWXYZ"), 
             strlist.filter(lambda n:not n.value[:1].isalpha()) )

[EDIT] if you must do it all in a single function, you could combine the two (unwieldy as it may be): [编辑]如果您必须在一个 function 中完成所有操作,您可以将两者结合起来(可能很笨拙):

def split_list(strlist):
    nextFound = split_list(strlist.rest) if strlist.rest else (None,None,None)
    return ( Node(strlist.value,nextFound[0]) if strlist.value[:1].upper() in "AEIOU" else nextFound[0], 
             Node(strlist.value,nextFound[1]) if strlist.value[:1].upper() in "BCDFGHJKLMNPQRSTVXYZ" else nextFound[1], 
             Node(strlist.value,nextFound[2]) if not strlist.value[:1].isalpha() else nextFound[2] )

if this for a homework, I sincerely hope your teacher will promote separation of concerns and not require questionable coding practices如果这是一个家庭作业,我真诚地希望你的老师能够促进关注点分离并且不需要有问题的编码实践

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

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