diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 8e25740b46e..17243aebb83 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -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": [ diff --git a/client/src/pages/learn/front-end-development/lab-permutation-generator/index.md b/client/src/pages/learn/front-end-development/lab-permutation-generator/index.md new file mode 100644 index 00000000000..3f35df60ea5 --- /dev/null +++ b/client/src/pages/learn/front-end-development/lab-permutation-generator/index.md @@ -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. diff --git a/curriculum/challenges/_meta/lab-permutation-generator/meta.json b/curriculum/challenges/_meta/lab-permutation-generator/meta.json new file mode 100644 index 00000000000..42406ff8a71 --- /dev/null +++ b/curriculum/challenges/_meta/lab-permutation-generator/meta.json @@ -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" +} diff --git a/curriculum/challenges/english/25-front-end-development/lab-permutation-generator/66fe4f33a2cc9b33f4d5cd9b.md b/curriculum/challenges/english/25-front-end-development/lab-permutation-generator/66fe4f33a2cc9b33f4d5cd9b.md new file mode 100644 index 00000000000..00f364fcca4 --- /dev/null +++ b/curriculum/challenges/english/25-front-end-development/lab-permutation-generator/66fe4f33a2cc9b33f4d5cd9b.md @@ -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; +}; +``` diff --git a/curriculum/superblock-structure/full-stack.json b/curriculum/superblock-structure/full-stack.json index 12cd8586bef..01eafd6a7a3 100644 --- a/curriculum/superblock-structure/full-stack.json +++ b/curriculum/superblock-structure/full-stack.json @@ -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" } ]