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

@ -72,17 +72,21 @@ 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
// This method will check for the presence of an element and return true or false
has(element) { has(element) {
return this.collection.indexOf(element) !== -1; return this.dictionary[element] !== undefined;
} }
// this method will return all the values in the set
// This method will return all the values in the set as an array
values() { values() {
return this.collection; return Object.keys(this.dictionary);
} }
// change code below this line // change code below this line
// write your add method here // write your add method here
@ -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;
this.length++;
return true; return true;
} else { }
return false; return false;
} }
}
remove(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
let i = this.collection.indexOf(element); delete this.dictionary[element];
this.collection.splice(i, 1); this.length--;
return true; return true;
} }
return false; return false;
} }
size() { size() {
return this.collection.length; return this.length;
} }
} }
``` ```

View File

@ -36,62 +36,78 @@ 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.dictionary = {};
this.length = 0;
}
// this method will check for the presence of an element and return true or false // this method will check for the presence of an element and return true or false
this.has = function(element) { has(element) {
return (collection.indexOf(element) !== -1); return this.dictionary[element] !== undefined;
}; }
// this method will return all the values in the set // this method will return all the values in the set
this.values = function() { values() {
return collection; return Object.keys(this.dictionary);
}; }
// this method will add an element to the set // this method will add an element to the set
this.add = function(element) { add(element) {
if (!this.has(element)) { if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
this.length++;
return true; return true;
} }
return false; return false;
}; }
// this method will remove an element from a set // this method will remove an element from a set
this.remove = function(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; 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);
} }
}); // this method will return the size of the set
return intersectionSet; size() {
}; return this.length;
}
// This is our union method from that lesson
union(set) {
const newSet = new Set();
this.values().forEach(value => {
newSet.add(value);
})
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
// This is our intersection method from that lesson
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;
}
// change code below this line // change code below this line
// change code above this line // change code above this line
} }
@ -104,71 +120,88 @@ 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) { }
add(element) {
if (!this.has(element)) { if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
this.length++;
return true; return true;
} }
return false; return false;
}; }
// this method will remove an element from a set
this.remove = function(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; 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; size() {
}; return this.length;
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; 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,73 +43,90 @@ 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.dictionary = {};
this.length = 0;
}
// this method will check for the presence of an element and return true or false // this method will check for the presence of an element and return true or false
this.has = function(element) { has(element) {
return (collection.indexOf(element) !== -1); return this.dictionary[element] !== undefined;
}; }
// this method will return all the values in the set // this method will return all the values in the set
this.values = function() { values() {
return collection; return Object.keys(this.dictionary);
}; }
// this method will add an element to the set // this method will add an element to the set
this.add = function(element) { add(element) {
if (!this.has(element)) { if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
this.length++;
return true; return true;
} }
return false; return false;
}; }
// this method will remove an element from a set // this method will remove an element from a set
this.remove = function(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; 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);
} }
}); // this method will return the size of the set
return intersectionSet; size() {
}; return this.length;
// this method will return the difference of two sets as a new set }
this.difference = function(otherSet) { // This is our union method from that lesson
var differenceSet = new Set(); union(set) {
var firstSet = this.values(); const newSet = new Set();
firstSet.forEach(function(e){ this.values().forEach(value => {
if(!otherSet.has(e)){ newSet.add(value);
differenceSet.add(e); })
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
// This is our intersection method from that lesson
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;
}
// 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;
} }
});
return differenceSet;
};
// change code below this line // change code below this line
// change code above 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,38 +34,44 @@ 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.dictionary = {};
this.length = 0;
}
// this method will check for the presence of an element and return true or false // this method will check for the presence of an element and return true or false
this.has = function(element) { has(element) {
return (collection.indexOf(element) !== -1); return this.dictionary[element] !== undefined;
}; }
// this method will return all the values in the set // this method will return all the values in the set
this.values = function() { values() {
return collection; return Object.keys(this.dictionary);
}; }
// this method will add an element to the set // this method will add an element to the set
this.add = function(element) { add(element) {
if (!this.has(element)) { if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
this.length++;
return true; return true;
} }
return false; return false;
}; }
// this method will remove an element from a set // this method will remove an element from a set
this.remove = function(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; return false;
}; }
// this method will return the size of the set // this method will return the size of the set
this.size = function() { size() {
return collection.length; return this.length;
}; }
// change code below this line // change code below this line
// change code above 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;
this.length++;
return true; return true;
} }
return false;
};
this.remove = function(element) { return false;
}
remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; return false;
}; }
this.size = function() { size() {
return collection.length; return this.length;
}; }
this.union = function(anotherSet){ union(set) {
const newSet = new Set(); const newSet = new Set();
const addToSet = el => newSet.add(el); this.values().forEach(value => {
this.values().forEach(addToSet); newSet.add(value);
anotherSet.values().forEach(addToSet); })
set.values().forEach(value => {
newSet.add(value);
})
return newSet; return newSet;
}; }
} }
``` ```

View File

@ -35,51 +35,56 @@ 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.dictionary = {};
this.length = 0;
}
// this method will check for the presence of an element and return true or false // this method will check for the presence of an element and return true or false
this.has = function(element) { has(element) {
return (collection.indexOf(element) !== -1); return this.dictionary[element] !== undefined;
}; }
// this method will return all the values in the set // this method will return all the values in the set
this.values = function() { values() {
return collection; return Object.keys(this.dictionary);
}; }
// this method will add an element to the set // this method will add an element to the set
this.add = function(element) { add(element) {
if (!this.has(element)) { if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
this.length++;
return true; return true;
} }
return false; return false;
}; }
// this method will remove an element from a set // this method will remove an element from a set
this.remove = function(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; return false;
}; }
// this method will return the size of the collection // this method will return the size of the set
this.size = function() { size() {
return collection.length; return this.length;
}; }
// this method will return the union of two sets // This is our union method from that lesson
this.union = function(otherSet) { union(set) {
var unionSet = new Set(); const newSet = new Set();
var firstSet = this.values(); this.values().forEach(value => {
var secondSet = otherSet.values(); newSet.add(value);
firstSet.forEach(function(e){ })
unionSet.add(e); set.values().forEach(value => {
}); newSet.add(value);
secondSet.forEach(function(e){ })
unionSet.add(e);
}); return newSet;
return unionSet; }
};
// change code below this line // change code below this line
// change code above this line // change code above this line
} }
@ -93,60 +98,76 @@ 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) { }
add(element) {
if (!this.has(element)) { if (!this.has(element)) {
collection.push(element); this.dictionary[element] = true;
this.length++;
return true; return true;
} }
return false; return false;
}; }
// this method will remove an element from a set
this.remove = function(element) { remove(element) {
if (this.has(element)) { if (this.has(element)) {
var index = collection.indexOf(element); delete this.dictionary[element];
collection.splice(index,1); this.length--;
return true; return true;
} }
return false; return false;
}; }
// this method will return the size of the collection
this.size = function() { size() {
return collection.length; return this.length;
}; }
// this method will return the union of two sets
this.union = function(otherSet) { union(set) {
var unionSet = new Set(); const newSet = new Set();
var firstSet = this.values(); this.values().forEach(value => {
var secondSet = otherSet.values(); newSet.add(value);
firstSet.forEach(function(e){ })
unionSet.add(e); set.values().forEach(value => {
}); newSet.add(value);
secondSet.forEach(function(e){ })
unionSet.add(e);
}); return newSet;
return unionSet; }
};
this.intersection = function(otherSet) { intersection(set) {
var intersectionSet = new Set(); const newSet = new Set();
var firstSet = this.values();
firstSet.forEach(function(e) { let largeSet;
if (otherSet.has(e)) { let smallSet;
intersectionSet.add(e); 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 intersectionSet;
return newSet;
} }
} }
``` ```