Merge pull request #135 from Alexandrshy/issues-134-formatting-problems-hash-tables

Fixed formatting problems
This commit is contained in:
Aditya Bhargava
2020-09-14 10:47:23 -05:00
committed by GitHub
7 changed files with 264 additions and 188 deletions

View File

@@ -1,14 +1,18 @@
const voted = {};
const checkVoter = (name) => {
/**
* Vote check
* @param {string} name Voter name
*/
const checkVoter = name => {
if (voted[name]) {
console.log('kick them out!');
console.log("kick them out!");
} else {
voted[name] = true;
console.log('let them vote!');
console.log("let them vote!");
}
};
checkVoter('tom'); // let them vote!
checkVoter('mike'); // let them vote!
checkVoter('mike'); // kick them out!
checkVoter("tom"); // let them vote!
checkVoter("mike"); // let them vote!
checkVoter("mike"); // kick them out!

View File

@@ -1,16 +1,20 @@
'use strict';
"use strict";
const voted = {};
/**
* Vote check
* @param {string} name Voter name
*/
function check_voter(name) {
if (voted[name]) {
console.log('kick them out!');
console.log("kick them out!");
} else {
voted[name] = true;
console.log('let them vote!');
console.log("let them vote!");
}
}
check_voter("tom"); // let them vote!
check_voter("mike"); // let them vote!
check_voter("mike"); // kick them out!

View File

@@ -1,173 +1,162 @@
/**
* Class HashTable
*
* @param {object} obj
* @param {Object} obj
*/
let HashTable = function( obj ) {
let length = 0;
this._items = ( function( obj ) {
let items = {};
for ( let p in obj ) {
items[p] = obj[p];
length++;
}
return items;
}( obj ) );
const HashTable = function(obj) {
let length = 0;
this._items = (function(obj) {
let items = {};
for (let p in obj) {
items[p] = obj[p];
length++;
}
return items;
})(obj);
/**
* Associates the specified value to the specified key
*
* @param {string} key The key to which associate the value
* @param {string} value The value to associate to the key
*
* @return {(undefined|object)} Undefined is object didn't exists before this call
*/
this.set = function( key, value ) {
let previous = undefined;
/**
* Associates the specified value to the specified key
* @param {string} key The key to which associate the value
* @param {string} value The value to associate to the key
* @returns {(undefined|Object)} Undefined is object didn't exists before this call
*/
this.set = function(key, value) {
let previous = undefined;
if ( this.has( key ) ) {
previous = this._items[key];
} else {
length++;
}
if (this.has(key)) {
previous = this._items[key];
} else {
length++;
}
this._items[key] = value;
this._items[key] = value;
return previous;
};
return previous;
};
/**
* Returns the value associated to the specified key
*
* @param {string} key The key from which retrieve the value
*
* @return {(undefined|string)} Undefined or associated value
*/
this.get = function( key ) {
return this._items.hasOwnProperty( key ) ? this._items[key] : undefined;
};
/**
* Returns the value associated to the specified key
* @param {string} key The key from which retrieve the value
* @returns {(undefined|string)} Undefined or associated value
*/
this.get = function(key) {
return this._items.hasOwnProperty(key) ? this._items[key] : undefined;
};
/**
* Returns whether the hashtable contains the specified key
*
* @param {string} key The key to check
*
* @return {boolean}
*/
this.has = function( key ) {
return this._items.hasOwnProperty( key );
};
/**
* Returns whether the hashtable contains the specified key
* @param {string} key The key to check
* @returns {boolean}
*/
this.has = function(key) {
return this._items.hasOwnProperty(key);
};
/**
* Removes the specified key with its value
*
* @param {string} key The key to remove
*
* @return {(undefined|string)} Undefined if key doesn't exist and
* string (previous value) - value of deleted item
*/
this.remove = function( key ) {
if ( this.has( key ) ) {
let previous = this._items[key];
length--;
delete this._items[key];
return previous;
} else {
return undefined;
}
};
/**
* Removes the specified key with its value
* @param {string} key The key to remove
* @returns {(undefined|string)} Undefined if key doesn't exist and
* string (previous value) - value of deleted item
*/
this.remove = function(key) {
if (this.has(key)) {
let previous = this._items[key];
length--;
delete this._items[key];
return previous;
} else {
return undefined;
}
};
/**
* Returns an array with all the registered keys
*
* @return {array}
*/
this.getKeys = function() {
let keys = [];
/**
* Returns an array with all the registered keys
* @returns {Array}
*/
this.getKeys = function() {
let keys = [];
for ( let i in this._items ) {
if ( this.has( i ) ) {
keys.push( i );
}
}
for (let i in this._items) {
if (this.has(i)) {
keys.push(i);
}
}
return keys;
};
return keys;
};
/**
* Returns an array with all the registered values
*
* @return {array}
*/
this.getValues = function() {
let values = [];
/**
* Returns an array with all the registered values
* @returns {Array}
*/
this.getValues = function() {
let values = [];
for ( let i in this._items ) {
if ( this.has( i ) ) {
values.push( this._items[i] );
}
}
for (let i in this._items) {
if (this.has(i)) {
values.push(this._items[i]);
}
}
return values;
};
return values;
};
/**
* Iterates all entries in the specified iterator callback
* @param {function} callback A method with 2 parameters: key, value
*/
this.each = function( callback ) {
for ( let i in this._items ) {
if ( this.has( i ) ) {
callback( i, this._items[i] );
}
}
};
/**
* Iterates all entries in the specified iterator callback
* @param {function} callback A method with 2 parameters: key, value
*/
this.each = function(callback) {
for (let i in this._items) {
if (this.has(i)) {
callback(i, this._items[i]);
}
}
};
/**
* Deletes all the key-value pairs on the hashmap
*/
this.clear = function() {
this._items = {};
length = 0;
};
/**
* Deletes all the key-value pairs on the hashmap
*/
this.clear = function() {
this._items = {};
length = 0;
};
/**
* Gets the count of the entries in the hashtable
*/
Object.defineProperty( this, 'length', {
get: function() {
return length;
},
});
/**
* Gets the count of the entries in the hashtable
*/
Object.defineProperty(this, "length", {
get: function() {
return length;
}
});
/**
* Gets an array of all keys in the hashtable
*/
Object.defineProperty(this, 'keys', {
get: function() {
return this.getKeys();
},
});
/**
* Gets an array of all keys in the hashtable
*/
Object.defineProperty(this, "keys", {
get: function() {
return this.getKeys();
}
});
/**
* Gets an array of all values in the hashtable
*/
Object.defineProperty(this, 'values', {
get: function() {
return this.getValues();
},
});
/**
* Gets an array of all values in the hashtable
*/
Object.defineProperty(this, "values", {
get: function() {
return this.getValues();
}
});
};
let hashtable = new HashTable({'one': 1, 'two': 2, 'three': 3, 'cuatro': 4});
const hashtable = new HashTable({ one: 1, two: 2, three: 3, cuatro: 4 });
console.log( 'Original length: ' + hashtable.length ); // Original length: 4
console.log( 'Value of key "one": ' + hashtable.get( 'one' ) ); // Value of key "one": 1
console.log( 'Has key "foo"? ' + hashtable.has( 'foo' )); // Has key "foo"? false
console.log( 'Previous value of key "foo": ' + hashtable.set( 'foo', 'bar' ) ); // Previous value of key "foo": undefined
console.log( 'Length after set: ' + hashtable.length ); // Length after set: 5
console.log( 'Value of key "foo": ' + hashtable.get( 'foo' ) ); // Value of key "foo": bar
console.log( 'Value of key "cuatro": ' + hashtable.get( 'cuatro' )); // Value of key "cuatro": 4
console.log( 'Get keys by using property: ' + hashtable.keys ); // Get keys by using property: one,two,three,cuatro,foo
console.log("Original length: " + hashtable.length); // Original length: 4
console.log('Value of key "one": ' + hashtable.get("one")); // Value of key "one": 1
console.log('Has key "foo"? ' + hashtable.has("foo")); // Has key "foo"? false
console.log('Previous value of key "foo": ' + hashtable.set("foo", "bar")); // Previous value of key "foo": undefined
console.log("Length after set: " + hashtable.length); // Length after set: 5
console.log('Value of key "foo": ' + hashtable.get("foo")); // Value of key "foo": bar
console.log('Value of key "cuatro": ' + hashtable.get("cuatro")); // Value of key "cuatro": 4
console.log("Get keys by using property: " + hashtable.keys); // Get keys by using property: one,two,three,cuatro,foo
hashtable.clear();
console.log( 'Length after clear: ' + hashtable.length ); // Length after clear: 0
console.log("Length after clear: " + hashtable.length); // Length after clear: 0

View File

@@ -1,18 +1,27 @@
const personIsSeller = name => name[name.length - 1] === 'm';
const graph = {};
graph.you = ['alice', 'bob', 'claire'];
graph.bob = ['anuj', 'peggy'];
graph.alice = ['peggy'];
graph.claire = ['thom', 'jonny'];
graph.you = ["alice", "bob", "claire"];
graph.bob = ["anuj", "peggy"];
graph.alice = ["peggy"];
graph.claire = ["thom", "jonny"];
graph.anuj = [];
graph.peggy = [];
graph.thom = [];
graph.jonny = [];
const search = (name) => {
let searchQueue = [];
searchQueue = searchQueue.concat(graph[name]);
/**
* Determine whether a person is a seller
* @param {string} name Friend's name
* @returns {boolean} Result of checking
*/
const personIsSeller = name => name[name.length - 1] === "m";
/**
* Find a mango seller
* @param {string} name Friend's name
* @returns {boolean} Search results
*/
const search = name => {
let searchQueue = [...graph[name]];
// This array is how you keep track of which people you've searched before.
const searched = [];
while (searchQueue.length) {
@@ -31,4 +40,4 @@ const search = (name) => {
return false;
};
search('you'); // thom is a mango seller!
search("you"); // thom is a mango seller!

View File

@@ -1,23 +1,35 @@
const graph = {};
graph.you = ['alice', 'bob', 'claire'];
graph.bob = ['anuj', 'peggy'];
graph.alice = ['peggy'];
graph.claire = ['thom', 'jonny'];
graph.you = ["alice", "bob", "claire"];
graph.bob = ["anuj", "peggy"];
graph.alice = ["peggy"];
graph.claire = ["thom", "jonny"];
graph.anuj = [];
graph.peggy = [];
graph.thom = [];
const isSeller = name => name[name.length - 1] === 'm';
/**
* Determine whether a person is a seller
* @param {string} name Friend's name
* @returns {boolean} Result of checking
*/
const isSeller = name => name[name.length - 1] === "m";
const search = (name, graph) => {
const iter = (waited, visited) => {
if (waited.length === 0) {
return false;
}
/**
* Find a mango seller
* @param {string} name Friend's name
* @param {Object} graph Hash table
* @returns {boolean} Search results
*/
const search = (name, graph = {}) => {
/**
* Recursive function to test people
* @param {Array} waited List of people you need to check
* @param {Set} visited List of checked people
*/
const iter = (waited = [], visited) => {
if (waited.length === 0) return false;
const [current, ...rest] = waited;
if (visited.has(current)) {
return iter(rest, visited);
}
if (visited.has(current)) return iter(rest, visited);
if (isSeller(current)) {
console.log(`${current} is a mango seller!`);
return true;
@@ -29,4 +41,4 @@ const search = (name, graph) => {
return iter(graph[name], new Set());
};
search('you');
search("you", graph);

View File

@@ -1,7 +1,3 @@
function person_is_seller(name) {
return name[name.length - 1] === "m";
}
const graph = {};
graph["you"] = ["alice", "bob", "claire"];
graph["bob"] = ["anuj", "peggy"];
@@ -12,6 +8,20 @@ graph["peggy"] = [];
graph["thom"] = [];
graph["jonny"] = [];
/**
* Determine whether a person is a seller
* @param {string} name Friend's name
* @returns {boolean} Result of checking
*/
function personIsSeller(name) {
return name[name.length - 1] === "m";
}
/**
* Find a mango seller
* @param {string} name Friend's name
* @returns {boolean} Search results
*/
function search(name) {
let search_queue = [];
search_queue = search_queue.concat(graph[name]);
@@ -21,7 +31,7 @@ function search(name) {
let person = search_queue.shift();
// Only search this person if you haven't already searched them
if (searched.indexOf(person) === -1) {
if (person_is_seller(person)) {
if (personIsSeller(person)) {
console.log(person + " is a mango seller!");
return true;
}

View File

@@ -0,0 +1,48 @@
const graph = {};
graph["you"] = ["alice", "bob", "claire"];
graph["bob"] = ["anuj", "peggy"];
graph["alice"] = ["peggy"];
graph["claire"] = ["thom", "jonny"];
graph["anuj"] = [];
graph["peggy"] = [];
graph["thom"] = [];
graph["jonny"] = [];
/**
* Determine whether a person is a seller
* @param {string} name Friend's name
* @returns {boolean} Result of checking
*/
function personIsSeller(name) {
return name[name.length - 1] === "m";
}
/**
* Find a mango seller
* @param {string} name Friend's name
* @param {Object} graph Hash table
* @returns {boolean} Search results
*/
function search(name, graph) {
graph = graph || {};
/**
* Recursive function to check people
* @param {Array} waited List of people you need to check
* @param {Array} visited List of checked people
*/
function inner(waited, visited) {
waited = waited || [];
if (waited.length === 0) return false;
const person = waited[0];
const waitedCloned = waited.slice(1);
if (visited.indexOf(person) !== -1) return inner(waitedCloned, visited);
if (personIsSeller(person)) {
console.log(person + " is a mango seller!");
return true;
}
return inner(waitedCloned.concat(graph[person]), visited.concat(person));
}
return inner(graph[name], []);
}
search("you", graph);