Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
Define a recursive function removeAll that expects an item and a Lisp-like list as arguments. This function returns a Lisp-like list with all of the instances of the item removed.
In the lisplist.py file, make sure to include the insert() and equal() methods from Programming exercise 9.7 and Programming exercise 9.8.
Hint: Keep on removing the item if it equals the list’s first item.
Here is an example of its use:
>>> lyst = cons(2, cons(2, (cons 3, THE_EMPTY_LIST))) >>> lyst (2 2 3) >>> removeAll(2, lyst) (3) >>> removeAll(3, lyst) (2 2)
File: lisplist.py
Project 9.9
Adds a removeAll function for Lisp lists.
Add the insert() and equal() methods from Programming Exercise 9.7 and 9.8
"""
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 <= index < length(lyst)"""
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 <= upper"""
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 <= index < length(lyst)"""
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 <= index < length(lyst)"""
#Add from exercise 7
def equals(lyst1, lyst2):
"""Returns True if list1 equals lyst2."""
#Add from exercise 8
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()
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- The for construction is a loops that iteratively processes a given list. Consequently, it works so long even though there are objects to process. What about this claim: true or false?arrow_forwardComplete the function that computes the length of a path that starts with the given Point. The Point structure = point on the plane that is a part of a path. The structure includes a pointer to the next point on the path, or nullptr if it's the last point. I'm not sure how to fill in the '?'; especially the while loop I'm not sure how to implement the condition for that.arrow_forwardPython please... Implement a function printIndex() that takes a list as a parameter, prompts the user to enter a whole number n, and prints the element in position Index[n]. If the list is empty ([]) or n is not a valid index into the list, the function will not print anything. Be careful to do the correct thing with negative indices. You should assume that the user will enter a whole number when prompted, and the function will crash if the user does not enter an integer. The function should not change the list passed as a parameter. Hint: Just because this involves a list does not mean that you need a loop to solve the problem. Think carefully about the right construct to use here. The following shows the function template, and several examples runs of the function (you must show all the examples with these values in your submission): Template def printIndex(lst): replace with your docstring newList = lst # ________________________ elementNum =…arrow_forward
- 1.Given the argument passed to the function is a list containing integer values, what does the function return def mystery1(someListOfValues: list):total = 0for value in someListOfValues:total += valuereturn total/len(someListOfValues) 2. The function below is to find the largest value in a list passed by parameter. Does it work as intened (yes or no)? If not give an example of an argument it would not work for. def maxValue(someListOfValues:list):"""returns the largest value the list passed by parameter"""value = 0for num in someListOfValues:if num > value:value = numreturn value 3.Write a function called loadNames() that will open a file called names.txt and build a list of its contents and return it. Function Call: names = loadNames()print(names) Sample ouput: ['Nolan Rafaj\n', 'Brandon Brzuszkiewicz\n', 'Amanda Vozari\n', 'Chase Harper\n', 'Bryce Conner']arrow_forwardIn this problem, you have available to you a working version of the function round_up described in part (a). It is located in a module named foo. Using the given round_up function, write a function round_up_all that takes a list of integers and MODIFIES the list so that each number is replaced by the result of calling round_up_ on that number. For example, if you run the code, 1st = [43, 137, 99, 501, 300, 275] round_up_all(1st) print(1st) the list would contain the values [100, 200, 100, 600, 300, 300] Do not re-implement the round_up function, just import the food module and use it. Here is the code for the round_up function that can be imported using foo: def round_up(num): if num % 100 == 0 : #if number is complete divisible return num #return orginal num else: return num + (100-(num % 100)) #else add 100-remainder to num, if __name__ == '__main__': print(round_up(234)) print(round_up(465)) print(round_up(400)) print(round_up(89))arrow_forwardAdd the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function. I have 5 tabs: I have tried every solution I can think of with no luck. These are the guides: arrayListType.h arrayListTypeImp.cpp: main.cpp unorderedArraryListType.h unorderedArrayListTypeImp.cpp I am needing these in order to pass the assignment in Cengage Mindtap, please help with codes for each one if possible.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education