From 67cf2b3c0e20d3d162eb9116fe5d37b3c499533d Mon Sep 17 00:00:00 2001 From: Bhanu Pratap Singh Rathore Date: Mon, 10 Dec 2018 12:33:04 +0530 Subject: [PATCH] feat(interview-prep): Porting Rosetta Problem: Sort Disjoint (#31105) * feat(interview-prep): Porting Rosetta Problem: Sort Disjoint * feat(interview-prep): Fixes * feat(interview-prep): FIxed indent and solution * Added whitespace to the tests --- .../rosetta-code/sort-disjoint-sublist.md | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-disjoint-sublist.md diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-disjoint-sublist.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-disjoint-sublist.md new file mode 100644 index 0000000000..8d0a40dd65 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-disjoint-sublist.md @@ -0,0 +1,82 @@ +--- +id: 5a23c84252665b21eecc8000 +title: Sort disjoint sublist +challengeType: 5 +--- + +## Description +
+Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, but preserving the values at indices outside the set of those to be sorted. +Make your function work with the following list of values and set of indices: + values: [7, 6, 5, 4, 3, 2, 1, 0] + indices(0-based): {6, 1, 7} +Where the correct result would be: +[7, 0, 5, 4, 3, 2, 1, 6]. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: sortDisjoint should be a function. + testString: assert(typeof sortDisjoint == 'function', 'sortDisjoint should be a function.'); + - text: sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]) should return a array. + testString: assert(Array.isArray(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])), 'sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]) should return a array.'); + - text: sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]) should return [7, 0, 5, 4, 3, 2, 1, 6]. + testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]), [7, 0, 5, 4, 3, 2, 1, 6], 'sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]) should return [7, 0, 5, 4, 3, 2, 1, 6].'); + - text: sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6]) should return [7, 1, 2, 4, 3, 5, 6, 0]. + testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6]), [7, 1, 2, 4, 3, 5, 6, 0], 'sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6]) should return [7, 1, 2, 4, 3, 5, 6, 0].'); + - text: sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7]) should return [8, 1, 6, 5, 4, 3, 2, 7]. + testString: assert.deepEqual(sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7]), [8, 1, 6, 5, 4, 3, 2, 7], 'sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7]) should return [8, 1, 6, 5, 4, 3, 2, 7].'); + - text: sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6]) should return [8, 2, 6, 3, 4, 5, 7, 1]. + testString: assert.deepEqual(sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6]), [8, 2, 6, 3, 4, 5, 7, 1], 'sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6]) should return [8, 2, 6, 3, 4, 5, 7, 1].'); + - text: sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4]) should return [6, 1, 7, 1, 3, 5, 6]. + testString: assert.deepEqual(sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4]), [6, 1, 7, 1, 3, 5, 6],'sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4]) should return [6, 1, 7, 1, 3, 5, 6].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function sortDisjoint (values, indices) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ +```js +function sortDisjoint(values, indices) { + let sublist = []; + + indices.sort(function (a, b) { return a - b; }); + + for (let i = 0; i < indices.length; i++) { + sublist.push(values[indices[i]]); + } + + sublist.sort((a, b) => { return a - b; }); + + for (let i = 0; i < indices.length; i++) { + values[indices[i]] = sublist[i]; + } + + return values; +} +``` + +