Travis build: 1751 [cron]
This commit is contained in:
@ -14,14 +14,16 @@ These snippets, while useful and interesting, didn't quite make it into the repo
|
|||||||
* [`countVowels`](#countvowels)
|
* [`countVowels`](#countvowels)
|
||||||
* [`factors`](#factors)
|
* [`factors`](#factors)
|
||||||
* [`fibonacciCountUntilNum`](#fibonaccicountuntilnum)
|
* [`fibonacciCountUntilNum`](#fibonaccicountuntilnum)
|
||||||
* [`howManyTimes`](#howmanytimes)
|
* [`fibonacciUntilNum`](#fibonacciuntilnum)
|
||||||
* [`httpDelete`](#httpdelete)
|
* [`httpDelete`](#httpdelete)
|
||||||
* [`httpPut`](#httpput)
|
* [`httpPut`](#httpput)
|
||||||
* [`isArmstrongNumber`](#isarmstrongnumber)
|
* [`isArmstrongNumber`](#isarmstrongnumber)
|
||||||
|
* [`isSimilar`](#issimilar)
|
||||||
|
* [`levenshteinDistance`](#levenshteindistance)
|
||||||
* [`quickSort`](#quicksort)
|
* [`quickSort`](#quicksort)
|
||||||
* [`removeVowels`](#removevowels)
|
* [`removeVowels`](#removevowels)
|
||||||
* [`solveRPN`](#solverpn)
|
* [`solveRPN`](#solverpn)
|
||||||
* [`fibonacciUntilNum`](#fibonacciuntilnum)
|
* [`howManyTimes`](#howmanytimes)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -264,26 +266,21 @@ fibonacciCountUntilNum(10); // 7
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### howManyTimes
|
### fibonacciUntilNum
|
||||||
|
|
||||||
Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer.
|
Generates an array, containing the Fibonacci sequence, up until the nth term.
|
||||||
Works for both negative and positive integers.
|
|
||||||
|
|
||||||
If `divisor` is `-1` or `1` return `Infinity`.
|
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
|
||||||
If `divisor` is `-0` or `0` return `0`.
|
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
|
||||||
Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer.
|
Uses a mathematical formula to calculate the length of the array required.
|
||||||
Return the number of times the loop was executed, `i`.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const howManyTimes = (num, divisor) => {
|
const fibonacciUntilNum = num => {
|
||||||
if (divisor === 1 || divisor === -1) return Infinity;
|
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
||||||
if (divisor === 0) return 0;
|
return Array.from({ length: n }).reduce(
|
||||||
let i = 0;
|
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
||||||
while (Number.isInteger(num / divisor)) {
|
[]
|
||||||
i++;
|
);
|
||||||
num = num / divisor;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -291,10 +288,7 @@ const howManyTimes = (num, divisor) => {
|
|||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
howManyTimes(100, 2); // 2
|
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
||||||
howManyTimes(100, 2.5); // 2
|
|
||||||
howManyTimes(100, 0); // 0
|
|
||||||
howManyTimes(100, -1); // Infinity
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
@ -398,6 +392,75 @@ isArmstrongNumber(56); // false
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
|
### isSimilar
|
||||||
|
|
||||||
|
Determines if the `pattern` matches with `str`.
|
||||||
|
|
||||||
|
Use `String.toLowerCase()` to convert both strings to lowercase, then loop through `str` and determine if it contains all characters of `pattern` and in the correct order.
|
||||||
|
Adapted from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db53428247e741b9efe2cde9667050c/code/fts_fuzzy_match.js#L18).
|
||||||
|
|
||||||
|
``` js
|
||||||
|
const isSimilar = (pattern, str) =>
|
||||||
|
[...str].reduce(
|
||||||
|
(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0
|
||||||
|
) === pattern.length ? true : false;
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
``` js
|
||||||
|
isSimilar('rt','Rohit'); // true
|
||||||
|
isSimilar('tr','Rohit'); // false
|
||||||
|
```<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
|
### levenshteinDistance
|
||||||
|
|
||||||
|
Calculates the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) between two strings.
|
||||||
|
|
||||||
|
Calculates the number of changes (substitutions, deletions or additions) required to convert `string1` to `string2`.
|
||||||
|
Can also be used to compare two strings as shown in the second example.
|
||||||
|
|
||||||
|
``` js
|
||||||
|
const levenshteinDistance = (string1, string2) => {
|
||||||
|
if(string1.length === 0) return string2.length;
|
||||||
|
if(string2.length === 0) return string1.length;
|
||||||
|
let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
|
||||||
|
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
|
||||||
|
for(let i = 1; i <= string2.length; i++){
|
||||||
|
for(let j = 1; j<=string1.length; j++){
|
||||||
|
if(string2[i-1] === string1[j-1]){
|
||||||
|
matrix[i][j] = matrix[i-1][j-1];
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
matrix[i][j] = Math.min(matrix[i-1][j-1]+1, matrix[i][j-1]+1, matrix[i-1][j]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matrix[string2.length][string1.length];
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```js
|
||||||
|
levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7
|
||||||
|
const compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length));
|
||||||
|
compareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%)
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### quickSort
|
### quickSort
|
||||||
|
|
||||||
QuickSort an Array (ascending sort by default).
|
QuickSort an Array (ascending sort by default).
|
||||||
@ -508,21 +571,26 @@ solveRPN('2 3 ^'); // 8
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### fibonacciUntilNum
|
### howManyTimes
|
||||||
|
|
||||||
Generates an array, containing the Fibonacci sequence, up until the nth term.
|
Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer.
|
||||||
|
Works for both negative and positive integers.
|
||||||
|
|
||||||
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
|
If `divisor` is `-1` or `1` return `Infinity`.
|
||||||
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
|
If `divisor` is `-0` or `0` return `0`.
|
||||||
Uses a mathematical formula to calculate the length of the array required.
|
Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer.
|
||||||
|
Return the number of times the loop was executed, `i`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const fibonacciUntilNum = num => {
|
const howManyTimes = (num, divisor) => {
|
||||||
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
if (divisor === 1 || divisor === -1) return Infinity;
|
||||||
return Array.from({ length: n }).reduce(
|
if (divisor === 0) return 0;
|
||||||
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
let i = 0;
|
||||||
[]
|
while (Number.isInteger(num / divisor)) {
|
||||||
);
|
i++;
|
||||||
|
num = num / divisor;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -530,7 +598,10 @@ const fibonacciUntilNum = num => {
|
|||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
howManyTimes(100, 2); // 2
|
||||||
|
howManyTimes(100, 2.5); // 2
|
||||||
|
howManyTimes(100, 0); // 0
|
||||||
|
howManyTimes(100, -1); // Infinity
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
5
test/isSimilar/isSimilar.js
Normal file
5
test/isSimilar/isSimilar.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
const isSimilar = (pattern, str) =>
|
||||||
|
[...str].reduce(
|
||||||
|
(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0
|
||||||
|
) === pattern.length ? true : false;
|
||||||
|
module.exports = isSimilar;
|
||||||
13
test/isSimilar/isSimilar.test.js
Normal file
13
test/isSimilar/isSimilar.test.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const test = require('tape');
|
||||||
|
const isSimilar = require('./isSimilar.js');
|
||||||
|
|
||||||
|
test('Testing isSimilar', (t) => {
|
||||||
|
//For more information on all the methods supported by tape
|
||||||
|
//Please go to https://github.com/substack/tape
|
||||||
|
t.true(typeof isSimilar === 'function', 'isSimilar is a Function');
|
||||||
|
//t.deepEqual(isSimilar(args..), 'Expected');
|
||||||
|
//t.equal(isSimilar(args..), 'Expected');
|
||||||
|
//t.false(isSimilar(args..), 'Expected');
|
||||||
|
//t.throws(isSimilar(args..), 'Expected');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
@ -1,15 +1,15 @@
|
|||||||
const levenshteinDistance = (string1,string2) => {
|
const levenshteinDistance = (string1, string2) => {
|
||||||
if(string1.length === 0) return string2.length;
|
if(string1.length === 0) return string2.length;
|
||||||
if(string2.length === 0) return string1.length;
|
if(string2.length === 0) return string1.length;
|
||||||
let matrix = Array(string2.length+ 1).fill(0).map((x,i) => [i]);
|
let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
|
||||||
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
|
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
|
||||||
for(let i = 1;i <= string2.length;i++){
|
for(let i = 1; i <= string2.length; i++){
|
||||||
for(let j = 1;j<=string1.length; j++){
|
for(let j = 1; j<=string1.length; j++){
|
||||||
if(string2[i-1] === string1[j-1]){
|
if(string2[i-1] === string1[j-1]){
|
||||||
matrix[i][j] = matrix[i-1][j-1];
|
matrix[i][j] = matrix[i-1][j-1];
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
matrix[i][j] = Math.min(matrix[i-1][j-1]+1,matrix[i][j-1]+1,matrix[i-1][j]+1);
|
matrix[i][j] = Math.min(matrix[i-1][j-1]+1, matrix[i][j-1]+1, matrix[i-1][j]+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
const pad = (string, length = 8, char = ' ') =>
|
const pad = (str, length, char = ' ') =>
|
||||||
string.padStart((string.length + length) / 2, char).padEnd(length, char);
|
str.padStart((str.length + length) / 2, char).padEnd(length, char);
|
||||||
module.exports = pad;
|
module.exports = pad;
|
||||||
@ -1,9 +1,6 @@
|
|||||||
const sortedLastIndex = (arr, n) => {
|
const sortedLastIndex = (arr, n) => {
|
||||||
const isDescending = arr[0] > arr[arr.length - 1];
|
const isDescending = arr[0] > arr[arr.length - 1];
|
||||||
const index = arr
|
const index = arr.reverse().findIndex(el => (isDescending ? n <= el : n >= el));
|
||||||
.map((val, i) => [i, val])
|
|
||||||
.reverse()
|
|
||||||
.findIndex(el => (isDescending ? n <= el[1] : n >= el[1]));
|
|
||||||
return index === -1 ? 0 : arr.length - index - 1;
|
return index === -1 ? 0 : arr.length - index - 1;
|
||||||
};
|
};
|
||||||
module.exports = sortedLastIndex;
|
module.exports = sortedLastIndex;
|
||||||
@ -2,9 +2,9 @@ const sortedLastIndexBy = (arr, n, fn) => {
|
|||||||
const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
|
const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
|
||||||
const val = fn(n);
|
const val = fn(n);
|
||||||
const index = arr
|
const index = arr
|
||||||
.map((val, i) => [i, fn(val)])
|
.map(fn)
|
||||||
.reverse()
|
.reverse()
|
||||||
.findIndex(el => (isDescending ? val <= el[1] : val >= el[1]));
|
.findIndex(el => (isDescending ? val <= el : val >= el));
|
||||||
return index === -1 ? 0 : arr.length - index;
|
return index === -1 ? 0 : arr.length - index;
|
||||||
};
|
};
|
||||||
module.exports = sortedLastIndexBy;
|
module.exports = sortedLastIndexBy;
|
||||||
3800
test/testlog
3800
test/testlog
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user