-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): add solution for challenge #3
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Challenge #3: The naughty elf | ||
|
||
In Santa's workshop, **a mischievous elf** has been playing around with the gift production line, adding or removing an unplanned step. | ||
|
||
You have the original sequence of original manufacturing steps and the modified modified sequence that may include an extra step or be missing a step. | ||
|
||
Your task is to **write a function that identifies and returns the first extra step that was added or removed in the manufacturing chain**. If there is no difference between the sequences, return an empty string. | ||
|
||
```js | ||
const original = 'abcd' | ||
const modified = 'abcde' | ||
findNaughtyStep(original, modified) // 'e' | ||
|
||
const original = 'stepfor' | ||
const modified = 'stepor' | ||
findNaughtyStep(original, modified) // 'f' | ||
|
||
const original = 'abcde' | ||
const modified = 'abcde' | ||
findNaughtyStep(original, modified) // '' | ||
``` | ||
|
||
Please, keep in mind: | ||
|
||
- **There will always be one different step or none.** | ||
- **The modification can occur anywhere in the string.** | ||
- **The original steps could be empty** |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe } from 'vitest' | ||
import { findNaughtyStep } from './challenge-03' | ||
|
||
const TEST_CASES: TestCases<[string, string], string> = [ | ||
{ args: ['abcd', 'abcde'], expected: 'e' }, | ||
{ args: ['xxxx', 'xxoxx'], expected: 'o' }, | ||
{ args: ['stepfor', 'stepor'], expected: 'f' }, | ||
{ args: ['iiiii', 'iiiii'], expected: '' }, | ||
] | ||
|
||
describe('Challenge #3: The naughty elf', () => { | ||
buildChallengeTestCases({ | ||
cases: TEST_CASES, | ||
spreadFn: findNaughtyStep, | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function findNaughtyStep(original: string, modified: string) { | ||
const originalLen = original.length | ||
const modifiedLen = modified.length | ||
|
||
const sequences = { | ||
[originalLen]: [original, modified], | ||
[modifiedLen]: [modified, original], | ||
} | ||
|
||
const [steps, reference] = sequences[Math.max(originalLen, modifiedLen)] | ||
|
||
return [...steps].find((step, index) => step !== reference[index]) ?? '' | ||
} |