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...
Redo Exercise 21.2-2 using a disjoint-set forest with union by rank and path compression.
Redo Exercise 21.2-2 using a disjoint-set forest with union by rank and path compression.
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...
Using JavaScript for the following assignment Design and code a function named "intersection" that takes two...
Using JavaScript for the following assignment Design and code a function named "intersection" that takes two arrays of numbers in parameter and returns a new array containing only the values that are both in the two arrays. A value must not be found more than once in the resulting array but can be found more than once in the arrays in parameter. The order of the elements in the resulting array must respect the order of the first parameter. For...
Using the definition of Compact Set, prove that the union of two compact sets is compact....
Using the definition of Compact Set, prove that the union of two compact sets is compact. Use this result to show that the union of a finite collection of compact sets is compact. Is the union of any collection of compact sets compact?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT