Skip to content

Commit

Permalink
Merge pull request #605 from Chalarangelo/add-permuteAll
Browse files Browse the repository at this point in the history
Add permuteAll
  • Loading branch information
Chalarangelo committed Feb 19, 2018
2 parents dead0ac + 3c02245 commit 542de4b
Show file tree
Hide file tree
Showing 14 changed files with 1,110 additions and 1,106 deletions.
27 changes: 0 additions & 27 deletions snippets/anagrams.md

This file was deleted.

16 changes: 16 additions & 0 deletions snippets/isAnagram.md
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions snippets/permutations.md
Original file line number Diff line number Diff line change
@@ -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 ] ]
```
27 changes: 27 additions & 0 deletions snippets/stringPermutations.md
Original file line number Diff line number Diff line change
@@ -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']
```
4 changes: 3 additions & 1 deletion tag_database
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
all:array,function
anagrams:string,recursion
any:array,function
approximatelyEqual:math
arrayToHtmlList:browser,array
Expand Down Expand Up @@ -117,6 +116,7 @@ intersectionWith:array,function
invertKeyValues:object,function
is:type,array,regexp
isAbsoluteURL:string,utility,browser,url
isAnagram:string,regexp
isArrayLike:type,array
isBoolean:type
isDivisible:math
Expand Down Expand Up @@ -183,6 +183,7 @@ partial:function
partialRight:function
partition:array,object,function
percentile:math
permutations:array,recursion
pick:object,array
pickBy:object,array,function
pipeAsyncFunctions:adapter,function,promise
Expand Down Expand Up @@ -235,6 +236,7 @@ splitLines:string
spreadOver:adapter
stableSort:array,sort,advanced
standardDeviation:math,array
stringPermutations:string,recursion
stripHTMLTags:string,utility,regexp
sum:math,array
sumBy:math,array,function
Expand Down
11 changes: 0 additions & 11 deletions test/anagrams/anagrams.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/anagrams/anagrams.test.js

This file was deleted.

5 changes: 5 additions & 0 deletions test/isAnagram/isAnagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isAnagram = (str1, str2) => {
const normalize = str => str.toLowerCase().replace(/[^a-z0-9]/gi, '').split('').sort().join('');
return normalize(str1) === normalize(str2);
};
module.exports = isAnagram;
17 changes: 17 additions & 0 deletions test/isAnagram/isAnagram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const test = require('tape');
const isAnagram = require('./isAnagram.js');

test('Testing isAnagram', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof isAnagram === 'function', 'isAnagram is a Function');
t.true(isAnagram('iceman', 'cinema'), 'Checks valid anagram');
t.true(isAnagram('rail safety', 'fairy tales'), 'Works with spaces');
t.true(isAnagram('roast beef', 'eat for BSE'), 'Ignores case');
t.true(isAnagram('Regera Dowdy', 'E. G. Deadworry'), 'Ignores special characters');
//t.deepEqual(isAnagram(args..), 'Expected');
//t.equal(isAnagram(args..), 'Expected');
//t.false(isAnagram(args..), 'Expected');
//t.throws(isAnagram(args..), 'Expected');
t.end();
});
14 changes: 14 additions & 0 deletions test/permutations/permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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,
])
),
[]
);
};
module.exports = permutations;
14 changes: 14 additions & 0 deletions test/permutations/permutations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const test = require('tape');
const permutations = require('./permutations.js');

test('Testing permutations', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof permutations === 'function', 'permutations is a Function');
t.deepEqual(permutations([1, 33, 5]), [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ], 'Generates all permutations of an array');
//t.deepEqual(permuteAll(args..), 'Expected');
//t.equal(permuteAll(args..), 'Expected');
//t.false(permuteAll(args..), 'Expected');
//t.throws(permuteAll(args..), 'Expected');
t.end();
});
11 changes: 11 additions & 0 deletions test/stringPermutations/stringPermutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)),
[]
);
};
module.exports = stringPermutations;
16 changes: 16 additions & 0 deletions test/stringPermutations/stringPermutations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const test = require('tape');
const stringPermutations = require('./stringPermutations.js');

test('Testing stringPermutations', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof stringPermutations === 'function', 'stringPermutations is a Function');
t.deepEqual(stringPermutations('abc'), ['abc','acb','bac','bca','cab','cba'], "Generates all stringPermutations of a string");
t.deepEqual(stringPermutations('a'), ['a'], "Works for single-letter strings");
t.deepEqual(stringPermutations(''), [''], "Works for empty strings");
//t.deepEqual(anagrams(args..), 'Expected');
//t.equal(anagrams(args..), 'Expected');
//t.false(anagrams(args..), 'Expected');
//t.throws(anagrams(args..), 'Expected');
t.end();
});

0 comments on commit 542de4b

Please sign in to comment.