Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! This must be done in Python. All coding files have been provided, I do not have anymore files to give or code to give.

Hello! This must be done in Python. All coding files have been provided, I do not have anymore files to give or code to give. The insert, equals, and removeALL functions must also be completed for this program to work. Thank you for your help!

image text in transcribed

image text in transcribed

image text in transcribed

"""

File: lisplist.py

Project 9.10

Adds lispMap and lispFilter functions for Lisp lists.

"""

class Node(object):

"""Represents a singly linked node."""

def __init__(self, data, next = None):

self.data = data

self.next = next

def __repr__(self):

"""Returns the string representation of a nonempty lisp list."""

def buildString(lyst):

if isEmpty(rest(lyst)):

return str(first(lyst))

else:

return str(first(lyst)) + " " + buildString(rest(lyst))

return "(" + buildString(self) + ")"

THE_EMPTY_LIST = None

# Basic functions

def isEmpty(lyst):

"""Returns True if lyst is empty or False otherwise."""

return lyst is THE_EMPTY_LIST

def first(lyst):

"""Returns the item at the head of lyst.

Precondition: lyst is not empty."""

return lyst.data

def rest(lyst):

"""Returns a list of items in lyst, after the first one.

Precondition: lyst is not empty."""

return lyst.next

def cons(item, lyst):

"""Adds item to the head of lyst and

returns the resulting list."""

return Node(item, lyst)

# Auxiliary functions

def contains(item, lyst):

"""Returns True if item is in lyst or

False otherwise."""

if isEmpty(lyst):

return False

elif item == first(lyst):

return True

else:

return contains(item, rest(lyst))

def get(index, lyst):

"""Returns the item at position index in lyst.

Precondition: 0

if index == 0:

return first(lyst)

else:

return get(index - 1, rest(lyst))

def length(lyst):

"""Returns the number of items in lyst."""

if isEmpty(lyst): return 0

else: return 1 + length(rest(lyst))

def buildRange(lower, upper):

"""Returns a list containing the numbers from

lower through upper.

Precondition: lower

if lower == upper:

return cons(lower, THE_EMPTY_LIST)

else:

return cons(lower, buildRange(lower + 1, upper))

def remove(index, lyst):

"""Returns a list with the item at index removed.

Precondition: 0

if index == 0:

return rest(lyst)

else:

return cons(first(lyst),

remove(index - 1, rest(lyst)))

def insert(index, item, lyst):

"""Returns a list with the item inserted at index.

Precondition: 0

def equals(lyst1, lyst2):

"""Returns True if list1 equals lyst2."""

def removeAll(item, lyst):

"""Returns a list with the item at index removed.

Precondition: 0

def main():

"""Create a list with 9..0 and print it."""

lyst= THE_EMPTY_LIST

for i in range(10):

lyst = cons(i, lyst)

print("List =", lyst)

print("Length =", length(lyst))

if __name__ == "__main__":

main()

Define the recursive functions lispMap() and lispFilter() for LISP constructed lists. Their behavior is similar to that of the Python functions map() and filter(), but the LISP functions return a list of the results. In the Node class of the lisplist.py file, complete the following: 1. Complete the implementation of the listMap() function. o Returns the results of applying function to each item in lyst. 2. Complete the implementation of the lispFilter() function. o Returns the items that pass the Boolean test. To test your program run the main() method in the lisplist.py file. Here is a sample interaction with this program: >>> lyst buildRange(1, 4) >>> lyst (1 2 3 4) >>> lispMap(lambda x: x ** 2, lyst) (1 4 9 16) >>> lispFilter(lambda x: x % 2 == 0, lyst) (24) >>> lispFilter(lambda x: x % 2 == 0, lispMap(lambda x: * ** 2, lyst)) (4 16)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions