Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
bfdc783
longestString initial commit
wlharvey4 Jul 11, 2017
e85d1f3
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 11, 2017
a458dbf
isUnique.js initial commit
wlharvey4 Jul 11, 2017
c262b27
callbackPractice.js completed
wlharvey4 Jul 18, 2017
9d4eae9
Deleted prior repo; cloned from backup
wlharvey4 Jul 18, 2017
90e9ad5
Deleted prior repo; cloned from backup
wlharvey4 Jul 18, 2017
542d86b
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Jul 18, 2017
de19d9d
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 18, 2017
7c435b4
forLoopTimeout.js completed
wlharvey4 Jul 18, 2017
25ffc51
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 19, 2017
a339ff4
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Jul 19, 2017
27872ab
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 20, 2017
2de7d15
binarySearch.js completed
wlharvey4 Jul 20, 2017
75b8bd4
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Jul 20, 2017
eb0c4d8
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 21, 2017
e9936f9
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 21, 2017
9379fdc
waterJugs solution
wlharvey4 Jul 21, 2017
5a4b704
working on array.js
wlharvey4 Jul 22, 2017
151ad63
finished array.js
wlharvey4 Jul 25, 2017
59f6e20
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 25, 2017
63bd7eb
stringCompression.js completed
wlharvey4 Jul 26, 2017
a9381e9
operators.js completed
wlharvey4 Jul 26, 2017
c158f68
operators.js completed
wlharvey4 Jul 26, 2017
39cce91
operations.js completed
wlharvey4 Jul 26, 2017
a6fdf1b
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 27, 2017
b3b6713
operators.js: correct versions using recursion on each
wlharvey4 Jul 27, 2017
c2b7d76
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 28, 2017
74aeea7
finished queueStack.js
wlharvey4 Jul 28, 2017
cb71114
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Jul 28, 2017
fec7fb8
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 29, 2017
1a581ee
bubbleSort.js: completed
wlharvey4 Jul 29, 2017
47067df
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Jul 29, 2017
8c5bd9e
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Jul 31, 2017
d9d71f7
allAnagrams.js starting over
wlharvey4 Aug 1, 2017
d2dd4c8
allAnagrams.js completed
wlharvey4 Aug 1, 2017
1b8d3a4
allAnagrams.js completed
wlharvey4 Aug 1, 2017
c7adbf1
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Aug 1, 2017
cca20b8
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 1, 2017
c451be6
insertionSort finished
wlharvey4 Aug 1, 2017
bb28b1e
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 2, 2017
e3fe31e
rotateImage.js finished
wlharvey4 Aug 2, 2017
460f137
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 3, 2017
e05b921
logicGates completed
wlharvey4 Aug 3, 2017
bdc904e
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Aug 3, 2017
abf642d
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 4, 2017
61063a2
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Aug 4, 2017
ca4c561
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 7, 2017
1137139
rockPaperScissors completed
wlharvey4 Aug 7, 2017
0be2655
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 9, 2017
41e3591
quickSort.js finished
wlharvey4 Aug 9, 2017
962f494
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 9, 2017
032b4b5
finished selectionSort
wlharvey4 Aug 10, 2017
401d822
finished selectionSort
wlharvey4 Aug 10, 2017
2d96d0f
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Aug 10, 2017
1c108ea
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Aug 10, 2017
d62d769
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Aug 10, 2017
c6be506
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 10, 2017
ec6db47
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 11, 2017
3b24273
finished rotatedArray
wlharvey4 Aug 11, 2017
30c2aca
rotatedArray update
wlharvey4 Aug 14, 2017
b03fe28
rotatedArray merged
wlharvey4 Aug 14, 2017
77e2b24
linkedListCycle completed
wlharvey4 Aug 14, 2017
098af52
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 15, 2017
5f49cbb
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 20, 2017
d801cd5
stackOfPlates completed
wlharvey4 Aug 20, 2017
5f1e25e
commonCharacters working
wlharvey4 Aug 20, 2017
2b5245f
robotPaths works
wlharvey4 Aug 20, 2017
c7bce03
lruCache working
wlharvey4 Aug 21, 2017
cca4fc5
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 22, 2017
a3013e6
fizzBuzz tests working
wlharvey4 Aug 22, 2017
7f3d2e7
added node_modules to .gitignore
wlharvey4 Aug 23, 2017
107694d
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 23, 2017
a650ca9
longestRun completed
wlharvey4 Aug 23, 2017
2075085
breadthFirstSearch
wlharvey4 Aug 24, 2017
b92c5b0
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
wlharvey4 Aug 24, 2017
cdcbd2f
vowelCount
wlharvey4 Aug 24, 2017
1c5e544
set of completed CS Code Challenges
wlharvey4 Sep 12, 2017
1d64b15
telephoneWords working
wlharvey4 Sep 13, 2017
3509e6b
twinPrime
wlharvey4 Sep 13, 2017
d4ebd9e
routeBetweenNodes
wlharvey4 Sep 13, 2017
10864f8
romanNumeralEncoder, stackMaching
wlharvey4 Sep 25, 2017
65f217a
checkString.js
wlharvey4 Sep 26, 2017
bcecd3f
Added states to correctly evaluate edge case with duplicate characters
wlharvey4 Sep 27, 2017
d0c0dd7
multiple comments removed; explanation added
wlharvey4 Sep 27, 2017
33e1217
sumDigPower.js
wlharvey4 Sep 27, 2017
f68db2a
primeReduction.js
wlharvey4 Sep 28, 2017
7c40540
primeReduction.js v1
wlharvey4 Sep 28, 2017
cce9f0d
fullAdder.js
wlharvey4 Sep 29, 2017
bd8bee4
working on adders.js, deepEquals
wlharvey4 Sep 30, 2017
24bf406
made sure deepEquals.js equaled adder.js
wlharvey4 Sep 30, 2017
4976225
added package-lock.json
wlharvey4 Sep 30, 2017
5a97832
findOverlap.js
wlharvey4 Oct 3, 2017
63099a9
mergesort.js
wlharvey4 Oct 5, 2017
258ebe1
Merge branch 'master' of https://github.com/wlharvey4/CS1-Code-Challe…
wlharvey4 Oct 5, 2017
16b1832
nthFib.c
wlharvey4 Oct 11, 2017
fcbdb5c
totalAveragePercent.c
wlharvey4 Oct 11, 2017
9d085b1
rand7toRand5.js
wlharvey4 Oct 11, 2017
5f28475
linkedList.c
wlharvey4 Oct 11, 2017
83c56f5
Initial commit
wlharvey4 Oct 13, 2017
371a265
queueUsingStack.c completed
wlharvey4 Oct 17, 2017
1c13372
binarySearchTree.c completed
wlharvey4 Oct 17, 2017
27b4ade
quicksort.c
wlharvey4 Oct 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
node_modules
package-lock.json
124 changes: 124 additions & 0 deletions adders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

function NAND(x, y) {
return (!x || !y);
}

const NAND_truth = [
[false,false,true],
[false,true,true],
[true,false,true],
[true,true,false]
];

function XOR(x, y) {
const n = NAND(x, y)
return NAND(NAND(x,n), NAND(n, y))
}

const XOR_truth = [
[false,false,false],
[false,true,true],
[true,false,true],
[true,true,false]
];

function AND(x,y) {
const n = NAND(x, y);
return NAND(n, n);
}

const AND_truth = [
[false,false,false],
[false,true,false],
[true,false,false],
[true,true,true],
]

function OR(x, y) {
const n1 = NAND(x, x);
const n2 = NAND(y, y);
return NAND(n1, n2);
}

const OR_truth = [
[false,false,false],
[false,true,true],
[true,false,true],
[true,true,true]
];

function NOT(x) {
return !x;
}

const NOT_truth = [
[false,true],
[true,false]
];

tester(NAND, NAND_truth);
tester(XOR, XOR_truth);
tester(AND, AND_truth);
tester(OR, OR_truth);
tester(NOT, NOT_truth);

function halfAdder(a, b) {
return [AND(a[0], b[0]), XOR(a[0], b[0])];
}

halfAdder_truth = [
[[false],[false],[false,false]],
[[false],[true],[false,true]],
[[true],[false],[false,true]],
[[true],[true],[true,false]]
]

tester(halfAdder, halfAdder_truth);

function fullAdder(c, a, b) {
const ha1 = halfAdder(a[0], b[0]);
if (b[0] === true) process.exit();
const ha2 = halfAdder(c[0], ha1[1]);
const cOut = OR(ha1[0], ha2[0]);

return [cOut, ha2[1]];
}

const fullAdder_truth = [
[[false],[false],[false],[false,false]],
[[false],[false],[true],[false,true]],
[[false],[true],[false],[false,true]],
[[false],[true],[true],[true,false]],
[[true],[false],[false],[false,true]],
[[true],[false],[true],[true,false]],
[[true],[true],[false],[true,false]],
[[true],[true],[true],[true,true]]
];

tester(fullAdder, fullAdder_truth);

function deepEquals (a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
return a.every((e,i) => deepEquals(e, b[i]))
}
else return a === b;
}

function tester(fn, truth) {
console.log(`testing ${fn}`);
truth.forEach(e => {
const args = e.slice(0, e.length - 1);
const exp = e[e.length - 1];
const ans = fn.apply(null, args);
if (deepEquals(ans, exp))
return
else {
console.log(`args: ${args}`);
console.log(`expected: ${exp}`);
console.log(`got: ${ans}`);
console.log();
}
});
console.log();
}

76 changes: 72 additions & 4 deletions allAnagrams/allAnagrams.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Given a single input string, write a function that produces all possible anagrams
* of a string and outputs them as an array. At first, don't worry about
* repeated strings. What time complexity is your solution?
* Given a single input string, write a function that produces all
* possible anagrams of a string and outputs them as an array. At
* first, don't worry about repeated strings. What time complexity is
* your solution?
*
* Extra credit: Deduplicate your return array.
*/
Expand All @@ -10,4 +11,71 @@
* example usage:
* const anagrams = allAnagrams('abc');
* console.log(anagrams); // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]
*/
*/

/** This algorithm utilizes a recursive tree structure to avoid any
* duplicate strings; the constructor creates a new object, and then
* recursively solves the anagram by dividing (using 'divide()') the
* string into smaller and smaller pieces, storing each single
* character in a node with the remaining string, then recursively
* doing the same for the remaining string. After it has finished
* splitting the string into individual nodes, it then produces
* (using 'produce()') all of the anagrams by walking each branch of
* the tree and collecting individual letters; when it has reached a
* tip of a branch, it stores the anagram in the answer property of
* the root (an array of answers) to be returned at the end.
*/

class Anagram {
constructor(string, letter = null) {
this.string = string;
this.letter = letter;
this.children = [];
this.answer = [];
this.divide();
this.produce();
}

// divide() recursively splits the string into a tree of objects
// containing a letter and the remaining string
divide() {
// create a new tree branch for each character in the string, with
// the remaing string, and then create children from the remaining
// string's characters, etc., producing a big tree.
const arr = this.string.split('');
arr.forEach(l => {
const rest = arr.filter(x => x !== l).join('');
// create a new node with an individual letter and the remaining
// string; when Anagram's constructor is called, it recursively
// breaks the string into smaller and smaller pieces, and then
// produces an answer, which is pushed onto the answer property.
const anagram = new Anagram(rest, l);
this.children.push(anagram);
});
}

// recursively runs through all branches of the tree picking out
// single letters and catenating them together to produce anagrams
produce(a = this, b = null) {
if (a.letter) {
b = b ? b.concat(a.letter) : a.letter;
}
if (a.children.length) {
a.children.forEach(aa => {
return this.produce(aa, b ? b : null)
});
return this.answer;
}
this.answer.push(b);
}
}

// acts as main(), returning the answers in an array
function allAnagrams(s) {
return (new Anagram(s)).answer;
}

const anagrams = allAnagrams('abc');
console.log(anagrams);
const abcd = allAnagrams('abcd');
console.log(abcd);
68 changes: 68 additions & 0 deletions array/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,71 @@
* How do these operations compare to that of a linked list?
* How does the time complexity of insertion and deletion compare to that of a linked list?
*/

/*
* I think property accesses in an object are based upon a hash, and so would be
* constant time accesses. Push and pop would be constant time accesses in a linked
* list. However, searching for an item in a linked list is linear, whereas it would
* be constant time in an object. Deleting an item in a linked list is a constant time
* operation, whereas it would be a linear operation in an object. Tradeoffs.
*/

class Array {
constructor(value) {
this.length = 0;
this.arrayObj = {};
}

lastIndex() {
return this.length - 1;
}

nextIndex() {
this.length++;
return this.lastIndex();
}

reduceIndex() {
this.length--;
return this.length;
}

push(value) {
this.arrayObj[this.nextIndex()] = value;
}

pop() {
const value = this.arrayObj[this.lastIndex()];
delete this.arrayObj[this.reduceIndex()];
return value;
}

get(index) {
return this.arrayObj[index]
}

delete(index) {
let value;
if ((value = this.arrayObj[index])) {
for (let i = index; i < this.length - 1; i++) {
this.arrayObj[i] = this.arrayObj[i + 1];
}
this.pop();
}
return value;
}
}

const myArray = new Array();
myArray.push('first');
myArray.push('second');
myArray.push('third');
myArray.push('fourth');
console.log(myArray);
console.log(myArray.pop());
console.log(myArray);
console.log(myArray.get(2));
console.log(myArray.get(3));
console.log(myArray.get(0));
console.log(myArray.delete(1));
console.log(myArray);
32 changes: 32 additions & 0 deletions binarySearch/binarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@
* * console.log(index); // 1
**/

// binarySearch returns index of target number or -1 on failure
const binarySearch = (nums, target) => {

const min = 0;
const max = nums.length;
const index = Math.floor((max - min) / 2);

// next index downward
const nextDown = (i) => {
if (i === min) return -1;
return Math.floor((i - min) / 2);
};

// next index upward
const nextUp = (i) => {
if (i === (max - 1)) return -1;
return i + Math.floor((max - i) / 2);
}

const bs = (i) => {
if (i === -1) return -1;
if (nums[i] === target) return i;
if (nums[i] > target) return bs(nextDown(i));
return bs(nextUp(i));
};

return bs(index);
};

const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(binarySearch(nums, 2));
console.log(binarySearch(nums, 1));
console.log(binarySearch(nums, 10));
console.log(binarySearch(nums, 0));
console.log(binarySearch(nums, 11));
7 changes: 7 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups).
How would you come up with exactly four quarts of water?
Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible.

Step 1: Fill the five-quart jug.
Step 2: Using the five-quart jug, fill the three-quart jug. The five-quart jug will have two quarts remaining.
Step 3: Empty the three-quart jug, then pour the two quarts into the three-quart jug.
Step 4: Fill the five-quart jug, then fill the three quart jug using the five-quart jug, removing one quart from the five-quart jug.

Solution: The five-quart jug will have four quarts remaining.
33 changes: 32 additions & 1 deletion breadthFirstSearch/breadthFirstSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,35 @@
* a: 2,
* };
* breadthFirstSearch(tree, 2);// will return true before it recursively searches `z`
*/
*/

const breadthFirstSearch = (tree, val) => {

let Q = (Object.entries(tree));

while (Q.length) {
const C = Q.shift();
console.log(C, "-", Q);
if (C[1] === val) return true;
if (typeof C[1] === 'object' && C[1] !== null) {
const O = Object.entries(C[1]);
O.forEach(e => Q.push(e));
}
}
return false;
}

const tree = {
x: 1,
y: 1,
n: null,
z: {
x: 1,
y: 1,
z: 1,
},
a: 2,
};

console.log(breadthFirstSearch(tree, 2));
console.log(breadthFirstSearch(tree, 3));
Loading