diff --git a/README.md b/README.md index 783e9c5..2daf9dc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Core Algorithms +# Core Algorithms #teamName premium-partridge #Aisha Ortiz and Thomas Dunn Tests and implementations for algorithms commonly used in job interviews. See the full list in the [algorithms.md](algorithms.md) file. diff --git a/algorithmTest1Week3.html b/algorithmTest1Week3.html new file mode 100644 index 0000000..2d1d662 --- /dev/null +++ b/algorithmTest1Week3.html @@ -0,0 +1,10 @@ + + +
+ + + + + + + diff --git a/algorithms.md b/algorithms.md index 93d5faa..68d86c5 100644 --- a/algorithms.md +++ b/algorithms.md @@ -61,7 +61,7 @@ Return the factorial of a number. factorial(5) // => 120 ``` - +fac #### fibonacci Return an array of Fibonacci numbers to the _nth_ position. diff --git a/core-algorithms b/core-algorithms new file mode 160000 index 0000000..2713bcd --- /dev/null +++ b/core-algorithms @@ -0,0 +1 @@ +Subproject commit 2713bcd06f1c9789caed80e4bb3e7bd15237b493 diff --git a/package.json b/package.json index 71f93c5..35d1917 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,12 @@ "description": "Tests and implementations for algorithms commonly used in job interviews.", "private": false, "version": "0.0.0", + "repository": { + "type": "git", + "url": "git+https://github.com/aishao/core-algorithms" + }, + "author": "Aisha , Thomas", + "license": "MIT", "devDependencies": { "babel-cli": "^6.18.0", "babel-preset-env": "^1.1.4", diff --git a/src/collatzConjecture.js b/src/collatzConjecture.js new file mode 100644 index 0000000..0365c43 --- /dev/null +++ b/src/collatzConjecture.js @@ -0,0 +1,23 @@ +export default function assembleCollatz(num) { + let collatzArray = [num]; + + while ( collatzArray[collatzArray.length-1] !== 1) { + let lastNumber = collatzArray[collatzArray.length-1]; + + if (lastNumber % 2 === 0) { + collatzArray.push(lastNumber / 2) + } else { + collatzArray.push((3 * lastNumber) + 1) + } + } + return collatzArray; +} +console.log(assembleCollatz(7)); + +//initalize function (num) +// create empty arraySequence + // while (the last number of arraySequence is not 1) + // if num is even + // push num / 2 to the array + // else if push 3num + 1 to arraySequence + // return sequence diff --git a/src/factorial.js b/src/factorial.js new file mode 100644 index 0000000..e76df74 --- /dev/null +++ b/src/factorial.js @@ -0,0 +1,8 @@ +export default function factorialize(num) { + let inputValue = num; + let factorial = 1; + for (let x = num; x >= 1; x--) { + factorial *= x; + } + return factorial; +} diff --git a/src/fibonacci.js b/src/fibonacci.js new file mode 100644 index 0000000..03935c0 --- /dev/null +++ b/src/fibonacci.js @@ -0,0 +1,14 @@ + +export default function assembleFibonacci(nth) { + let a = 0, b = 1, f = 1; + let fibonacciArray = [a,b]; + for (let i = 2; i < nth; i++) { + f = a + b; + a = b; + b = f; + fibonacciArray.push(f); + } + return fibonacciArray; +} + +//console.log(assembleFibonacci(10)); diff --git a/src/fizzBuzz.js b/src/fizzBuzz.js new file mode 100644 index 0000000..cbdfa7b --- /dev/null +++ b/src/fizzBuzz.js @@ -0,0 +1,14 @@ +export default let list = new Array(100); + + for (let i = 0; i < 100; i++) { + list[i] = i + 1; // $$c populates the array. +1 is necessary because arrays are 0 index based and you want to store 1 - 100 in it, NOT 0-99.{ + + //list[i] is equal to an index in the array + if (list[i] % 5 === 0 && list[i] % 3 === 0) { // populate multiples of both five && three with "FIZZBUZZ" + list[i] = "FizzBuzz" + } else if (list[i] % 5 === 0) { // populate multiples of five with "BUZZ" + list[i] = "Buzz" + } else if (list[i] % 3 === 0) { // populate multiples of three with "FIZZ" + list[i] = "Fizz" + } + } diff --git a/src/isPalindrome.js b/src/isPalindrome.js new file mode 100644 index 0000000..328ed3c --- /dev/null +++ b/src/isPalindrome.js @@ -0,0 +1,23 @@ + +export default function isPalindrome(str) { + + str = str.replace(/[^0-9a-z]/gi, "").toLowerCase(); + let palindrome = str.split("").reverse().join(""); + if (str === palindrome) { + return true; + } else { + return false; + } +} + +/*console.log( + + isPalindrome('radar') + => true + + isPalindrome('bananas') + => false + + isPalindrome('A man, a plan, a canal: Panama') + => true + isPalindrome("A man, a plan, a canal: Panama")); */ diff --git a/src/makeChange.js b/src/makeChange.js index 59d89b1..9aaf666 100644 --- a/src/makeChange.js +++ b/src/makeChange.js @@ -1,3 +1,21 @@ export default function makeChange({price, amountGiven}) { - // your code here + // your code here +let coins = {quarters: 25, dimes: 10, nickels: 5, pennies: 1 } + + for(let key in coins) { + let numberOfCoins = Math.floor(changeDue / coins[key]) + /*takes the changeDue divides it by coin section of quarters (25) resulting in a + decimal of 1.64, then Math.floor round down to the nearest integer which is + 1 (<= the given number) and assigns it to the variable numberOfCoins. + */ + + changeDue -= coins[keys] * numberOfCoins + + changeDue -= coins[key] * numberOfCoins + + + coins[key] = numberOfCoins + + } + return coins; } diff --git a/src/setUnion.js b/src/setUnion.js new file mode 100644 index 0000000..3718376 --- /dev/null +++ b/src/setUnion.js @@ -0,0 +1,6 @@ +export default function setUnion(myArray) { + + return [...new Set(myArray)]; // .. Es6 version of .unique method + +} +console.log(setUnion()); diff --git a/test/factorial_test.js b/test/factorial_test.js new file mode 100644 index 0000000..69e0fa8 --- /dev/null +++ b/test/factorial_test.js @@ -0,0 +1,16 @@ +import { expect } from 'chai' +import factorialize from '../src/factorial' + +describe('factorial()', function(){ + + it('should be a function', function(){ + expect(factorial).to.be.a('function') + }) + + it('returns the outout of the factorial)', function(){ + expect(factorialize(5)).to.equal(120) + expect(factorialize(12)).to.equal(479001600) + expect(factorialize(8)).to.equal(40320) + + }) +}) diff --git a/test/fizzBuzzTests.js b/test/fizzBuzzTests.js new file mode 100644 index 0000000..e69de29 diff --git a/test/isPalindrome_test.js b/test/isPalindrome_test.js new file mode 100644 index 0000000..a4f30c2 --- /dev/null +++ b/test/isPalindrome_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import isPalindrome from '../src/isPalindrome' + +describe('isPalindrome()', function(){ + + it('should be a function', function(){ + expect(isPalindrome).to.be.a('function') + }) + + it('returns true if the input is a Palindrome', function(){ + expect(isPalindrome('racecar')).to.equal(true) + }) + it('returns true if the input is a Palindrome', function(){ + expect(isPalindrome('trevor')).to.equal(false) + }) + it('returns true if the input is a Palindrome', function(){ + expect(isPalindrome('A man, a plan, a canal: Panama')).to.equal(true) + }) + +}) diff --git a/test/setUnion.js b/test/setUnion.js new file mode 100644 index 0000000..cc0bc8d --- /dev/null +++ b/test/setUnion.js @@ -0,0 +1,18 @@ +import { expect } from 'chai' +import setUnion from '../src/setUnion' + +describe.only('setUnion()', function(){ + + it('should be a function', function(){ + expect(setUnion).to.be.a('function') + }) + + it('returns an array', function(){ + expect(setUnion([1, 2, 3])).to.deep.equal([1, 2, 3]) + }) + + it ('seeks one of a kind in the array', function(){ + expect(setUnion([1, 2, 3, 4, 2, 4, 6, 8,])).to.deep.equal([1, 2, 3, 4, 6, 8]) + }) + +})