Question
I need to write a program based on two finished programs (listm.py and setunion.py) to implement the intersection operation between two sets of list. The
I need to write a program based on two finished programs (listm.py and setunion.py) to implement the intersection operation between two sets of list. The program should not use the Python built-in list and set related functions and operations.
I'm sorry about the program format is wrong(no "TAB" space), here is the same question I posted 2 days ago, but the question title is incorrect, there have complete images of my 3 Python program. The link is:
https://www.chegg.com/homework-help/questions-and-answers/need-write-program-based-two-finished-programs-listmpy-setunionpy-implement-intersection-o-q44800953?trackid=uNY0ilRc
Thank you!
here is listm.py
class List: def __init__(self, extlist=None): if extlist is None: self.inlist = [] else: self.inlist = extlist def length(L): return len(L.inlist) def isempty(L): if len(L.inlist)==0: return True else: return False def get(L, i): if isempty(L): print("The get() is unsuccessful!") print("The list is empty!") return -1 elif (i<1) or (i>length(L)): print("The get() is unsuccessful!") print("The index given is out of range!") return -1 else: return(L.inlist[i-1]) def locatebyvalue(L, x): position = 1 for item in L.inlist: if x == item: return position else: position = position+1 return -1 def locatebyid(L, idd): position = 1 for item in L.inlist: if item.id == idd: return position else: position = position+1 return -1 def insert(L, i, x): if (i<1) or (i>length(L)+1): print("The insert() is unsuccessful!") print("The index given is out of range!") else: L.inlist.insert(i-1, x) def delete(L, i): if isempty(L): print("The delete() is unsuccessful!") print("The list is empty!") return elif (i<1) or (i>length(L)): print("The delete() is unsuccessful!") print("The index given is out of range!") return else: del L.inlist[i-1] def display(L): print(L.inlist) |
here is setunion.py
from listm import * def un(setA, setB): n = length(setA) for i in range(length(setB)): x = get(setB, i+1) k = locatebyvalue(setA, x) if (k==-1): insert(setA, n+1, x) n = n+1 setA = List([1, 2, 3, 4, 5]) setB = List([2, 4, 5, 10, 11]) display(setA) display(setB) un(setA, setB) print("The union result is as follows") display(setA) |
I'm trying to finish it by modify setunion.py and open a new file called intersection.py:
intersection.py
def intersection(seta,setb): setc=[] for num in seta: if num in setb: setc.append(num) return setc |
And I add:
from intersection import * #on the top of setunion.py
print("The intersection is:") #below the setunion.py print(intersection(setA,setB))
but finally I get the result is: TypeError: 'List' object is not iterable
What's wrong with me?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started