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 get(L, i): def locatebyvalue(L, x): def locatebyid(L, idd): def insert(L, i, x): def delete(L, i):
|
here is setunion.py
|
from listm import * def un(setA, setB):
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?
ANSWER:-
CODE:-
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 L.isempty():
print("The get() is unsuccessful!")
print("The list is empty!")
return -1
elif (i<1) or
(i>L.length()):
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 display(L):
print(L.inlist)
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>L.length()+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 L.isempty():
print("The delete() is unsuccessful!")
print("The list is empty!")
return
elif (i<1) or
(i>L.length()):
print("The delete() is unsuccessful!")
print("The index given is out of range!")
return
else:
del L.inlist[i-1]
setunion.py:-
from listm import *
def un(setA, setB):
n = setA.length()
for i in range(setB.length()):
x = setB.get(i+1)
k =
setA.locatebyvalue(x)
if (k==-1):
setA.insert(n+1, x)
n = n+1
setA = List([1, 2, 3, 4, 5])
setB = List([2, 4, 5, 10, 11])
setA.display()
setB.display()
un(setA, setB)
print("The union result is as follows")
setA.display()
setintersection.py:-
from listm import *
def intersection(seta,setb):
setC= List()
n = 0
for i in range(setA.length()):
x = setA.get(i+1)
k =
setB.locatebyvalue(x)
if (k>-1):
setC.insert(n+1, x)
n = n+1
return setC
setA = List([1, 2, 3, 4, 5,6,10])
setB = List([2, 4, 5, 10, 11])
setA.display()
setB.display()
setC = intersection(setA, setB)
print("The intersection result is as follows")
setC.display()
NOTE:- To access members in a class, first we need to create an object for that class, then we can access by using that object with '.' operator. The reason for getting error is setA and setB are List objects created by you.
we can not iterate through those objects. we didn't define any function in List class to iterate through list elements.
If you have any doubts, please comment below. Please give positive rating.THUMBS UP.
THANK YOU!!!
OUTPUT:-
setintersection.py

setunion.py

We were unable to transcribe this image
from listm import * def un(seta, setB): n = seta.length() for i in range (sets.length()): x = sets.get (i+1) k = seta.locatebyvalue (x) if (k==-1): setA.insert (n+1, x) n = n+1 seta = List([1, 2, 3, 4, 5]) setB = List([2, 4, 5, 10, 11]) setA.display ( ) setB.display ( ) un (seta, setB) print("The union result is as follows") setA.display ( ) Python 3.7.1 Shell File Edit Shell Debug Options Window Help Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bi 4)] on win32 Type "help", "copyright", "credits" or "license ()" for more information. >>> ============= RESTART: C:/Users/Bhanu Logan/Desktop/setunion.py ========= [1, 2, 3, 4, 5] [2, 4, 5, 10, 11] The union result is as follows [1, 2, 3, 4, 5, 10, 11] >>> |
I need to write a program based on two finished programs (listm.py and setunion.py) to implement...
Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation. Hi, I wrote the program but I don"t know why it showing the output - 000 000 0 0 My main.cpp file code is this - #include<stdio.h> #include<iostream> #include<fstream> #include "ArraySet.h" #include "ArraySet.cpp" using namespace std; int main(){ ArraySet<int> setA, setB, setC; // adds to setA setA.add(10); setA.add(20); setA.add(30); //...
Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...
In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...
Use python to code!
Q1 - You need to keep track of book titles. Write code using
function(s)
that will allow the user to do the following.
1. Add book titles to the list.
2. Delete book titles anywhere in the list by value.
3. Delete a book by position.
Note: if you wish to display
the booklist as a vertical number booklist , that is ok
(that is also a hint)
4. Insert book titles anywhere in the list....
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...
Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod { public static void main(String[] args) { String input; // To hold keyboard input String message; // Message...
Use the Concept of vector and string to do the following question 1) Write a program that creates a vector of string called V. Vector V grows and shrinks as the user processes the transactions from a data file called “data.txt”. The transaction file can only include three commands: Insert, Delete and Print. With Insert command, you get two more information, the information you need to insert and the position it should be inserted. With Delete, you get one more...