Skip to content

Commit

Permalink
❌ added challenge 16: friday deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jamerrq committed Dec 16, 2023
1 parent 1e3ca69 commit 1359380
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Para esta versión estaré usando TypeScript, en su versión 5.3.2.
| 13 | [**Calculando el tiempo**](https://adventjs.dev/es/challenges/2023/13) | 🟢 | [TS](./src/challenges/13.ts) | [SPEC](./src/tests/13.spec.ts) |
| 14 | [**Evita la alarma**](https://adventjs.dev/es/challenges/2023/14) | 🟠 | [TS](./src/challenges/14.ts) | [SPEC](./src/tests/14.spec.ts) |
| 15 | [**Robot autónomo**](https://adventjs.dev/es/challenges/2023/15) | 🟠 | [TS](./src/challenges/15.ts) | [SPEC](./src/tests/15.spec.ts) |
| 16 | [**Viernes de deploy**](https://adventjs.dev/es/challenges/2023/16) | 🟢 | [TS](./src/challenges/16.ts) | [SPEC](./src/tests/16.spec.ts) |

## Herramientas utilizadas 🛠️

Expand Down
34 changes: 34 additions & 0 deletions src/challenges/16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// For testing
export interface ANSWER_TYPE {
value: number
left: ANSWER_TYPE | null
right: ANSWER_TYPE | null
}

export function transformTree (tree: Array<number | null>): ANSWER_TYPE | null {
const root = tree.shift()
if (root === undefined || root === null) {
return null
}
const L = []; const R = []
let n = 1; let i = 0; let inL = true; let c = 0
while (i < tree.length) {
if (inL) {
L.push(tree.at(i) as number | null)
} else {
R.push(tree.at(i) as number | null)
}
c++
i++
if (c === n) {
c = 0
inL = !inL
if (inL) n *= 2
}
}
return {
value: root,
left: transformTree(L),
right: transformTree(R)
}
}
174 changes: 174 additions & 0 deletions src/tests/16.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { test, expectTypeOf, assertType, expect, describe } from 'vitest'
import { transformTree } from '../challenges/16'
import type { ANSWER_TYPE } from '../challenges/16'

describe('Challenge #16', () => {
test('Test #01', () => {
expectTypeOf(transformTree).toBeFunction()
assertType<ANSWER_TYPE | null>(transformTree([]))
})

test('Test #02', () => {
expect(transformTree([])).toEqual(null)
})

test('Test #03', () => {
expect(transformTree([1])).toEqual({
value: 1,
left: null,
right: null
})
})

test('Test #04', () => {
expect(transformTree([1, 2, 3])).toEqual({
value: 1,
left: {
value: 2,
left: null,
right: null
},
right: {
value: 3,
left: null,
right: null
}
})
})

test('Test #05', () => {
expect(transformTree([1, 2, 3, 4, 5])).toEqual({
value: 1,
left: {
value: 2,
left: {
value: 4,
left: null,
right: null
},
right: {
value: 5,
left: null,
right: null
}
},
right: {
value: 3,
left: null,
right: null
}
})
})

test('Test #06', () => {
expect(transformTree([1, 2, 3, 4, 5, 6, 7])).toEqual({
value: 1,
left: {
value: 2,
left: {
value: 4,
left: null,
right: null
},
right: {
value: 5,
left: null,
right: null
}
},
right: {
value: 3,
left: {
value: 6,
left: null,
right: null
},
right: {
value: 7,
left: null,
right: null
}
}
})
})

test('Test #07', () => {
expect(transformTree([17, 0, null, null, 1])).toEqual({
value: 17,
left: {
value: 0,
left: null,
right: {
value: 1,
left: null,
right: null
}
},
right: null
})
})

test('Test #08', () => {
expect(transformTree([3, 1, 0, 8, 12, null, 1])).toEqual({
value: 3,
left: {
value: 1,
left: {
value: 8,
left: null,
right: null
},
right: {
value: 12,
left: null,
right: null
}
},
right: {
value: 0,
left: null,
right: {
value: 1,
left: null,
right: null
}
}
})
})

test('Test #09', () => {
expect(transformTree([2, 7, 5, null, 6, null, 9, null, null, 1, 11, null, null, null, 10])).toEqual({
value: 2,
left: {
value: 7,
left: null,
right: {
value: 6,
left: {
value: 1,
left: null,
right: null
},
right: {
value: 11,
left: null,
right: null
}
}
},
right: {
value: 5,
left: null,
right: {
value: 9,
left: null,
right: {
value: 10,
left: null,
right: null
}
}
}
})
})
})

0 comments on commit 1359380

Please sign in to comment.