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

This is the function that we need implemented. The seq and item come from these test functions right here.

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)

We are given multiple starting points at the top. We are given the car(lst) function:

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]

We are also given the cdr(lst) function:

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:]

And finally, we are given the cons function:

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

I have attempted to fix implement the code, however, for some reason, my code I attempt returns this error:

 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

This is my Code:

    if seq == ():
        return False
    elif item == car(seq):
        return True
    return search_sequence(cdr(seq, item))

Can you guys tell me what I'm getting wrong? Or how to fix it? Thank you so much!!!

Markus Meskanen
  • 15,203
  • 11
  • 57
  • 106

1 Answers1

0

Your cdr() only takes one argument, the list:

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

But you're passing it two arguments:

return search_sequence(cdr(seq, item))

You probably meant to pass the item as an argument to the search_sequence() instead of the cdr():

return search_sequence(cdr(seq), item)
Markus Meskanen
  • 15,203
  • 11
  • 57
  • 106
  • that was the issue! thank you! I always run into errors like that! If I had a coding duck, I would have thrown it. – aceofspades Apr 18 '17 at 09:44