我如何完成这个递归语句?

时间:2017-04-18 08:51:30

标签: python function recursion cardinality recursive-backtracking

array([[[ 0.62507486,  0.3246161 ,  0.43934602],
    [ 0.14476213,  0.76139957,  0.92813474],
    [ 0.26556504,  0.02475475,  0.90740073],
    [ 0.08017973,  0.97526789,  0.2213122 ]],

   [[ 0.70042586,  0.8122381 ,  0.79289031],
    [ 0.0369414 ,  0.10780825,  0.77501732],
    [ 0.10386232,  0.86237574,  0.5829311 ],
    [ 0.1888348 ,  0.85105735,  0.31599012]],

   [[ 0.26350111,  0.8787083 ,  0.12869285],
    [ 0.25927794,  0.25701383,  0.81212741],
    [ 0.06661031,  0.53449911,  0.50212061],
    [ 0.40009728,  0.78002244,  0.81524432]],

   [[ 0.49921468,  0.82028496,  0.51261139],
    [ 0.62790054,  0.64566481,  0.02624587],
    [ 0.39364958,  0.99537313,  0.33225098],
    [ 0.88214922,  0.20252077,  0.78350848]],

   [[ 0.29032609,  0.95975012,  0.06733917],
    [ 0.24497923,  0.51818371,  0.93520784],
    [ 0.80267638,  0.88271469,  0.30779642],
    [ 0.57030594,  0.34175804,  0.52563131]],

   [[ 0.61039209,  0.57186425,  0.76554799],
    [ 0.55681604,  0.33107477,  0.05680386],
    [ 0.15465826,  0.13452645,  0.09498007],
    [ 0.29682869,  0.93196124,  0.94435322]],

   [[ 0.23904459,  0.94893754,  0.97033942],
    [ 0.89159942,  0.85306913,  0.02144577],
    [ 0.57696968,  0.82578647,  0.33358794],
    [ 0.81979036,  0.73351973,  0.027876  ]],

   [[ 0.6568135 ,  0.25458351,  0.10369358],
    [ 0.06151289,  0.00939822,  0.00798484],
    [ 0.92518032,  0.19057493,  0.84838325],
    [ 0.78189474,  0.15273546,  0.34607282]],

   [[ 0.46961641,  0.19778872,  0.1498462 ],
    [ 0.55704814,  0.96889585,  0.08894933],
    [ 0.48003736,  0.59383452,  0.42212519],
    [ 0.78752649,  0.07204869,  0.4215464 ]],

   [[ 0.6454156 ,  0.84189773,  0.10041234],
    [ 0.89345407,  0.60821944,  0.56667495],
    [ 0.62806529,  0.67642623,  0.4951494 ],
    [ 0.85371262,  0.13159418,  0.3402876 ]],

   [[ 0.39828625,  0.50659049,  0.34835485],
    [ 0.06839356,  0.74652916,  0.5722388 ],
    [ 0.20762053,  0.0692997 ,  0.02790474],
    [ 0.84786427,  0.98461425,  0.19105092]],

   [[ 0.36976317,  0.44268745,  0.23061621],
    [ 0.47827819,  0.43044546,  0.90150601],
    [ 0.2307732 ,  0.61590552,  0.82066673],
    [ 0.49611789,  0.4480612 ,  0.46685895]],

   [[ 0.40907925,  0.15996945,  0.05480348],
    [ 0.70230347,  0.00926704,  0.97775948],
    [ 0.19834276,  0.20127937,  0.44351548],
    [ 0.48512974,  0.07319999,  0.5580616 ]],

   [[ 0.35749629,  0.88443983,  0.55465496],
    [ 0.61600298,  0.08260803,  0.4010818 ],
    [ 0.40910226,  0.31984288,  0.50188118],
    [ 0.34836289,  0.14394118,  0.06841569]]], dtype=float32)

这是我们需要实现的功能。 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
    """

我们在顶部有多个起点。我们获得了汽车(lst)功能:

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)

我们还获得了cdr(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]

最后,我们得到了cons函数:

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

你们能告诉我自己错了什么吗?或者如何解决?非常感谢你!!!

1 个答案:

答案 0 :(得分: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)
相关问题