-1
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) 

我們在頂部給出了多個起點。我們給予汽車(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:] 

最後,我們給出利弊功能:

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 

我試圖修復這個代碼,但是,由於某些原因,我的代碼嘗試返回這個錯誤:

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)) 

你們能告訴我什麼我越來越錯了嗎?或者如何解決它?非常感謝!!!

+1

「不起作用」不是有用的錯誤描述,請參閱[問]。另外,你真的希望人們閱讀所有這些嗎?請參閱[mcve] ... –

+0

您是否閱讀過'cdr'功能的文檔?你爲什麼要傳遞它_two_參數? –

回答

0

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) 
+0

這是問題!謝謝!我總是遇到這樣的錯誤!如果我有一隻編碼鴨子,我會拋出它。 – aceofspades