Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
albinotonnina committed Oct 13, 2020
1 parent f49ac48 commit 49b319f
Show file tree
Hide file tree
Showing 185 changed files with 3,639 additions and 3,234 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"semi": true,
"printWidth": 80
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language:
node_js:
- "8"
- '8'
install:
- npm install
- npm install -g codecov
Expand Down
67 changes: 32 additions & 35 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program",
"program": "${relativeFile}",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program",
"program": "${relativeFile}",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
},
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
48 changes: 24 additions & 24 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ Written in ES6 🔥, tested with Jest 🔍 and some ⏱ perf measurements

## Data Structures

* [Array](src/dataStructures/Array)
* [Hash Table](src/dataStructures/HashTable)
* [Set](src/dataStructures/Set)
* [Singly Linked List](src/dataStructures/SinglyLinkedList)
* [Doubly Linked List](src/dataStructures/DoublyLinkedList)
* [Stack](src/dataStructures/Stack)
* [Queue](src/dataStructures/Queue)
* Tree
* [Binary search tree](src/dataStructures/BinarySearchTree)
* Trie
* Graph
- [Array](src/dataStructures/Array)
- [Hash Table](src/dataStructures/HashTable)
- [Set](src/dataStructures/Set)
- [Singly Linked List](src/dataStructures/SinglyLinkedList)
- [Doubly Linked List](src/dataStructures/DoublyLinkedList)
- [Stack](src/dataStructures/Stack)
- [Queue](src/dataStructures/Queue)
- Tree
- [Binary search tree](src/dataStructures/BinarySearchTree)
- Trie
- Graph

## Algorithms

* [Fizz-Buzz](src/algorithms/FizzBuzz)
* [isPalindrome](src/algorithms/isPalindrome)
* [caesarCipher](src/algorithms/caesarCipher)
* [reverseWords](src/algorithms/reverseWords)
* [reverseArrayInPlace](src/algorithms/reverseArrayInPlace)
* [meanMedianMode](src/algorithms/meanMedianMode)
* [twoSum](src/algorithms/twoSum)
* [getEvenOccurrence](src/algorithms/getEvenOccurrence): Find the first even occurrence of a number in an array
* [getAllPermutations](src/algorithms/getPermutations): Get all the permutations of a string
* [Roman to Numerals](src/algorithms/romanToNumerals): Translate Romans into numerals
* [QueueTwoStacks](src/algorithms/queueStacks): Implement a queue with 2 stacks.
* [GetMaxStack](src/algorithms/getMaxStack): Get the largest item in a stack
* [ResponseHandler](src/algorithms/responseHandler): Add handlers and implement fallbacks
- [Fizz-Buzz](src/algorithms/FizzBuzz)
- [isPalindrome](src/algorithms/isPalindrome)
- [caesarCipher](src/algorithms/caesarCipher)
- [reverseWords](src/algorithms/reverseWords)
- [reverseArrayInPlace](src/algorithms/reverseArrayInPlace)
- [meanMedianMode](src/algorithms/meanMedianMode)
- [twoSum](src/algorithms/twoSum)
- [getEvenOccurrence](src/algorithms/getEvenOccurrence): Find the first even occurrence of a number in an array
- [getAllPermutations](src/algorithms/getPermutations): Get all the permutations of a string
- [Roman to Numerals](src/algorithms/romanToNumerals): Translate Romans into numerals
- [QueueTwoStacks](src/algorithms/queueStacks): Implement a queue with 2 stacks.
- [GetMaxStack](src/algorithms/getMaxStack): Get the largest item in a stack
- [ResponseHandler](src/algorithms/responseHandler): Add handlers and implement fallbacks

[snyk-image]: https://snyk.io/test/github/albinotonnina/javascript-problems/badge.svg
[snyk-url]: https://snyk.io/test/github/albinotonnina/javascript-problems
Expand Down
8 changes: 4 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
"env": {
"test": { presets: [ "@babel/preset-env" ] }
}
};
env: {
test: {presets: ['@babel/preset-env']},
},
};
2 changes: 1 addition & 1 deletion map.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = test => test.replace(/\.tap\.js$/, '.js')
module.exports = (test) => test.replace(/\.tap\.js$/, '.js');
4 changes: 2 additions & 2 deletions src/algorithms/FizzBuzz/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# ⚗️ Fizz-Buzz

The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag.
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag.

The text of the programming assignment is as follows:

> Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
[check the code](fizzbuzz.js) and the [test](__tests__/fizzbuzz.test.js)
[check the code](fizzbuzz.js) and the [test](__tests__/fizzbuzz.test.js)
50 changes: 25 additions & 25 deletions src/algorithms/FizzBuzz/__tests__/fizzbuzz.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { displayPerfTime } from '../../../utils/perf-utils'
import {displayPerfTime} from '../../../utils/perf-utils';

import { fizzBuzz1, fizzBuzz2 } from '../fizzbuzz'
import {fizzBuzz1, fizzBuzz2} from '../fizzbuzz';

describe('Fizz-Buzz', () => {
const vals = [
Expand Down Expand Up @@ -103,43 +103,43 @@ describe('Fizz-Buzz', () => {
97,
98,
'Fizz',
'Buzz'
]
'Buzz',
];

it('should pass with fizzBuzz1', () => {
expect(fizzBuzz1(100)).toEqual(vals)
})
expect(fizzBuzz1(100)).toEqual(vals);
});

it('should pass with fizzBuzz2', () => {
expect(fizzBuzz2(100)).toEqual(vals)
})
expect(fizzBuzz2(100)).toEqual(vals);
});

describe('Performance', () => {
it('will display perftime, num=10', () => {
displayPerfTime('fizzBuzz1(10)', () => {
fizzBuzz1(10)
})
fizzBuzz1(10);
});
displayPerfTime('fizzBuzz2(10)', () => {
fizzBuzz2(10)
})
})
fizzBuzz2(10);
});
});

it('will display perftime, num=100', () => {
displayPerfTime('fizzBuzz1(100)', () => {
fizzBuzz1(10)
})
fizzBuzz1(10);
});
displayPerfTime('fizzBuzz2(100)', () => {
fizzBuzz2(10)
})
})
fizzBuzz2(10);
});
});

it('will display perftime, num=1000', () => {
displayPerfTime('fizzBuzz1(1000)', () => {
fizzBuzz1(1000)
})
fizzBuzz1(1000);
});
displayPerfTime('fizzBuzz2(1000)', () => {
fizzBuzz2(1000)
})
})
})
})
fizzBuzz2(1000);
});
});
});
});
35 changes: 19 additions & 16 deletions src/algorithms/FizzBuzz/fizzbuzz.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export const fizzBuzz1 = num => {
export const fizzBuzz1 = (num) => {
return [...Array(num).keys()]
.map((_, i) => i + 1)
.map(
i =>
i % 15 === 0
? 'FizzBuzz'
: i % 3 === 0 ? 'Fizz' : i % 5 === 0 ? 'Buzz' : i
)
}
.map((i) =>
i % 15 === 0
? 'FizzBuzz'
: i % 3 === 0
? 'Fizz'
: i % 5 === 0
? 'Buzz'
: i
);
};

/*
Expand All @@ -24,20 +27,20 @@ now we apply FizzBuzz logic, by testing all the possible combinations in the cor
*/

export const fizzBuzz2 = num => {
let fizzedBuzzedArr = []
export const fizzBuzz2 = (num) => {
let fizzedBuzzedArr = [];

for (let i = 1; i <= num; i++) {
if (i % 3 === 0 && i % 5 === 0) {
fizzedBuzzedArr.push('FizzBuzz')
fizzedBuzzedArr.push('FizzBuzz');
} else if (i % 3 === 0) {
fizzedBuzzedArr.push('Fizz')
fizzedBuzzedArr.push('Fizz');
} else if (i % 5 === 0) {
fizzedBuzzedArr.push('Buzz')
fizzedBuzzedArr.push('Buzz');
} else {
fizzedBuzzedArr.push(i)
fizzedBuzzedArr.push(i);
}
}

return fizzedBuzzedArr
}
return fizzedBuzzedArr;
};
43 changes: 43 additions & 0 deletions src/algorithms/RiverSizes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function riverSizes(matrix) {
const sizes = [];
const visited = matrix.map((row) => row.map(() => false));
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (visited[i][j]) continue;
traverseNode(i, j, matrix, visited, sizes);
}
}
return sizes;
}

function traverseNode(i, j, matrix, visited, sizes) {
let currentRiverSize = 0;
const nodesToExplore = [[i, j]];
while (nodesToExplore.length) {
const currentNode = nodesToExplore.pop();
i = currentNode[0];
j = currentNode[1];
if (visited[i][j]) continue;
visited[i][j] = true;
if (matrix[i][j] === 0) continue;
currentRiverSize++;
const unvisitedNeighbors = getUnvisitedNeighbors(i, j, matrix, visited);
for (const neighbor of unvisitedNeighbors) {
nodesToExplore.push(neighbor);
}
}
if (currentRiverSize > 0) sizes.push(currentRiverSize);
}

function getUnvisitedNeighbors(i, j, matrix, visited) {
const unvisitedNeighbors = [];
if (i > 0 && !visited[i - 1][j]) unvisitedNeighbors.push([i - 1, j]);
if (i < matrix.length - 1 && !visited[i + 1][j])
unvisitedNeighbors.push([i + 1, j]);
if (j > 0 && !visited[i][j - 1]) unvisitedNeighbors.push([i, j - 1]);
if (j < matrix[0].length - 1 && !visited[i][j + 1])
unvisitedNeighbors.push([i, j + 1]);
return unvisitedNeighbors;
}

export default riverSizes;

0 comments on commit 49b319f

Please sign in to comment.