-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Cleaned tests for 'permutations' method.
- Loading branch information
Showing
5 changed files
with
64 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import {findIndices} from "./findIndices"; | ||
import {Unary} from "../types"; | ||
import {Slice, Unary} from "../types"; | ||
import {$equal} from "../boolean/equal"; | ||
|
||
/** | ||
* Returns found "value" indices. | ||
*/ | ||
export const elemIndices = <T>(value: T, xs: T[]): number[] => findIndices($equal(value) as Unary, xs), | ||
export const elemIndices = <T>(value: T, xs: Slice<T>): number[] => findIndices($equal(value) as Unary, xs), | ||
|
||
$elemIndices = <T>(value: T) => | ||
(xs: T[]): number[] => elemIndices(value, xs) | ||
(xs: Slice<T>): number[] => elemIndices(value, xs) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,55 @@ | ||
import {expectLength, expectTrue} from "../helpers"; | ||
import {expectLength, expectTrue, vowelsArray} from "../helpers"; | ||
import {permutations} from "../../src/list/permutations"; | ||
|
||
describe('#permutations', () => { | ||
const areAllPermutesUnique = permutes => { | ||
const xs = permutes, | ||
limit = xs.length; | ||
for (let i = 0; i < limit; i += 1) { | ||
let str = xs[i].join(''); | ||
for (let j = 0; j < limit; j += 1) { | ||
if (j !== i && xs[j].join('') === str) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
}, | ||
const areAllPermutesUnique = permutes => { | ||
const xs = permutes, | ||
limit = xs.length; | ||
for (let i = 0; i < limit; i += 1) { | ||
const str = xs[i].join(''); | ||
for (let j = 0; j < limit; j += 1) { | ||
if (j !== i && xs[j].join('') === str) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
}, | ||
|
||
howManyPermutes = n => { | ||
if (n <= 0) { | ||
return 0; | ||
} | ||
let lastPermutes = 1, | ||
i = 1; | ||
while (i <= n) { | ||
lastPermutes = i * lastPermutes; | ||
i += 1; | ||
} | ||
return lastPermutes; | ||
}; | ||
howManyPermutes = n => { | ||
if (n <= 0) { | ||
return 0; | ||
} | ||
let lastPermutes = 1, | ||
i = 1; | ||
while (i <= n) { | ||
lastPermutes = i * lastPermutes; | ||
i += 1; | ||
} | ||
return lastPermutes; | ||
}; | ||
|
||
it('Should return unique permutations for a given set of items', () => { | ||
const lists: string[] = | ||
'abcd'.split('').reduceRight((agg, item, ind, list) => | ||
agg.concat([list.slice(ind)]), | ||
(<any[]>[]) | ||
); // I know laziness lol | ||
expectLength(4, lists); | ||
expectTrue(lists.every( | ||
(xs, ind) => xs.length === ind + 1 | ||
)); | ||
expectTrue( | ||
lists.every(xs => { | ||
const result = permutations(xs); | ||
return areAllPermutesUnique(result) && | ||
howManyPermutes(xs.length) === result.length; | ||
}) | ||
); | ||
it('Should return unique permutations for a given set of items', () => { | ||
const lists = | ||
vowelsArray.reduceRight((agg, _, ind, list) => | ||
agg.concat([list.slice(ind)]), | ||
[] | ||
); | ||
|
||
// Test initial `lists` | ||
|
||
expect(lists).toHaveLength(vowelsArray.length); | ||
|
||
// Tests initial lists' length | ||
lists.forEach( | ||
(xs, ind) => expect(xs).toHaveLength(ind + 1) | ||
); | ||
|
||
// Test permutations uniqueness, and number | ||
lists.forEach(xs => { | ||
const result = permutations(xs); | ||
expect(areAllPermutesUnique(result)).toEqual(true) | ||
expect(howManyPermutes(xs.length)).toEqual(result.length); | ||
}); | ||
}); | ||
}); |