Implemented suggesting improvements

This commit is contained in:
Angelos Chalaris
2018-02-19 15:47:47 +02:00
parent 46efdf1bc0
commit c385cd66bf
17 changed files with 1126 additions and 1092 deletions

View File

@ -1,27 +0,0 @@
### anagrams
⚠️ **WARNING**: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
Generates all anagrams of a string (contains duplicates).
Use recursion.
For each letter in the given string, create all the partial anagrams for the rest of its letters.
Use `Array.map()` to combine the letter with each partial anagram, then `Array.reduce()` to combine all anagrams in one array.
Base cases are for string `length` equal to `2` or `1`.
```js
const anagrams = str => {
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str
.split('')
.reduce(
(acc, letter, i) =>
acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)),
[]
);
};
```
```js
anagrams('abc'); // ['abc','acb','bac','bca','cab','cba']
```

16
snippets/isAnagram.md Normal file
View File

@ -0,0 +1,16 @@
### isAnagram
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
Use `String.toLowerCase()`, `String.replace()` with an appropriate regular expression to remove unnecessary characters, `String.split('')`, `Array.sort()` and `Array.join('')` on both strings to normalize them, then check if their normalized forms are equal.
```js
const isAnagram = (str1, str2) => {
const normalize = str => str.toLowerCase().replace(/[^a-z0-9]/gi, '').split('').sort().join('');
return normalize(str1) === normalize(str2);
}
```
```js
isAnagram('iceman', 'cinema'); // true
```

30
snippets/permutations.md Normal file
View File

@ -0,0 +1,30 @@
### permutations
⚠️ **WARNING**: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations.
Generates all permutations of an array's elements (contains duplicates).
Use recursion.
For each element in the given array, create all the partial permutations for the rest of its elements.
Use `Array.map()` to combine the element with each partial permutation, then `Array.reduce()` to combine all permutations in one array.
Base cases are for array `length` equal to `2` or `1`.
```js
const permutations = arr => {
if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
return arr.reduce(
(acc, item, i) =>
acc.concat(
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [
item,
...val,
])
),
[]
);
};
```
```js
permutations([1, 33, 5]) // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]
```

View File

@ -1,37 +0,0 @@
### permuteAll
Uses recursion and `Array.push()` to return all the permutations of the given input in an array.
```js
const permuteAll = (input) => {
const result = [];
let inputState = input;
if (typeof input === 'string') inputState = input.split('');
if (typeof input === 'number') inputState = (input).toString().split('');
const permute = (arr, m = []) => {
(arr.length === 0)
? result.push(m)
: arr.forEach((_, i) => {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next));
});
};
permute(inputState);
return (typeof input === 'string')
? result.map(variant => variant.join(''))
: (typeof input === 'number')
? result.map(variant => parseFloat(variant.join('')))
: result;
};
```
```js
permuteAll('sun') // [ 'sun', 'snu', 'usn', 'uns', 'nsu', 'nus' ]
permuteAll([1, 33, 5]) // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]
permuteAll(345) // [ 345, 354, 435, 453, 534, 543 ]
```

View File

@ -0,0 +1,27 @@
### stringPermutations
⚠️ **WARNING**: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
Generates all permutations of a string (contains duplicates).
Use recursion.
For each letter in the given string, create all the partial permutations for the rest of its letters.
Use `Array.map()` to combine the letter with each partial permutation, then `Array.reduce()` to combine all permutations in one array.
Base cases are for string `length` equal to `2` or `1`.
```js
const stringPermutations = str => {
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str
.split('')
.reduce(
(acc, letter, i) =>
acc.concat(stringPermutations(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)),
[]
);
};
```
```js
stringPermutations('abc'); // ['abc','acb','bac','bca','cab','cba']
```