我如何完成这个递归语句?
How do I finish this recursive statement?
def search_sequence( seq, item ):
""" Search a sequence for the given item. PROVIDE AN IMPLEMENTATION (TASK
#2). This function should use **car** and **cdr**.
:param seq: the sequence to be searched.
:param item: the item to be searched
:type seq: tuple
:type item: str
:returns: True if the item is contained in the sequence, False
otherwise.
:rtype: bool
"""
这是我们需要实现的功能。 seq 和 item 来自这里的这些测试函数。
def test_search_sequence_0(self):
""" Search empty tuple """
sandwich = ()
self.assertEqual( search_sequence( sandwich, 'ham' ), False)
def test_search_sequence_size_1_1(self):
""" Search single-element tuple: successful search"""
sandwich = ('mustard',)
self.assertEqual( search_sequence( sandwich, 'mustard' ), True)
def test_search_sequence_size_1_2(self):
""" Search single-element tuple: unsuccessful search"""
sandwich = ('mustard',)
self.assertEqual( search_sequence( sandwich, 'ham' ), False)
def test_search_sequence_size_7_1(self):
""" Search 7-element tuple: successful search"""
sandwich = ("jelly","butter", "mustard", "bread", "pickles", "jam",
"cheese")
self.assertEqual( search_sequence( sandwich, 'pickles'), True)
def test_search_sequence_size_7_2(self):
""" Search 7-element tuple: unsuccessful search"""
sandwich = ("jelly","butter", "mustard", "bread", "pickles", "jam",
"cheese")
self.assertEqual( search_sequence( sandwich, 'pear'), False)
我们在顶部有多个起点。我们得到了 car(lst) 函数:
def car(lst):
""" The first of the 3 primitive functions: return the first element of a sequence.
.. note:: The Law of Car: The `car` function is only defined for non-empty lists.
:param lst: a non-empty sequence; passing an empty sequence will raise an exception.
:type lst: tuple
:returns: an object
:rtype: object
"""
if type(lst) is not tuple:
raise WrongTypeArgumentException("Argument is not a list.")
if len(lst)==0:
raise WrongTypeArgumentException("List has no element")
if len(lst)>=1:
return lst[0]
我们还得到了 cdr(lst) 函数:
def cdr( lst ):
""" The second of the 3 primitive functions: return a sequence, minus the first element.
.. note:: The Law of Cdr: The `cdr` function is only defined for non-empty lists; the `cdr` of any non-empty list is always another list.
:param lst: a non-empty sequence; passing an empty sequence will raise an exception.
:type lst: tuple
:returns: a tuple; if the sequence has only one element, return an empty sequence.
:rtype: tuple
"""
if type(lst) is not tuple:
raise WrongTypeArgumentException("Argument is not a list.")
if len(lst)==0:
raise WrongTypeArgumentException("Cannot cdr on an empty list.")
if len(lst)==1:
return ()
return lst[1:]
最后,我们得到了 cons 函数:
def cons( a, lst):
""" The third of the 3 primitive functions: return the sequence created by adding element `a` to the sequence `lst`.
.. note:: The Law of Cons: the primitive `cons` takes two arguments; the second argument to `cons` must be a list; the result is a list.
:param a: an object
:param lst: a tuple
:type a: object
:type lst: tuple
:returns: the tuple resulting from adding parameter `a` in front of sequence `lst`.
:rtype: tuple
"""
if type(lst) is not tuple:
raise WrongTypeArgumentException("Argument is not a list.")
return (a,) + lst
我试图修复代码的实现,但是,出于某种原因,我的代码尝试 returns 这个错误:
Traceback (most recent call last):
File "C:\Users\MacKenzy\Desktop\pylisp_skeleton.py", line 223, in test_search_sequence_size_1_2
self.assertEqual( search_sequence( sandwich, 'ham' ), False)
File "C:\Users\MacKenzy\Desktop\pylisp_skeleton.py", line 133, in search_sequence
return search_sequence(cdr(seq, item))
TypeError: cdr() takes 1 positional argument but 2 were given
这是我的代码:
if seq == ():
return False
elif item == car(seq):
return True
return search_sequence(cdr(seq, item))
你们能告诉我我做错了什么吗?或者如何解决?非常感谢!!!
你的cdr()
只接受一个参数,列表:
def cdr( lst ):
""" The second of the 3 primitive functions: return a sequence, minus the first element.
.. note:: The Law of Cdr: The `cdr` function is only defined for non-empty lists; the `cdr` of any non-empty list is always another list.
:param lst: a non-empty sequence; passing an empty sequence will raise an exception.
:type lst: tuple
:returns: a tuple; if the sequence has only one element, return an empty sequence.
:rtype: tuple
"""
但是您向它传递了两个参数:
return search_sequence(cdr(seq, item))
您可能打算将 item
作为参数传递给 search_sequence()
而不是 cdr()
:
return search_sequence(cdr(seq), item)
def search_sequence( seq, item ):
""" Search a sequence for the given item. PROVIDE AN IMPLEMENTATION (TASK
#2). This function should use **car** and **cdr**.
:param seq: the sequence to be searched.
:param item: the item to be searched
:type seq: tuple
:type item: str
:returns: True if the item is contained in the sequence, False
otherwise.
:rtype: bool
"""
这是我们需要实现的功能。 seq 和 item 来自这里的这些测试函数。
def test_search_sequence_0(self):
""" Search empty tuple """
sandwich = ()
self.assertEqual( search_sequence( sandwich, 'ham' ), False)
def test_search_sequence_size_1_1(self):
""" Search single-element tuple: successful search"""
sandwich = ('mustard',)
self.assertEqual( search_sequence( sandwich, 'mustard' ), True)
def test_search_sequence_size_1_2(self):
""" Search single-element tuple: unsuccessful search"""
sandwich = ('mustard',)
self.assertEqual( search_sequence( sandwich, 'ham' ), False)
def test_search_sequence_size_7_1(self):
""" Search 7-element tuple: successful search"""
sandwich = ("jelly","butter", "mustard", "bread", "pickles", "jam",
"cheese")
self.assertEqual( search_sequence( sandwich, 'pickles'), True)
def test_search_sequence_size_7_2(self):
""" Search 7-element tuple: unsuccessful search"""
sandwich = ("jelly","butter", "mustard", "bread", "pickles", "jam",
"cheese")
self.assertEqual( search_sequence( sandwich, 'pear'), False)
我们在顶部有多个起点。我们得到了 car(lst) 函数:
def car(lst):
""" The first of the 3 primitive functions: return the first element of a sequence.
.. note:: The Law of Car: The `car` function is only defined for non-empty lists.
:param lst: a non-empty sequence; passing an empty sequence will raise an exception.
:type lst: tuple
:returns: an object
:rtype: object
"""
if type(lst) is not tuple:
raise WrongTypeArgumentException("Argument is not a list.")
if len(lst)==0:
raise WrongTypeArgumentException("List has no element")
if len(lst)>=1:
return lst[0]
我们还得到了 cdr(lst) 函数:
def cdr( lst ):
""" The second of the 3 primitive functions: return a sequence, minus the first element.
.. note:: The Law of Cdr: The `cdr` function is only defined for non-empty lists; the `cdr` of any non-empty list is always another list.
:param lst: a non-empty sequence; passing an empty sequence will raise an exception.
:type lst: tuple
:returns: a tuple; if the sequence has only one element, return an empty sequence.
:rtype: tuple
"""
if type(lst) is not tuple:
raise WrongTypeArgumentException("Argument is not a list.")
if len(lst)==0:
raise WrongTypeArgumentException("Cannot cdr on an empty list.")
if len(lst)==1:
return ()
return lst[1:]
最后,我们得到了 cons 函数:
def cons( a, lst):
""" The third of the 3 primitive functions: return the sequence created by adding element `a` to the sequence `lst`.
.. note:: The Law of Cons: the primitive `cons` takes two arguments; the second argument to `cons` must be a list; the result is a list.
:param a: an object
:param lst: a tuple
:type a: object
:type lst: tuple
:returns: the tuple resulting from adding parameter `a` in front of sequence `lst`.
:rtype: tuple
"""
if type(lst) is not tuple:
raise WrongTypeArgumentException("Argument is not a list.")
return (a,) + lst
我试图修复代码的实现,但是,出于某种原因,我的代码尝试 returns 这个错误:
Traceback (most recent call last):
File "C:\Users\MacKenzy\Desktop\pylisp_skeleton.py", line 223, in test_search_sequence_size_1_2
self.assertEqual( search_sequence( sandwich, 'ham' ), False)
File "C:\Users\MacKenzy\Desktop\pylisp_skeleton.py", line 133, in search_sequence
return search_sequence(cdr(seq, item))
TypeError: cdr() takes 1 positional argument but 2 were given
这是我的代码:
if seq == ():
return False
elif item == car(seq):
return True
return search_sequence(cdr(seq, item))
你们能告诉我我做错了什么吗?或者如何解决?非常感谢!!!
你的cdr()
只接受一个参数,列表:
def cdr( lst ):
""" The second of the 3 primitive functions: return a sequence, minus the first element.
.. note:: The Law of Cdr: The `cdr` function is only defined for non-empty lists; the `cdr` of any non-empty list is always another list.
:param lst: a non-empty sequence; passing an empty sequence will raise an exception.
:type lst: tuple
:returns: a tuple; if the sequence has only one element, return an empty sequence.
:rtype: tuple
"""
但是您向它传递了两个参数:
return search_sequence(cdr(seq, item))
您可能打算将 item
作为参数传递给 search_sequence()
而不是 cdr()
:
return search_sequence(cdr(seq), item)