Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
61636fc
Update README and add CONTRACT file
SomayaB Apr 24, 2017
7f2ed9b
Complete makeChange algorithm w tests
SomayaB Apr 24, 2017
6f4b5d8
Add makeChange test for invalid inputs
SomayaB Apr 25, 2017
2306267
Complete fizzBuzz algorithm w tests
SomayaB Apr 25, 2017
c8fdd34
WIP isPalindrome
SomayaB Apr 25, 2017
b445096
Complete isPalindrome algorithm w tests
SomayaB Apr 25, 2017
3a8074a
Clean up code
SomayaB Apr 25, 2017
8ce1370
Merge pull request #1 from SomayaB/classic
SomayaB Apr 25, 2017
82dc880
Complete factorial algorithm w tests
SomayaB Apr 25, 2017
25d0906
Complete fibonacci algorithm w tests
SomayaB Apr 25, 2017
2e65747
Complete collatzConjecture algorithm w tests
SomayaB Apr 25, 2017
e07820e
Clean code
SomayaB Apr 25, 2017
d1c8849
Merge pull request #2 from SomayaB/numeric
SomayaB Apr 25, 2017
cecde9c
Complete setUnion algorithm w tests
SomayaB Apr 25, 2017
80095c9
Complete setIntersection w tests
SomayaB Apr 25, 2017
d9697a3
Complete setComplement algorithm w tests
SomayaB Apr 26, 2017
41d8ce9
Complete setSymmetricDifference algorithm w tests
SomayaB Apr 26, 2017
e278fb0
Merge pull request #3 from SomayaB/set-operations
SomayaB Apr 26, 2017
8c3b352
Add ESLint and clean code
SomayaB Apr 27, 2017
5002485
Fix linting errors
SomayaB Apr 27, 2017
c8b7754
Add setup instructions in README file
SomayaB Apr 27, 2017
6267dad
Fix setIntersection to account for edge case
SomayaB Apr 28, 2017
6163461
Refactor to ES6
SomayaB Apr 28, 2017
d222cfd
Merge pull request #4 from SomayaB/setup
SomayaB Apr 28, 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: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["env"]
}
}
29 changes: 29 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"env": {
"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}
69 changes: 69 additions & 0 deletions CONTRACT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#158 Core Algorithms [Classic, Numeric, and Set]
#filthy-gerbil
Somaya Bounouar
http://jsdev.learnersguild.org/goals/158-Core_Algorithms-Classic_Numeric_and_Set.html


# Core Algorithms [Classic, Numeric, and Set]

## Challenge Rating

This goal will likely be within your ZPD if you...

- Can write programs and tests using Node.js and npm
- Can use problem decomposition to split difficult problems into smaller parts
- Have used pseudocode as an intermediary step in writing a program
- Are interested in common computer science algorithms
- Are interested in critical thinking and problem solving

## Description

This is a sub-goal of [Core Algorithms][core-algos]. Reference that goal for more description, context, and resources.

Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md#classic), [Numeric](https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md#numeric), and [Set Operations](https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md#set-operations) algorithms, not the Sorting/Searching or Graphing/Geometry. That's a [separate goal][core-algos-adv]. :)

## Specifications

- [x] Artifact produced is a fork of the [core-algorithms][core-algorithms] repo.
- [x] Can run all tests with `npm test`.
- [x] `makeChange()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `makeChange()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] `fizzBuzz()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `fizzBuzz()` exist.
- [x] `isPalindrome()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `isPalindrome()` exist with at least 2 unit tests using valid inputs.
- [x] `factorial()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `factorial()` exist with at least 2 unit tests using valid inputs.
- [x] `fibonacci()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `fibonacci()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] `collatzConjecture()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `collatzConjecture()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [x] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [x] Repository includes a README file with basic installation and setup instructions.
- [x] All dependencies are properly declared in `package.json`.
- [x] All major features are added via pull requests with a clear description and concise commit messages.
- [x] Code uses a linter and there are no linting errors.
- [x] Variables, functions, files, etc. have appropriate and meaningful names.
- [x] Functions are small and serve a single purpose.
- [x] The artifact produced is properly licensed, preferably with the [MIT license][mit-license].

### Stretch

Pick a _different_ programming language from JavaScript (e.g. Ruby, Swift, Python, C, Java...) and write tests & implementations for each.

- [ ] Can run all non-JavaScript tests with a single command.
- [ ] For each algorithm identified above, there exists:
- [ ] a test file with multiple unit tests for each algorithm _in a language other than JavaScript_.
- [ ] an implementation file with a correct implementation of the algorithm _in a language other than JavaScript_.


[core-algos]: {{ site.url }}{% link _goals/123-Core_Algorithms.md %}
[core-algos-adv]: {{ site.url }}{% link _goals/159-Core_Algorithms-Sorting_Searching_Graphing_and_Geometry.md %}
[algorithms-list]: https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Core Algorithms
#158 Core Algorithms [Classic, Numeric, and Set]
#filthy-gerbil
Somaya Bounouar
http://jsdev.learnersguild.org/goals/158-Core_Algorithms-Classic_Numeric_and_Set.html


Tests and implementations for algorithms commonly used in job interviews. See the full list in the [algorithms.md](algorithms.md) file.

Base repository for the [Core Algorithms](http://jsdev.learnersguild.org/goals/123) goal.

## Installation and Setup

_Fill this out_
Use the "npm i" command to install the dependencies.

## Usage and Examples
Use the "npm run test" command to run the tests in the terminal.

_...and this_
Use the "npm install --save-dev eslint" command to install the linter ESlint in the JSON file.
Use the "eslint --init" command to configure ESlint.
Use the "eslint [filename or folder name]" command to lint through your files.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"babel-preset-env": "^1.1.4",
"babel-register": "^6.18.0",
"chai": "~1.8.0",
"eslint": "^3.19.0",
"mocha": "2.0.1"
},
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions src/collatzConjecture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// collatzConjecture
//
// Return the Collatz sequence for a given number.
//
// The Collatz sequence for any positive integer n is defined as follows:
//
// If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process until you reach 1.
// collatzConjecture(1)
// // => [1]
//
// collatzConjecture(7)
// // => [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]


let collatzConjecture = num => {
if (!Number.isInteger(num) || num < 0) {
throw new Error('Invalid input format. Expected a positive integer')
}
let collatzArr = [num]
let result = num

while (result > 1) {
if (result % 2 === 0) {
result = result / 2
collatzArr.push(result)
} else {
result = (result * 3) + 1
collatzArr.push(result)
}
}
return collatzArr
}

module.exports = { collatzConjecture }
26 changes: 26 additions & 0 deletions src/factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// factorial
//
// Return the factorial of a number.
//
// factorial(5)
// // => 120

let factorial = num => {
let accumulator = num
for (let i = num; i > 1; i--) {
accumulator = accumulator * (i - 1)
}
return accumulator
}

module.exports = { factorial }

//Other way:
// var factorialElements = [num]
// for (var i = num; i > 1; i--) {
// factorialElements.push(i - 1)
// }
// return factorialElements.reduce(function(a,b){
// return a*b
// }, 1)
// }
26 changes: 26 additions & 0 deletions src/fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// fibonacci
//
// Return an array of Fibonacci numbers to the nth position.
//
// fibonacci(10)
// // => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

let fibonacci = num => {
if (typeof num !== 'number') {
throw new Error('Invalid input format. Expected a number')
}
let first = 0
let second = 1
let result
let fibonacciArr = [0]

for (let i = 1; i < num; i++) {
result = first + second
first = second
second = result
fibonacciArr.push(first)
}
return fibonacciArr
}

module.exports = { fibonacci }
28 changes: 28 additions & 0 deletions src/fizzBuzz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// fizzBuzz
//
// Return an array of numbers from 1 to 100.
//
// For multiples of three, use the string Fizz instead of the number and for multiples of five replace with Buzz.
//
// For numbers which are multiples of both three and five replace with FizzBuzz.
//
// fizzBuzz()
// // => [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', ...]

let fizzBuzz = () => {
let fizzBuzzArray = []
for (let i = 1; i <= 100; i++){
if (i % 3 == 0 && i % 5 == 0) {
fizzBuzzArray.push('FizzBuzz')
} else if (i % 3 == 0) {
fizzBuzzArray.push('Fizz')
} else if (i % 5 == 0) {
fizzBuzzArray.push('Buzz')
} else {
fizzBuzzArray.push(i)
}
}
return fizzBuzzArray
}

module.exports = { fizzBuzz }
38 changes: 38 additions & 0 deletions src/isPalindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// isPalindrome
//
// Determine if a string is a palindrome. Return true or false.
//
// Ignore punctuation, spacing, and case sensitivity.
//
// isPalindrome('radar')
// // => true
//
// isPalindrome('bananas')
// // => false
//
// isPalindrome('A man, a plan, a canal: Panama')
// // => true

let isPalindrome = string => {
let strToLowerCase = string.toLowerCase()
let cleanStr = strToLowerCase.replace(/[^a-zA-Z]/g, '')
let isPalindromeArray = cleanStr.split('')

let shiftArray = []
let popArray = []

for (let i = 0; i < isPalindromeArray.length; i++) {
shiftArray.push(isPalindromeArray.shift([i]))
popArray.push(isPalindromeArray.pop([i]))
}
let shiftString = shiftArray.join('')
let popString = popArray.join('')

if (shiftString == popString) {
return true
} else {
return false
}
}

module.exports = { isPalindrome }
53 changes: 51 additions & 2 deletions src/makeChange.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
export default function makeChange({price, amountGiven}) {
// your code here
// makeChange:
//
// Given a price and an amount paid, return the number of coins in each denomination that should be given as change.
//
// Note: because JavaScript is bad at decimal math, the inputs are given as integers that represent dollar + cent amounts, so 139 = $1.39.
//
// makeChange({ price: 100, amountGiven: 100 })
// // => { quarters: 0, dimes: 0, nickels: 0, pennies: 0 }
//
// makeChange({ price: 159, amountGiven: 200 })
// // => { quarters: 1, dimes: 1, nickels: 1, pennies: 1 }
//
// makeChange({ price: 432, amountGiven: 500 })
// // => { quarters: 2, dimes: 1, nickels: 1, pennies: 3 }


let makeChange = options => {
if (typeof options !== 'object') {
throw new Error('Invalid input format. Expected an object')
}
const {price, amountGiven} = options
let change = {
quarters: 0,
dimes: 0,
nickels: 0,
pennies: 0,
}
let result = amountGiven - price

if (result === 0) {
return change
}
while (result >= 25) {
result = result - 25
change.quarters += 1
}
while (result >= 10) {
result = result - 10
change.dimes += 1
}
while (result >= 5) {
result = result - 5
change.nickels += 1
}
while (result >= 1) {
result = result - 1
change.pennies += 1
}
return change
}

module.exports = { makeChange }
24 changes: 24 additions & 0 deletions src/setComplement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// setComplement
//
// Return the complement of two sets.
//
// const a = [1, 2, 3, 4]
// const b = [2, 4, 6, 8]
// setComplement(a, b)
// // => [6, 8]


let setComplement = (setA, setB) => {
if ( !Array.isArray(setA) || !Array.isArray(setB) ) {
throw new Error('Invalid input format. Expected two arrays')
}
let complementArray = []
for(let element of setB) {
if(!setA.includes(element)) {
complementArray.push(element)
}
}
return complementArray
}

module.exports = { setComplement }
Loading