Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for string permutation #23

Merged
merged 4 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Find 2 numbers that add upto N](src/_Problems_/find-2-nums-adding-to-n)
- [Find 2nd Maxinum from an Array](src/_Problems_/find-2nd-max)
- [FizzBuzz](src/_Problems_/fizzbuzz)
- [String Permutaions](src/_Problems_/get_string_permutations)
- [String Permutaions](src/_Problems_/get-string-permutations)
- [Get Subsequence](src/_Problems_/get_subsequence)
- [Get Maze Path](src/_Problems_/get_subsequence)
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { getPermutations } = require('.');

describe('Get permutations of a string', () => {
it('returns permutations of a short string', () => {
const shortString = 'ab';
const expectedPermutations = ['ab', 'ba'];

expect(getPermutations(shortString)).toEqual(expectedPermutations);
});

it('returns permutations of a long string', () => {
const shortString = 'XUNDA';
const expectedPermutations = ['XUNDA', 'UXNDA', 'NXUDA', 'XNUDA', 'UNXDA', 'NUXDA', 'DUXNA', 'UDXNA', 'XDUNA', 'DXUNA', 'UXDNA', 'XUDNA', 'XNDUA', 'NXDUA', 'DXNUA', 'XDNUA', 'NDXUA', 'DNXUA', 'DNUXA', 'NDUXA', 'UDNXA', 'DUNXA', 'NUDXA', 'UNDXA', 'ANDXU', 'NADXU', 'DANXU', 'ADNXU', 'NDAXU', 'DNAXU', 'XNADU', 'NXADU', 'AXNDU', 'XANDU', 'NAXDU', 'ANXDU', 'ADXNU', 'DAXNU', 'XADNU', 'AXDNU', 'DXANU', 'XDANU', 'XDNAU', 'DXNAU', 'NXDAU', 'XNDAU', 'DNXAU', 'NDXAU', 'UDXAN', 'DUXAN', 'XUDAN', 'UXDAN', 'DXUAN', 'XDUAN', 'ADUXN', 'DAUXN', 'UADXN', 'AUDXN', 'DUAXN', 'UDAXN', 'UXADN', 'XUADN', 'AUXDN', 'UAXDN', 'XAUDN', 'AXUDN', 'AXDUN', 'XADUN', 'DAXUN', 'ADXUN', 'XDAUN', 'DXAUN', 'NXAUD', 'XNAUD', 'ANXUD', 'NAXUD', 'XANUD', 'AXNUD', 'UXNAD', 'XUNAD', 'NUXAD', 'UNXAD', 'XNUAD', 'NXUAD', 'NAUXD', 'ANUXD', 'UNAXD', 'NUAXD', 'AUNXD', 'UANXD', 'UAXND', 'AUXND', 'XUAND', 'UXAND', 'AXUND', 'XAUND', 'DAUNX', 'ADUNX', 'UDANX', 'DUANX', 'AUDNX', 'UADNX', 'NADUX', 'ANDUX', 'DNAUX', 'NDAUX', 'ADNUX', 'DANUX', 'DUNAX', 'UDNAX', 'NDUAX', 'DNUAX', 'UNDAX', 'NUDAX', 'NUADX', 'UNADX', 'ANUDX', 'NAUDX', 'UANDX', 'AUNDX'];

expect(getPermutations(shortString).sort()).toEqual(expectedPermutations.sort());
});

it('returns the same string if the string is one character long', () => {
const shortString = 'a';
const expectedPermutations = ['a'];

expect(getPermutations(shortString)).toEqual(expectedPermutations);
});

it('returns an empty array for an empty string', () => {
const shortString = '';
const expectedPermutations = [];

expect(getPermutations(shortString)).toEqual(expectedPermutations);
});

it('is case sensitive', () => {
const shortString = 'aB';

expect(getPermutations(shortString)).not.toEqual(['ab', 'ba']);
expect(getPermutations(shortString)).toEqual(['aB', 'Ba']);
});
});
29 changes: 29 additions & 0 deletions src/_Problems_/get-string-permutations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// GET PERMUTATION OF A GIVEN STRING

const getPermutations = (str) => {
let result = [];

if (str.length == 0) {
return result;
}

if (str.length == 1) {
result.push(str);
return result;
}

let currentCharacter = str.charAt(0);
let restOfString = str.substring(1);
let returnResult = getPermutations(restOfString);

for (j = 0; j < returnResult.length; j++) {
for (i = 0; i <= returnResult[j].length; i++) {
let value = returnResult[j].substring(0, i) + currentCharacter + returnResult[j].substring(i);
result.push(value);
}
}

return result;
};

module.exports = { getPermutations };
24 changes: 0 additions & 24 deletions src/_Problems_/get_string_permutations/index.js

This file was deleted.