Skip to content

Commit ef26989

Browse files
committed
🏬 added challenge 8
1 parent 9dd3c84 commit ef26989

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/challenges/08.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function organizeGifts (gifts: string): string {
2+
const giftsRegex = /(\d+)([a-z])/g
3+
const giftsMatches = gifts.matchAll(giftsRegex)
4+
const giftsArray = Array.from(giftsMatches).map((match) => {
5+
const [, count, symbol] = match
6+
return { count: Number(count), symbol }
7+
})
8+
let ans = ''
9+
for (const { count, symbol } of giftsArray) {
10+
let remainder = count
11+
const pallets = Math.floor(remainder / 50)
12+
remainder -= pallets * 50
13+
const boxes = Math.floor(remainder / 10)
14+
remainder -= boxes * 10
15+
const bags = remainder
16+
const palletsStr = `[${symbol}]`.repeat(pallets)
17+
const boxesStr = `{${symbol}}`.repeat(boxes)
18+
const bagsStr = `(${symbol.repeat(bags)})`.repeat(Math.min(1, bags))
19+
ans += palletsStr + boxesStr + bagsStr
20+
}
21+
return ans
22+
}

src/tests/08.spec.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { test, expectTypeOf, expect, describe } from 'vitest'
2+
import { organizeGifts } from '../challenges/08'
3+
4+
describe('Challenge Test Template', () => {
5+
test('Test #01', () => {
6+
expectTypeOf(organizeGifts).returns.toEqualTypeOf('')
7+
})
8+
9+
// Test: organizeGifts("76a11b")
10+
// Expected: "[a]{a}{a}(aaaaaa){b}(b)"
11+
test('Test #02', () => {
12+
expect(organizeGifts('76a11b')).toEqual('[a]{a}{a}(aaaaaa){b}(b)')
13+
})
14+
15+
// Test: organizeGifts("20a")
16+
// Expected:
17+
// "{a}{a}"
18+
test('Test #03', () => {
19+
expect(organizeGifts('20a')).toEqual('{a}{a}')
20+
})
21+
22+
// Test: organizeGifts("70b120a4c")
23+
24+
// Expected:
25+
// "[b]{b}{b}[a][a]{a}{a}(cccc)"
26+
test('Test #04', () => {
27+
expect(organizeGifts('70b120a4c')).toEqual('[b]{b}{b}[a][a]{a}{a}(cccc)')
28+
})
29+
30+
// Test: organizeGifts("9c")
31+
32+
// Expected:
33+
// "(ccccccccc)"
34+
test('Test #05', () => {
35+
expect(organizeGifts('9c')).toEqual('(ccccccccc)')
36+
})
37+
38+
// Test: organizeGifts("19d51e")
39+
40+
// Expected:
41+
// "{d}(ddddddddd)[e](e)"
42+
test('Test #06', () => {
43+
expect(organizeGifts('19d51e')).toEqual('{d}(ddddddddd)[e](e)')
44+
})
45+
})

0 commit comments

Comments
 (0)