From 56f6d4654ce5cc1e02ee8a0c2b67bf4e1d6a5b45 Mon Sep 17 00:00:00 2001 From: Keith Warter Date: Wed, 22 Apr 2020 13:44:50 -0700 Subject: [PATCH] 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 --- .../create-a-set-class.english.md | 74 +++-- ...-difference-on-two-sets-of-data.english.md | 279 ++++++++++-------- ...ubset-check-on-two-sets-of-data.english.md | 247 +++++++++++----- .../perform-a-union-on-two-sets.english.md | 150 +++++----- ...ntersection-on-two-sets-of-data.english.md | 223 +++++++------- 5 files changed, 581 insertions(+), 392 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md index 65b5a344cd..3931573a4b 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md @@ -71,27 +71,31 @@ tests: ```js class Set { - constructor() { - // collection will hold our set - this.collection = []; - } - // 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 + constructor() { + // Dictionary will hold the items of our set + this.dictionary = {}; + this.length = 0; + } - // 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 class Set { constructor() { - this.collection = []; + this.dictionary = {}; + this.length = 0; } + has(element) { - return this.collection.indexOf(element) !== -1; + return this.dictionary[element] !== undefined; } + values() { - return this.collection; + return Object.keys(this.dictionary); } + add(element) { if (!this.has(element)) { - this.collection.push(element); - return true; - } else { - return false; - } - } - remove(element) { - if (this.has(element)) { - let i = this.collection.indexOf(element); - this.collection.splice(i, 1); + 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.collection.length; + return this.length; } } ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md index 5e6d8cc5f5..4f22b89699 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md @@ -36,64 +36,80 @@ tests:
```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // 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; - }; - // change code below this line - // change code above this line +class Set { + constructor() { + // 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 + has(element) { + return this.dictionary[element] !== undefined; + } + // this method will return all the values in the set + values() { + return Object.keys(this.dictionary); + } + // this method will add an element to the set + add(element) { + if (!this.has(element)) { + this.dictionary[element] = true; + this.length++; + return true; + } + + 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; + } + // 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 above this line } ``` @@ -104,72 +120,89 @@ function Set() {
```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // 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; +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; + } } ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md index f65e2cfb79..c2ff098f24 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md @@ -43,75 +43,92 @@ tests:
```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // 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 method will return the difference of two sets as a new set - 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; - }; - // change code below this line - // change code above this line +class Set { + constructor() { + // 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 + has(element) { + return this.dictionary[element] !== undefined; + } + // this method will return all the values in the set + values() { + return Object.keys(this.dictionary); + } + // this method will add an element to the set + add(element) { + if (!this.has(element)) { + this.dictionary[element] = true; + this.length++; + return true; + } + + 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; + } + // 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; + } + // 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() {
```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 + } +} ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md index 273b0fdb17..f5c7f3e0c3 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md @@ -34,41 +34,47 @@ tests:
```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // 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 set - this.size = function() { - return collection.length; - }; - // change code below this line +class Set { + constructor() { + // 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 + has(element) { + return this.dictionary[element] !== undefined; + } + // this method will return all the values in the set + values() { + return Object.keys(this.dictionary); + } + // this method will add an element to the set + add(element) { + if (!this.has(element)) { + this.dictionary[element] = true; + this.length++; + return true; + } - // 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() {
```js -function Set() { - var collection = []; +class Set { + constructor() { + this.dictionary = {}; + this.length = 0; + } - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; + has(element) { + return this.dictionary[element] !== undefined; + } - this.values = function() { - return collection; - }; + values() { + return Object.keys(this.dictionary); + } - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; + add(element) { + if (!this.has(element)) { + this.dictionary[element] = true; + this.length++; + return true; + } - this.remove = function(element) { - if(this.has(element)){ - var index = collection.indexOf(element); - collection.splice(index,1); - return true; - } - return false; - }; + return false; + } - this.size = function() { - return collection.length; - }; + remove(element) { + if (this.has(element)) { + delete this.dictionary[element]; + this.length--; + return true; + } - this.union = function(anotherSet){ - const newSet = new Set(); - const addToSet = el => newSet.add(el); - this.values().forEach(addToSet); - anotherSet.values().forEach(addToSet); - return newSet; - }; + 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; + } } ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md index 5c06e4ff79..1e41fc3245 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md @@ -35,53 +35,58 @@ tests:
```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // 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; - }; - // change code below this line - // change code above this line +class Set { + constructor() { + // 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 + has(element) { + return this.dictionary[element] !== undefined; + } + // this method will return all the values in the set + values() { + return Object.keys(this.dictionary); + } + // this method will add an element to the set + add(element) { + if (!this.has(element)) { + this.dictionary[element] = true; + this.length++; + return true; + } + + 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; + } + // 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; + } + // change code below this line + // change code above this line } ``` @@ -93,61 +98,77 @@ function Set() { ```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // 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; +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; + } } ```