feat(curriculum): add permutation generator lab (#56482)

This commit is contained in:
Zaira
2024-11-19 22:47:06 +05:00
committed by GitHub
parent 99fafdd727
commit b8736a0992
5 changed files with 193 additions and 1 deletions

View File

@@ -3102,7 +3102,12 @@
]
},
"ndl3": { "title": "242", "intro": [] },
"ndl4": { "title": "243", "intro": [] },
"lab-permutation-generator": {
"title": "Build a Permutation Generator",
"intro": [
"For this lab, you will build a permutation generator that produces all possible permutations of a given string."
]
},
"review-recursion": {
"title": "Recursion Review",
"intro": [

View File

@@ -0,0 +1,9 @@
---
title: Introduction to the Build a Permutation Generator
block: lab-permutation-generator
superBlock: full-stack-developer
---
## Introduction to the Build a Permutation Generator
For this lab, you will build a permutation generator that produces all possible permutations of a given string.

View File

@@ -0,0 +1,11 @@
{
"name": "Build a Permutation Generator",
"isUpcomingChange": true,
"usesMultifileEditor": true,
"dashedName": "lab-permutation-generator",
"superBlock": "full-stack-developer",
"challengeOrder": [{ "id": "66fe4f33a2cc9b33f4d5cd9b", "title": "Build a Permutation Generator" }],
"helpCategory": "JavaScript",
"blockType": "lab",
"blockLayout": "link"
}

View File

@@ -0,0 +1,166 @@
---
id: 66fe4f33a2cc9b33f4d5cd9b
title: Build a Permutation Generator
challengeType: 14
dashedName: build-a-permutation-generator
---
# --description--
In this lab, you will build a permutation generator that will take a string and return all possible permutations of the characters in the string. For example, the possible permutations of the string `'cat'` are `'cat'`, `'cta'`, `'act'`, `'atc'`, `'tac'`, and `'tca'`.
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should create a function named `permuteString`.
2. The `permuteString` function should take three parameters- a string, a prefix value and an empty array for storing and returning the results. The prefix value would accumulate characters to form a permutation.
3. Inside the function, you should check if the length of the passed string is `0`. If it is, push the current prefix to the results and return the results.
4. Iterate over each character in the input string and for each iteration, remove the current character from the string and call the `permuteString` function recursively with updated arguments to build the remaining permutations.
5. You should return the final results array.
6. You should ensure that the permutations are unique by removing duplicates.
# --hints--
You should have a function `permuteString`.
```js
assert.isFunction(permuteString);
```
You should use recursion in your `permuteString` function.
```js
assert.match(permuteString.toString(), /permuteString\s*\(/);
```
`permuteString('far')` should return `[ 'far', 'fra', 'afr', 'arf', 'rfa', 'raf' ]`.
```js
assert.sameMembers(permuteString('far'), [ 'far', 'fra', 'afr', 'arf', 'rfa', 'raf' ]);
```
`permuteString('fcc')` should return `[ 'fcc', 'cfc', 'ccf' ]`.
```js
assert.sameMembers(permuteString('fcc'), [ 'fcc', 'cfc', 'ccf' ]);
```
`permuteString('p')` should return `[ 'p' ]`.
```js
assert.deepStrictEqual(permuteString('p'), ['p']);
```
`permuteString('')` should return `['']`.
```js
let emptyArr= permuteString('');
// 1 because the empty string is being pushed and is a permutation of itself
assert.lengthOf(emptyArr, 1);
```
`permuteString('101')` should return `[ '101', '011', '110' ]`.
```js
assert.sameMembers(permuteString('101'), [ '101', '011', '110']);
```
`permuteString` should return the correct results.
```js
// tests to prevent hard coding
assert.sameMembers(permuteString('road'), [ 'road',
'roda',
'raod',
'rado',
'rdoa',
'rdao',
'orad',
'orda',
'oard',
'oadr',
'odra',
'odar',
'arod',
'ardo',
'aord',
'aodr',
'adro',
'ador',
'droa',
'drao',
'dora',
'doar',
'daro',
'daor' ]);
assert.sameMembers(permuteString('fog'), [ 'fog',
'fgo',
'ofg',
'ogf',
'gfo',
'gof' ]);
assert.sameMembers(permuteString('bird'), [ 'bird',
'bidr',
'brid',
'brdi',
'bdir',
'bdri',
'ibrd',
'ibdr',
'irbd',
'irdb',
'idbr',
'idrb',
'rbid',
'rbdi',
'ribd',
'ridb',
'rdbi',
'rdib',
'dbir',
'dbri',
'dibr',
'dirb',
'drbi',
'drib' ]);
```
# --seed--
## --seed-contents--
```js
```
# --solutions--
```js
const permuteString = (str, prefix = '', result = []) => {
if (str.length === 0) {
result.push(prefix);
return result;
}
// Sort the string initially to group duplicates
const sortedStr = str.split('').sort().join('');
for (let i = 0; i < sortedStr.length; i++) {
if (i > 0 && sortedStr[i] === sortedStr[i - 1]) {
// Skip duplicate characters
continue;
}
const rem = sortedStr.slice(0, i) + sortedStr.slice(i + 1);
permuteString(rem, prefix + sortedStr[i], result);
}
return result;
};
```

View File

@@ -497,6 +497,7 @@
"dashedName": "lecture-understanding-recursion-and-the-call-stack"
},
{ "dashedName": "workshop-decimal-to-binary-converter" },
{ "dashedName": "lab-permutation-generator" },
{ "dashedName": "review-recursion" },
{ "dashedName": "quiz-recursion" }
]