Question

In: Computer Science

1. Implement the union set function using the prototype. 2. Implement the intersection set function using...

1. Implement the union set function using the prototype.

2. Implement the intersection set function using the prototype.

3. Implement the set difference function using the prototype.

4. Implement the subset function using the prototype.

Solutions

Expert Solution

a)Union

Set.prototype.union = function(otherSet) { 
    var unionSet = new Set(); 
    for (var elem of this) { 
        unionSet.add(elem); 
    } 
    for(var elem of otherSet) 
        unionSet.add(elem);  
    return unionSet; 
} 

var set1 = new Set([10, 20, 30, 40, 50]); 
var set2 = new Set([40, 50, 60, 70, 80]);   
  

var unionSet = set1.union(set2); 

console.log(unionSet.values()); 

b) Intersection

Set.prototype.intersection = function(otherSet) { 
    var intersectionSet = new Set(); 
    for(var elem of otherSet) { 
        if(this.has(elem)) 
            intersectionSet.add(elem); 
    } 
  
return intersectionSet;                 
} 

var set1 = new Set([10, 20, 30, 40, 50]); 
var set2 = new Set([40, 50, 60, 70, 80]);   

var intersectionSet = set1.intersection(set2); 
  
console.log(intersectionSet.values()); 

c)Diffrence

Set.prototype.difference = function(otherSet) { 

     var differenceSet = new Set(); 
     for(var elem of this) { 
        if(!otherSet.has(elem)) 
            differenceSet.add(elem); 
    } 
    return differenceSet; 
} 

var set1 = new Set([10, 20, 30, 40, 50]); 
var set2 = new Set([40, 50, 60, 70, 80]);   

var differenceSet = set1.difference(set2); 

console.log(differenceSet); 

d)subset

Set.prototype.subSet = function(otherSet) { 

    if(this.size > otherSet.size) 
        return false; 
    else{ 
        for(var elem of this) { 
            if(!otherSet.has(elem)) 
                return false; 
        } 
        return true; 
    } 
} 

var setA = new Set([10, 20, 30]); 
var setB = new Set([50, 60, 10, 20, 30, 40]); 
var setC = new Set([10, 30, 40, 50]); 

console.log(setA.subSet(setB)); 

console.log(setA.subSet(setC)); 

console.log(setC.subSet(setB)); 

Happy Coding.

PLEASE GIVE A THUMBSUP IF IT WAS HELPFUL AND LEAVE A COMMENT FOR ANY QUERY.

THANKS.


Related Solutions

Prove the following Theorems: 1. A finite union of compact sets is compact. 2. Any intersection...
Prove the following Theorems: 1. A finite union of compact sets is compact. 2. Any intersection of compact set is compact. 3. A closed subset of a compact set is compact. 4. Every finite set in IRn is compact.
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). Type your answers. The following database schema is given: ATHLETE(name,age,height,weight,country) RACE(id,location,date,time-start,distance) COMPETES(aname,rid,time,position) where ATHLETE has information about runners (their name, age, height, weight, and nationality); RACE has information about races (id, location, date when it’s held, time it starts, and distance ran); and COMPETES keeps track of which runners run on with race, the time it took them to complete the race,...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). Type your answers. Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid...
Function writing: 1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes...
Function writing: 1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes three integer parameters. It returns true if all three of the parameters are odd, and false otherwise. 2). (1 point) Write a sample call for this function. (Assume that you are calling from main) 3). (3 points) Write the definition (header and body) of the function.
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
For the second exercise determine the union, intersection and difference of the square and cube of...
For the second exercise determine the union, intersection and difference of the square and cube of integers ranging from 1 to 100. Sets are clearly the way to go here. You can use Math functions to generate the sets for the square and cube for the Integers. The following functionality should be available for the user via a simple interface: 1. Display the Square and Cube for Integers ranging from 1 to 100 2. Search the sets for a specific...
2. working with databases and files in python a) Write a function with prototype “def profound():”...
2. working with databases and files in python a) Write a function with prototype “def profound():” that will prompt the user to type something profound. It will then record the date and time using the “datetime” module and then append the date, time and profound line to a file called “profound.txt”. Do only one line per function call. Use a single write and f-string such that the file contents look like: 2020-10-27 11:20:22 -- Has eighteen letters does 2020-10-27 11:20:36...
Prove that the intersection of two compact sets is compact, using criterion (2).
  Question: Prove that the intersection of two compact sets is compact, using criterion (2). Prove that the intersection of two compact sets is compact, using criterion (1). Prove that the intersection of two compact sets is compact, using criterion (3). Probably the most important new idea you'll encounter in real analysis is the concept of compactness. It's the compactness of [a, b] that makes a continuous function reach its maximum and that makes the Riemann in- tegral exist. For...
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT