Skip to content

Commit

Permalink
feat(2023): add solution for challenge #3
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 3, 2023
1 parent 25ddf20 commit ebc9f92
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 2023/challenge-03/README.md
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**
16 changes: 16 additions & 0 deletions 2023/challenge-03/challenge-03.test.ts
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,
})
})
13 changes: 13 additions & 0 deletions 2023/challenge-03/challenge-03.ts
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]) ?? ''
}

0 comments on commit ebc9f92

Please sign in to comment.