forked from fay-jai/toy-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gitignore and all skeleton toy problems
- Loading branch information
Showing
54 changed files
with
11,402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* write a function that takes a string of text and returns true if | ||
* the parentheses are balanced and false otherwise. | ||
* | ||
* Example: | ||
* balancedParens('('); // false | ||
* balancedParens('()'); // true | ||
* balancedParens(')('); // false | ||
* balancedParens('(())'); // true | ||
* | ||
* Step 2: | ||
* make your solution work for all types of brackets | ||
* | ||
* Example: | ||
* balancedParens('[](){}'); // true | ||
* balancedParens('[({})]'); // true | ||
* balancedParens('[(]{)}'); // false | ||
* | ||
* Step 3: | ||
* ignore non-bracket characters | ||
* balancedParens(' var wow = { yo: thisIsAwesome() }'); // true | ||
* balancedParens(' var hubble = function() { telescopes.awesome();'); // false | ||
* | ||
* | ||
*/ | ||
|
||
var balancedParens = function (input) { | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
given a sorted array, find the index of an element | ||
using a binary search algorithm. | ||
Test for linear vs binary by applying a data set that | ||
exhibits the desired characteristics. | ||
*/ | ||
|
||
/** | ||
* example usage | ||
* var index = binarySearch([1, 2, 3, 4, 5], 3); | ||
* console.log(index); // [2] | ||
**/ | ||
|
||
var binarySearch = function (array, element) { | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*jshint expr:true*/ | ||
|
||
/* | ||
* Bubble sort is the most basic sorting algorithm in all of Computer | ||
* Sciencedom. It works by starting at the first element of an array and | ||
* comparing it to the second element; if the first element is greater than the | ||
* second element, it swaps the two. It then compares the second to the third, | ||
* and the third to the fourth, and so on; in this way, the largest values | ||
* "bubble" to the end of the array. Once it gets to the end of the array, it | ||
* starts over and repeats the process until the array is sorted numerically. | ||
* | ||
* Implement a function that takes an array and sorts it using this technique. | ||
* Don't use JavaScript's built-in sorting function (Array.prototype.sort). | ||
* | ||
* QUERY: What's the time complexity of your algorithm? If you don't already | ||
* know, try to intuit this without consulting the Googles. | ||
* | ||
* Extra credit: Optimization time! During any given pass, if no elements are | ||
* swapped we can assume the list is sorted and can exit the function early. | ||
* After this optimization, what is the time complexity of your algorithm? | ||
* | ||
* Moar credits: Do you need to consider every element every time you iterate | ||
* through the array? Make it happen, boss. Again: Has the time complexity of | ||
* your algorithm changed? | ||
*/ | ||
|
||
/* | ||
* Example usage: | ||
* bubbleSort([2, 1, 3]); // yields [1, 2, 3] | ||
* | ||
*/ | ||
|
||
// Introduce i into the global scope so we can test function efficiency | ||
var i; | ||
|
||
// Feel free to add helper functions if needed. | ||
|
||
var bubbleSort = function(array) { | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Write a function that takes as its input a string and returns an array of | ||
* arrays as shown below sorted in descending order by frequency and then by | ||
* ascending order by character. | ||
* | ||
* | ||
* :: Example :: | ||
* | ||
* characterFrequency('mississippi') === | ||
* [ | ||
* ['i', 4], | ||
* ['s', 4], | ||
* ['p', 2], | ||
* ['m', 1] | ||
* ] | ||
* | ||
* :: Gotcha :: | ||
* | ||
* characterFrequency('miaaiaaippi') === | ||
* [ | ||
* ['a', 4], | ||
* ['i', 4], | ||
* ['p', 2], | ||
* ['m', 1] | ||
* ] | ||
* | ||
* | ||
*/ | ||
|
||
|
||
var characterFrequency = function(string) { | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: | ||
1p piece | ||
2p piece | ||
5p piece | ||
10p piece | ||
20p piece | ||
50p piece | ||
1 euro (100p) | ||
2 euro (200p) | ||
It is possible to make £2 in the following way: | ||
1 * £1 + 1 * 50p + 2 * 20p + 1 * 5p + 1 * 2p + 3 * 1p | ||
How many different ways can £2 be made using any number of coins? | ||
example usage of `makeChange`: | ||
// aka, there's only one way to make 1p. that's with a single 1p piece | ||
makeChange(1) === 1 | ||
// aka, there's only two ways to make 2p. that's with two, 1p pieces or with a single 2p piece | ||
makeChange(2) === 2 | ||
*/ | ||
|
||
var makeChange = function(total){ | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
/** | ||
* implement the function `getClosestCommonAncestor` and `getAncestorPath` | ||
* in the following Tree class | ||
*/ | ||
|
||
/** example usage: | ||
* var grandma = new Tree(); | ||
* var mom = new Tree(); | ||
* grandma.addChild(mom); | ||
* var me = new Tree(); | ||
* mom.addChild(me); | ||
* grandma.getAncestorPath(me); // => [grandma, mom, me] | ||
*/ | ||
|
||
var Tree = function(){ | ||
this.children = []; | ||
}; | ||
|
||
/** | ||
* add an immediate child | ||
*/ | ||
Tree.prototype.addChild = function(child){ | ||
if(!this.isDescendant(child)){ | ||
this.children.push(child); | ||
}else { | ||
throw new Error("That child is already a child of this tree"); | ||
} | ||
return this; | ||
}; | ||
|
||
/** | ||
* return the lowest common ancestor of the two child nodes. | ||
* (assume for these examples that only a women can be the parent of a child) | ||
* more examples: | ||
* 1.) between me and my brother -> my mom | ||
* 2.) between me and my cousin -> my grandma | ||
* 3.) between my grandma and my grandma -> my grandma | ||
* 4.) between me and a potato -> null | ||
*/ | ||
Tree.prototype.getClosestCommonAncestor = function(/*...*/){ | ||
// TODO: implement me! | ||
} | ||
|
||
/** | ||
* should return the ancestral path of a child to this node. | ||
* more examples: | ||
* 1.) greatGrandma.getAncestorPath(me) -> [great grandma, grandma, mom, me] | ||
* 2.) mom.getAncestorPath(me) -> [mom, me] | ||
* 3.) me.getAncestorPath(me) -> [me] | ||
* 4.) grandma.getAncestorPath(H R Giger) -> null | ||
*/ | ||
Tree.prototype.getAncestorPath = function(/*...*/){ | ||
// TODO: implement me! | ||
} | ||
|
||
/** | ||
* check to see if the provided tree is already a child of this | ||
* tree __or any of its sub trees__ | ||
*/ | ||
Tree.prototype.isDescendant = function(child){ | ||
if(this.children.indexOf(child) !== -1){ | ||
// `child` is an immediate child of this tree | ||
return true; | ||
}else{ | ||
for(var i = 0; i < this.children.length; i++){ | ||
if(this.children[i].isDescendant(child)){ | ||
// `child` is descendant of this tree | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
/** | ||
* remove an immediate child | ||
*/ | ||
Tree.prototype.removeChild = function(child){ | ||
var index = this.children.indexOf(child); | ||
if(index !== -1){ | ||
// remove the child | ||
this.children.splice(index,1); | ||
}else{ | ||
throw new Error("That node is not an immediate child of this tree"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Write a function `f(a, b)` which takes two strings as arguments and returns a | ||
* string containing only the unique characters found in both strings, in the | ||
* order that they appeared in `a`. Remember to skip spaces and characters you | ||
* have already encountered! | ||
* | ||
* Example: commonCharacters('acexivou', 'aegihobu') | ||
* Returns: 'aeiou' | ||
* | ||
* Extra credit: Extend your function to handle more than two input strings. | ||
*/ | ||
|
||
|
||
var commonCharacters = function(string1, string2) { | ||
// TODO: Your code here! | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Write a stack using your preferred instantiation pattern. Implement a min function | ||
* that returns the minimum value of all the elements in the stack in constant time. | ||
*/ | ||
|
||
/** | ||
* Stack Class | ||
*/ | ||
var Stack = function() { | ||
|
||
// add an item to the top of the stack | ||
this.push = function(value){ | ||
}; | ||
|
||
// remove an item from the top of the stack | ||
this.pop = function(){ | ||
}; | ||
|
||
// return the number of items in the stack | ||
this.size = function(){ | ||
} | ||
|
||
this.min = function() { | ||
|
||
} | ||
return this; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Write a function that, given two objects, returns whether or not the two | ||
* are deeply equivalent--meaning the structure of the two objects is the | ||
* same, and so is the structure of each of their corresponding descendants. | ||
* | ||
* Examples: | ||
* | ||
* deepEquals({a:1, b: {c:3}},{a:1, b: {c:3}}); // true | ||
* deepEquals({a:1, b: {c:5}},{a:1, b: {c:6}}); // false | ||
* | ||
* don't worry about handling cyclical object structures. | ||
* | ||
*/ | ||
var deepEquals = function(apple, orange){ | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Find the first item that occurs an even number of times in an array. | ||
* Remember to handle multiple even-occurance items and return the first one. | ||
* Return null if there are no even-occurance items. | ||
*/ | ||
|
||
/* | ||
* example usage: | ||
* var onlyEven = evenOccurence([1, 7, 2, 4, 5, 6, 8, 9, 6, 4]); | ||
* console.log(onlyEven); // 4 | ||
*/ | ||
|
||
var evenOccurence = function(arr) { | ||
// Your code here. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Make an eventing system mix-in that adds .trigger() and .on() to any input | ||
* object. | ||
* | ||
* Example usage: | ||
* var obj = mixEvents({ name: 'Alice', age: 30 }); | ||
* obj.on('ageChange', function(){ // On takes an event name and a callback function | ||
* console.log('Age changed'); | ||
* }); | ||
* obj.age++; | ||
* obj.trigger('ageChange'); // This should call our callback! Should log 'age changed'. | ||
* | ||
* Caveats: | ||
* - mixEvents should return the original object it was passed after extending it. | ||
* - If we repeatedly call .on with the same event name, it should | ||
* continue to call the old function as well. That is to say, we can have multiple | ||
* listeners for an event. | ||
* - If `obj.trigger` is called with additional arguments, pass those to the | ||
* listeners. | ||
* - It is not necessary to write a way to remove listeners. | ||
*/ | ||
|
||
var mixEvents = function(obj) { | ||
// TODO: Your code here | ||
return obj; | ||
}; |
Oops, something went wrong.