fix(learn): update solutions in interview-prep challenges (#38430)

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Keith Warter
2020-04-22 13:44:50 -07:00
committed by GitHub
parent 4b09ec2786
commit 56f6d4654c
5 changed files with 581 additions and 392 deletions

View File

@ -71,27 +71,31 @@ tests:
```js ```js
class Set { class Set {
constructor() { constructor() {
// collection will hold our set // Dictionary will hold the items of our set
this.collection = []; this.dictionary = {};
} this.length = 0;
// this method will check for the presence of an element and return true or false }
has(element) {
return this.collection.indexOf(element) !== -1;
}
// this method will return all the values in the set
values() {
return this.collection;
}
// change code below this line
// write your add method here // This method will check for the presence of an element and return true or false
has(element) {
return this.dictionary[element] !== undefined;
}
// write your remove method here // This method will return all the values in the set as an array
values() {
return Object.keys(this.dictionary);
}
// write your size method here // change code below this line
// write your add method here
// change code above this line // write your remove method here
// write your size method here
// change code above this line
} }
``` ```
@ -105,32 +109,40 @@ class Set {
```js ```js
class Set { class Set {
constructor() { constructor() {
this.collection = []; this.dictionary = {};
this.length = 0;
} }
has(element) { has(element) {
return this.collection.indexOf(element) !== -1; return this.dictionary[element] !== undefined;
} }
values() { values() {
return this.collection; return Object.keys(this.dictionary);
} }
add(element) { add(element) {
if (!this.has(element)) { if (!this.has(element)) {
this.collection.push(element); this.dictionary[element] = true;
return true; this.length++;
} else {
return false;
}
}
remove(element) {
if (this.has(element)) {
let i = this.collection.indexOf(element);
this.collection.splice(i, 1);
return true; return true;
} }
return false; return false;
} }
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
size() { size() {
return this.collection.length; return this.length;
} }
} }
``` ```

View File

@ -36,64 +36,80 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function Set() { class Set {
// the var collection will hold the set constructor() {
var collection = []; // This will hold the set
// this method will check for the presence of an element and return true or false this.dictionary = {};
this.has = function(element) { this.length = 0;
return (collection.indexOf(element) !== -1); }
}; // this method will check for the presence of an element and return true or false
// this method will return all the values in the set has(element) {
this.values = function() { return this.dictionary[element] !== undefined;
return collection; }
}; // this method will return all the values in the set
// this method will add an element to the set values() {
this.add = function(element) { return Object.keys(this.dictionary);
if(!this.has(element)){ }
collection.push(element); // this method will add an element to the set
return true; add(element) {
} if (!this.has(element)) {
return false; this.dictionary[element] = true;
}; this.length++;
// this method will remove an element from a set return true;
this.remove = function(element) { }
if(this.has(element)){
var index = collection.indexOf(element); return false;
collection.splice(index,1); }
return true; // this method will remove an element from a set
} remove(element) {
return false; if (this.has(element)) {
}; delete this.dictionary[element];
// this method will return the size of the collection this.length--;
this.size = function() { return true;
return collection.length; }
};
// this method will return the union of two sets return false;
this.union = function(otherSet) { }
var unionSet = new Set(); // this method will return the size of the set
var firstSet = this.values(); size() {
var secondSet = otherSet.values(); return this.length;
firstSet.forEach(function(e){ }
unionSet.add(e); // This is our union method from that lesson
}); union(set) {
secondSet.forEach(function(e){ const newSet = new Set();
unionSet.add(e); this.values().forEach(value => {
}); newSet.add(value);
return unionSet; })
}; set.values().forEach(value => {
// this method will return the intersection of two sets as a new set newSet.add(value);
this.intersection = function(otherSet) { })
var intersectionSet = new Set();
var firstSet = this.values(); return newSet;
firstSet.forEach(function(e){ }
if(otherSet.has(e)){ // This is our intersection method from that lesson
intersectionSet.add(e); intersection(set) {
} const newSet = new Set();
});
return intersectionSet; let largeSet;
}; let smallSet;
// change code below this line if (this.dictionary.length > set.length) {
// change code above this line largeSet = this;
smallSet = set;
} else {
largeSet = set;
smallSet = this;
}
smallSet.values().forEach(value => {
if (largeSet.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
// change code below this line
// change code above this line
} }
``` ```
@ -104,72 +120,89 @@ function Set() {
<section id='solution'> <section id='solution'>
```js ```js
function Set() { class Set {
// the var collection will hold the set constructor() {
var collection = []; this.dictionary = {};
// this method will check for the presence of an element and return true or false this.length = 0;
this.has = function(element) { }
return (collection.indexOf(element) !== -1);
}; has(element) {
// this method will return all the values in the set return this.dictionary[element] !== undefined;
this.values = function() { }
return collection;
}; values() {
// this method will add an element to the set return Object.keys(this.dictionary);
this.add = function(element) { }
if(!this.has(element)){
collection.push(element); add(element) {
return true; if (!this.has(element)) {
} this.dictionary[element] = true;
return false; this.length++;
}; return true;
// this method will remove an element from a set
this.remove = function(element) {
if(this.has(element)){
var index = collection.indexOf(element);
collection.splice(index,1);
return true;
}
return false;
};
// this method will return the size of the collection
this.size = function() {
return collection.length;
};
// this method will return the union of two sets
this.union = function(otherSet) {
var unionSet = new Set();
var firstSet = this.values();
var secondSet = otherSet.values();
firstSet.forEach(function(e){
unionSet.add(e);
});
secondSet.forEach(function(e){
unionSet.add(e);
});
return unionSet;
};
// this method will return the intersection of two sets as a new set
this.intersection = function(otherSet) {
var intersectionSet = new Set();
var firstSet = this.values();
firstSet.forEach(function(e){
if(otherSet.has(e)){
intersectionSet.add(e);
}
});
return intersectionSet;
};
this.difference = function(otherSet) {
var differenceSet = new Set();
var firstSet = this.values();
firstSet.forEach(function(e) {
if (!otherSet.has(e)) {
differenceSet.add(e);
}
});
return differenceSet;
} }
return false;
}
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
size() {
return this.length;
}
union(set) {
const newSet = new Set();
this.values().forEach(value => {
newSet.add(value);
})
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
intersection(set) {
const newSet = new Set();
let largeSet;
let smallSet;
if (this.dictionary.length > set.length) {
largeSet = this;
smallSet = set;
} else {
largeSet = set;
smallSet = this;
}
smallSet.values().forEach(value => {
if (largeSet.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
difference(set) {
const newSet = new Set();
this.values().forEach(value => {
if (!set.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
} }
``` ```

View File

@ -43,75 +43,92 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function Set() { class Set {
// the var collection will hold the set constructor() {
var collection = []; // This will hold the set
// this method will check for the presence of an element and return true or false this.dictionary = {};
this.has = function(element) { this.length = 0;
return (collection.indexOf(element) !== -1); }
}; // this method will check for the presence of an element and return true or false
// this method will return all the values in the set has(element) {
this.values = function() { return this.dictionary[element] !== undefined;
return collection; }
}; // this method will return all the values in the set
// this method will add an element to the set values() {
this.add = function(element) { return Object.keys(this.dictionary);
if(!this.has(element)){ }
collection.push(element); // this method will add an element to the set
return true; add(element) {
} if (!this.has(element)) {
return false; this.dictionary[element] = true;
}; this.length++;
// this method will remove an element from a set return true;
this.remove = function(element) { }
if(this.has(element)){
var index = collection.indexOf(element); return false;
collection.splice(index,1); }
return true; // this method will remove an element from a set
} remove(element) {
return false; if (this.has(element)) {
}; delete this.dictionary[element];
// this method will return the size of the collection this.length--;
this.size = function() { return true;
return collection.length; }
};
// this method will return the union of two sets return false;
this.union = function(otherSet) { }
var unionSet = new Set(); // this method will return the size of the set
var firstSet = this.values(); size() {
var secondSet = otherSet.values(); return this.length;
firstSet.forEach(function(e){ }
unionSet.add(e); // This is our union method from that lesson
}); union(set) {
secondSet.forEach(function(e){ const newSet = new Set();
unionSet.add(e); this.values().forEach(value => {
}); newSet.add(value);
return unionSet; })
}; set.values().forEach(value => {
// this method will return the intersection of two sets as a new set newSet.add(value);
this.intersection = function(otherSet) { })
var intersectionSet = new Set();
var firstSet = this.values(); return newSet;
firstSet.forEach(function(e){ }
if(otherSet.has(e)){ // This is our intersection method from that lesson
intersectionSet.add(e); intersection(set) {
} const newSet = new Set();
});
return intersectionSet; let largeSet;
}; let smallSet;
// this method will return the difference of two sets as a new set if (this.dictionary.length > set.length) {
this.difference = function(otherSet) { largeSet = this;
var differenceSet = new Set(); smallSet = set;
var firstSet = this.values(); } else {
firstSet.forEach(function(e){ largeSet = set;
if(!otherSet.has(e)){ smallSet = this;
differenceSet.add(e); }
}
}); smallSet.values().forEach(value => {
return differenceSet; if (largeSet.dictionary[value]) {
}; newSet.add(value);
// change code below this line }
// change code above this line })
return newSet;
}
// This is our difference method from that lesson
difference(set) {
const newSet = new Set();
this.values().forEach(value => {
if (!set.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
// change code below this line
// change code above this line
} }
``` ```
@ -122,7 +139,97 @@ function Set() {
<section id='solution'> <section id='solution'>
```js ```js
function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};this.intersection = function(set) {var i = new Set();var c = this.values();c.forEach(function(element){if(s.has(element)) i.add(element);});};this.difference = function(set) {var d = new Set();var c = this.values();c.forEach(function(e){if(!set.has(e)) d.add(e);});};this.subset = function(set) {var isSubset = true;var c = this.values();c.forEach(function(e){if(!set.has(e)) isSubset = false;});return isSubset;};} class Set {
constructor() {
this.dictionary = {};
this.length = 0;
}
has(element) {
return this.dictionary[element] !== undefined;
}
values() {
return Object.keys(this.dictionary);
}
add(element) {
if (!this.has(element)) {
this.dictionary[element] = true;
this.length++;
return true;
}
return false;
}
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
size() {
return this.length;
}
union(set) {
const newSet = new Set();
this.values().forEach(value => {
newSet.add(value);
})
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
intersection(set) {
const newSet = new Set();
let largeSet;
let smallSet;
if (this.dictionary.length > set.length) {
largeSet = this;
smallSet = set;
} else {
largeSet = set;
smallSet = this;
}
smallSet.values().forEach(value => {
if (largeSet.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
difference(set) {
const newSet = new Set();
this.values().forEach(value => {
if (!set.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
subset(set) {
for(const value of this.values()){
if(!set.dictionary[value]) return false;
}
return true
}
}
``` ```
</section> </section>

View File

@ -34,41 +34,47 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function Set() { class Set {
// the var collection will hold the set constructor() {
var collection = []; // This will hold the set
// this method will check for the presence of an element and return true or false this.dictionary = {};
this.has = function(element) { this.length = 0;
return (collection.indexOf(element) !== -1); }
}; // this method will check for the presence of an element and return true or false
// this method will return all the values in the set has(element) {
this.values = function() { return this.dictionary[element] !== undefined;
return collection; }
}; // this method will return all the values in the set
// this method will add an element to the set values() {
this.add = function(element) { return Object.keys(this.dictionary);
if(!this.has(element)){ }
collection.push(element); // this method will add an element to the set
return true; add(element) {
} if (!this.has(element)) {
return false; this.dictionary[element] = true;
}; this.length++;
// this method will remove an element from a set return true;
this.remove = function(element) { }
if(this.has(element)){
var index = collection.indexOf(element);
collection.splice(index,1);
return true;
}
return false;
};
// this method will return the size of the set
this.size = function() {
return collection.length;
};
// change code below this line
// change code above this line return false;
}
// this method will remove an element from a set
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
// this method will return the size of the set
size() {
return this.length;
}
// change code below this line
// change code above this line
} }
``` ```
@ -79,45 +85,55 @@ function Set() {
<section id='solution'> <section id='solution'>
```js ```js
function Set() { class Set {
var collection = []; constructor() {
this.dictionary = {};
this.length = 0;
}
this.has = function(element) { has(element) {
return (collection.indexOf(element) !== -1); return this.dictionary[element] !== undefined;
}; }
this.values = function() { values() {
return collection; return Object.keys(this.dictionary);
}; }
this.add = function(element) { add(element) {
if(!this.has(element)){ if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
return true; this.length++;
} return true;
return false; }
};
this.remove = function(element) { return false;
if(this.has(element)){ }
var index = collection.indexOf(element);
collection.splice(index,1);
return true;
}
return false;
};
this.size = function() { remove(element) {
return collection.length; if (this.has(element)) {
}; delete this.dictionary[element];
this.length--;
return true;
}
this.union = function(anotherSet){ return false;
const newSet = new Set(); }
const addToSet = el => newSet.add(el);
this.values().forEach(addToSet); size() {
anotherSet.values().forEach(addToSet); return this.length;
return newSet; }
};
union(set) {
const newSet = new Set();
this.values().forEach(value => {
newSet.add(value);
})
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
} }
``` ```

View File

@ -35,53 +35,58 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function Set() { class Set {
// the var collection will hold the set constructor() {
var collection = []; // This will hold the set
// this method will check for the presence of an element and return true or false this.dictionary = {};
this.has = function(element) { this.length = 0;
return (collection.indexOf(element) !== -1); }
}; // this method will check for the presence of an element and return true or false
// this method will return all the values in the set has(element) {
this.values = function() { return this.dictionary[element] !== undefined;
return collection; }
}; // this method will return all the values in the set
// this method will add an element to the set values() {
this.add = function(element) { return Object.keys(this.dictionary);
if(!this.has(element)){ }
collection.push(element); // this method will add an element to the set
return true; add(element) {
} if (!this.has(element)) {
return false; this.dictionary[element] = true;
}; this.length++;
// this method will remove an element from a set return true;
this.remove = function(element) { }
if(this.has(element)){
var index = collection.indexOf(element); return false;
collection.splice(index,1); }
return true; // this method will remove an element from a set
} remove(element) {
return false; if (this.has(element)) {
}; delete this.dictionary[element];
// this method will return the size of the collection this.length--;
this.size = function() { return true;
return collection.length; }
};
// this method will return the union of two sets return false;
this.union = function(otherSet) { }
var unionSet = new Set(); // this method will return the size of the set
var firstSet = this.values(); size() {
var secondSet = otherSet.values(); return this.length;
firstSet.forEach(function(e){ }
unionSet.add(e); // This is our union method from that lesson
}); union(set) {
secondSet.forEach(function(e){ const newSet = new Set();
unionSet.add(e); this.values().forEach(value => {
}); newSet.add(value);
return unionSet; })
}; set.values().forEach(value => {
// change code below this line newSet.add(value);
// change code above this line })
return newSet;
}
// change code below this line
// change code above this line
} }
``` ```
@ -93,61 +98,77 @@ function Set() {
```js ```js
function Set() { class Set {
// the var collection will hold the set constructor() {
var collection = []; this.dictionary = {};
// this method will check for the presence of an element and return true or false this.length = 0;
this.has = function(element) { }
return (collection.indexOf(element) !== -1);
}; has(element) {
// this method will return all the values in the set return this.dictionary[element] !== undefined;
this.values = function() { }
return collection;
}; values() {
// this method will add an element to the set return Object.keys(this.dictionary);
this.add = function(element) { }
if(!this.has(element)){
collection.push(element); add(element) {
return true; if (!this.has(element)) {
} this.dictionary[element] = true;
return false; this.length++;
}; return true;
// this method will remove an element from a set
this.remove = function(element) {
if(this.has(element)){
var index = collection.indexOf(element);
collection.splice(index,1);
return true;
}
return false;
};
// this method will return the size of the collection
this.size = function() {
return collection.length;
};
// this method will return the union of two sets
this.union = function(otherSet) {
var unionSet = new Set();
var firstSet = this.values();
var secondSet = otherSet.values();
firstSet.forEach(function(e){
unionSet.add(e);
});
secondSet.forEach(function(e){
unionSet.add(e);
});
return unionSet;
};
this.intersection = function(otherSet) {
var intersectionSet = new Set();
var firstSet = this.values();
firstSet.forEach(function(e) {
if (otherSet.has(e)) {
intersectionSet.add(e);
}
})
return intersectionSet;
} }
return false;
}
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
size() {
return this.length;
}
union(set) {
const newSet = new Set();
this.values().forEach(value => {
newSet.add(value);
})
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
intersection(set) {
const newSet = new Set();
let largeSet;
let smallSet;
if (this.dictionary.length > set.length) {
largeSet = this;
smallSet = set;
} else {
largeSet = set;
smallSet = this;
}
smallSet.values().forEach(value => {
if (largeSet.dictionary[value]) {
newSet.add(value);
}
})
return newSet;
}
} }
``` ```