Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
ba19352
first commit
sfmajors373 Jul 10, 2017
85f01e0
isUnique, first part works
sfmajors373 Jul 11, 2017
035c3aa
delete unnecessary comments
sfmajors373 Jul 11, 2017
9dcf51a
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 12, 2017
0031f30
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 13, 2017
d776dd4
LargestPrimePalindrome - work
sfmajors373 Jul 14, 2017
70ebcce
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 14, 2017
9f0c853
ievenOccurences
sfmajors373 Jul 16, 2017
f6920e1
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 17, 2017
e938619
callbackPractice: code written, not tested
sfmajors373 Jul 18, 2017
768885a
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 18, 2017
29e292c
forLoopTimeout: fixed
sfmajors373 Jul 18, 2017
7b40ece
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 19, 2017
5c87020
Daily update
sfmajors373 Jul 20, 2017
4b9127b
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 20, 2017
0f90717
Fixed spacing issues
sfmajors373 Jul 20, 2017
8c88393
waterJug solution
sfmajors373 Jul 21, 2017
50622aa
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 21, 2017
9a1ba4c
Array: tests added, not passing
sfmajors373 Jul 23, 2017
cd1cef2
Array: tests pass
sfmajors373 Jul 23, 2017
89ecf7a
Array: comment maintenance
sfmajors373 Jul 23, 2017
f67ad92
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 24, 2017
2817968
Array: add return statement to pop
sfmajors373 Jul 24, 2017
02e7850
stringCompression: beginning but not passing tests yet
sfmajors373 Jul 25, 2017
5f1308b
string compression: tests pass
sfmajors373 Jul 25, 2017
579abbd
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 25, 2017
9040d7b
operators: tests bench created
sfmajors373 Jul 25, 2017
a8ab1a5
operators: tests pass
sfmajors373 Jul 25, 2017
77a2364
operators: adjust spacing
sfmajors373 Jul 25, 2017
b1afd25
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 26, 2017
1098af0
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 27, 2017
877b243
QueueStack: Queue remove not working correctly
sfmajors373 Jul 28, 2017
67299f3
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 28, 2017
cd87257
Adjustments to queueStack and all of bubbleSort
sfmajors373 Jul 31, 2017
72b14a8
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Jul 31, 2017
900e743
allAnagrams: works, could be improved for efficiency
sfmajors373 Aug 1, 2017
0fd3dcc
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 1, 2017
ee8b129
instertionSort: works
sfmajors373 Aug 1, 2017
d4f7fb9
Added Alt solutions for AllAnagrams
sfmajors373 Aug 1, 2017
1616844
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 2, 2017
fdcb4dc
RotateImage: works, not extra credit though
sfmajors373 Aug 3, 2017
3c1996c
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 3, 2017
f22acfb
logicGates passes tests
sfmajors373 Aug 3, 2017
c37b57a
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 4, 2017
6956712
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 7, 2017
9179398
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 9, 2017
53041f6
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 9, 2017
283f41c
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 10, 2017
5caa890
rotatedArray: tests do not pass
sfmajors373 Aug 10, 2017
a537df1
rotatedArray works
sfmajors373 Aug 11, 2017
fe74674
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 11, 2017
d259ea5
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 14, 2017
ea04e4a
Updating backlog of codechallenges
sfmajors373 Aug 14, 2017
7141d6a
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 15, 2017
7e6b0fd
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 16, 2017
277cb71
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 17, 2017
567da0a
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 18, 2017
6b95236
Common characters
sfmajors373 Aug 18, 2017
45dd999
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 23, 2017
108db8e
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 24, 2017
215403d
Update
sfmajors373 Aug 24, 2017
e932397
Merge branch 'master' of https://github.com/ryanhca/CS1-Code-Challenges
sfmajors373 Aug 25, 2017
bbb3dba
Update
sfmajors373 Aug 27, 2017
0560515
Daily update
sfmajors373 Aug 29, 2017
9c902d4
Adjustment to daily challenge
sfmajors373 Aug 29, 2017
f4a8de3
Daily update
sfmajors373 Aug 30, 2017
63804bf
Daily update
sfmajors373 Aug 31, 2017
5b774d5
Daily update
sfmajors373 Sep 1, 2017
9d0dbc7
Daily update
sfmajors373 Sep 6, 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
79 changes: 79 additions & 0 deletions allAnagrams/allAnagramAlternatives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Recursive:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sarah, this is fantastic. allAnagrams is such a plaguing algorithm to complete. I hate it. haha. You've solved it recursively and with a depth first search solution. I'm very impressed!

const allAnagrams = (str, start = '') => {
if (str.length === 1) return [start + str];
const anagrams = [];

for (let i = 0; i < str.length; i++) {
const result = allAnagrams(str.substr(0, i) + str.substr(i + 1), str[i]);
for (let j = 0; j < result.length; j++) {
anagrams.push(start + result[j]);
}
}
return anagrams;
};

// Depth First Search, turning string into tree:
class Anagram {
constructor(string, letter = null) {
this.string = string;
this.letter = letter;
this.children = [];
this.answer = [];
this.divide();
this.produce();
}

divide() {
const arr = this.string.split('');
arr.forEach(l => {
const rest = arr.filter(x => x !== l).join('');
const anagram = new Anagram(rest, l);
this.children.push(anagram);
});
}

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);
}
}

function allAnagrams(s) {
return (new Anagram(s)).answer;
}

const anagrams = allAnagrams('abcdefghi');
console.log(anagrams);

// JS implementation of Heap's Permutation Algorithm, also using generator:
var swap = function (array, pos1, pos2) {
var temp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = temp;
};

var heapsPermute = function (array, output, n) {
n = n || array.length; // set n default to array.length
if (n === 1) {
output(array);
} else {
for (var i = 1; i <= n; i += 1) {
heapsPermute(array, output, n - 1);
if (n % 2) {
var j = 1;
} else {
var j = i;
}
swap(array, j - 1, n - 1); // -1 to account for javascript zero-indexing
}
}
};
https://gist.github.com/dsernst/2570de0dc7d44a8cbbd0
21 changes: 20 additions & 1 deletion allAnagrams/allAnagrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,23 @@
* example usage:
* const anagrams = allAnagrams('abc');
* console.log(anagrams); // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]
*/
*/

function anagrams(string) {
const results = [];
if (string.length < 2) {
return string;
}
for (let i = 0; i < string.length; i++) {
let firstChar = string[i];
let charsLeft = string.substring(0, i) + string.substring(i + 1);
let innerPermutations = anagrams(charsLeft);
for (let j = 0; j < innerPermutations.length; j++) {
results.push(firstChar + innerPermutations[j]);
}
}
return results;
}


console.log(anagrams('abc'));
15 changes: 15 additions & 0 deletions armstrongNumbers/armstrongNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//An Armstrong number is an n-digit number that is equal to the sum of the n'th powers of its digits. Determine if the input numbers are Armstrong numbers. Return either true or false .

// For example with the number 153 there are 3 digits so you would add together the cubed values of all the digits like this: 1^3 + 5^3 + 3^3 === 153

function isArmstrongNumber(n){
const arr = n.toString().split('');
let acc = 0;
for (let i = 0; i < arr.length; i++) {
acc = Math.pow(Number(arr[i]), arr.length) + acc;
}
if (acc === n) {
return true;
}
return false;
}
63 changes: 63 additions & 0 deletions array/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,66 @@
* 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?
*/
class Array {
constructor(item) {
this[0] = item;
this.length = 1;
}

push(item){
this[this.length] = item;
this.length = this.length + 1;
}

pop(){
const toReturn = this.length-1;
delete this[(this.length-1)];
this.length = this.length-1;
return toReturn;
}

get(index){
return this[index];
}

delete(index){
// make sure to shift the items after deletion
for (let n = index; n + 1 < this.length; n++) {
this[n] = this[n + 1];
}
delete this[this.length - 1];
this.length = this.length - 1;
}

}

//**************************************************************************************************
// Check if constructor works
// const arr = new Array('first');
// console.log(arr);
// Check if push works (builds on constructor working test)
// arr.push('second');
// arr.push('third');
// arr.push('fourth');
// console.log(arr);
// ***********************Leave this arr for remaining tests
// check if length works
// const arr = {
// 0: 'first',
// 1: 'second',
// 2: 'third',
// 3: 'fourth',
// }
// *****************************************
// console.log(arr.length); // 4
// check if push works
// arr.push('fifth');
// console.log(arr);
// check if pop works
// arr.pop();
// console.log(arr);
// test get(index)
// console.log(arr.get(1)); // third
// test delete
// arr.delete(1);
// console.log(arr);
53 changes: 52 additions & 1 deletion binarySearch/binarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,58 @@
* * const index = binarySearch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 2);
* * console.log(index); // 1
**/
// const binarySearch = (nums, target) => {
// function bSearch(nums, target, low, high) {
// if (high === low) {
// return low;
// }
// let mid = Math.ceil((low + high)/2);
// if (nums[mid] === target) {
// return mid;
// } else if (nums[mid] < target) {
// if (low === mid) {
// return false;
// else {
// return bSearch(nums, target, mid + 1, high);
// }
// } if (nums[mid] > target) {
// if (high = mid) {
// return false;
// else {
// return bSearch(nums, target, low, mid - 1)
// }
// }
// }
// if (nums.length === 0) {
// return false;
// } else {
// return bSearch(nums, target, 0, nums.length - 1);
// }
// };

const binarySearch = (nums, target) => {

function bSearch(nums, target, low, high) {
if (target === nums[low]) {
return low;
}
if (nums[high] === target){
return high;
}
let middle = Math.floor((low + high) / 2);
let middleElement = nums[middle];
if (target === middleElement) {
return middle;
} else if (target > middleElement) {
return bSearch(nums, target, middle, high-1);
} else if (target < middleElement) {
return bSearch(nums, target, low+1, middle);
}
}
return bSearch(nums, target, 0, nums.length-1);
};


// const index = binarySearch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 2);
// console.log(index); // 1

export { binarySearch };
23 changes: 23 additions & 0 deletions blueSquares/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<header>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="scripts.js"></script>
</header>
<body>
<div class="container">
<a class="box" href="#">1</a>
<a class="box" href="#">2</a>
<a class="box" href="#">3</a>
<a class="box" href="#">4</a>
<a class="box" href="#">5</a>
<a class="box" href="#">6</a>
<a class="box" href="#">7</a>
<a class="box" href="#">8</a>
<a class="box" href="#">9</a>
<a class="box" href="#">10</a>
<a class="box" href="#">11</a>
<a class="box" href="#">12</a>
</div>
</body>
</html>
Empty file added blueSquares/scripts.js
Empty file.
26 changes: 26 additions & 0 deletions blueSquares/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
}

.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
}

.box {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bolder;
font-size: 3em;
text-align: center;
}

a:hover {
background-color: blue;
}
2 changes: 2 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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.

Take the full 5, use it to fill the 3-quart jug. Empty the 3-quart jug, and place the remaining 2-quarts from the 5-quart jug into the small jug. Refill the large jug. Pour enough out of the large jug to fill the small jug. This leaves 4 quarts in the larger jug.
46 changes: 45 additions & 1 deletion breadthFirstSearch/breadthFirstSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,48 @@
* a: 2,
* };
* breadthFirstSearch(tree, 2);// will return true before it recursively searches `z`
*/
*/

// Use a helper queue
class Queue {
constructor(){
this.items = [];
this.size = 0;
}
enqueue(item){
this.items.push(item);
this.size++;
}
dequeue(){
return this.items.shift();
}
isEmpty(){
if (this.size === 0) {
return true;
} else {
return false;
}
}
}
const breadthFirst = (tree, value) => {
const queue = new Queue();
// Enqueue first vertex
// Dequeue first vertex, enqueue its children
// Continue dequeueing a vertex and enqueueing its children
}

// Ben's code:
// const BFS = (tree, searchTerm) => {
// const queue = Object.values(tree);
// while(queue.length > 0) {
// const value = queue.shift();
// if (value === searchTerm) {
// return true;
// }
// if (typeOf value === 'object' && value !== null && !Array.isArray(value)) {
// queue = queue.concat(Object.values(value)); // push would just nest array instead of making it a big array
// }
// }
// return false;
// };
// O(n)
18 changes: 17 additions & 1 deletion bubbleSort/bubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,21 @@
*/

const bubbleSort = (arr) => {
//code here
let sorted = false;
let temp = null;
while(!sorted) {
sorted = true;
for (let i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i + 1]) {
swap = false;
temp = arr[i];
arr[i] = arr[i +1];
arr[i + 1] = temp;
}
}
}
return arr;
};


console.log(bubbleSort([2, 1, 3]));
Loading