Skip to content

Commit 5ce80df

Browse files
authored
Improved tasks 2623-2637
1 parent c62e15e commit 5ce80df

File tree

12 files changed

+338
-14
lines changed

12 files changed

+338
-14
lines changed

src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #Hard #2023_07_17_Time_314_ms_(99.39%)_Space_115.7_MB_(62.42%)
1+
// #Hard #2023_08_31_Time_264_ms_(98.86%)_Space_115.9_MB_(61.71%)
22

33
type Fn = (...params: any) => any
44

@@ -13,22 +13,29 @@ function memoize(fn: Fn): Fn {
1313
currentCache = new Map()
1414
cache.set(args.length, currentCache)
1515
}
16+
1617
for (let i = 0, len = args.length; i <= len; i++) {
1718
const arg = args[i]
1819
const isEnd = i >= len - 1
20+
1921
if (currentCache.has(arg)) {
2022
if (isEnd) {
2123
return currentCache.get(arg)
2224
} else {
2325
currentCache = currentCache.get(arg)
2426
}
25-
} else if (!isEnd) {
27+
} else if (isEnd) {
28+
break
29+
} else {
2630
const newSubCache = new Map()
31+
2732
currentCache.set(arg, newSubCache)
2833
currentCache = newSubCache
2934
}
3035
}
31-
let value = fn.apply( ...args)
36+
37+
let value = fn(...args)
38+
3239
currentCache.set(args[args.length - 1], value)
3340
return value
3441
}

src/test/kotlin/g2601_2700/s2623_memoize/solution.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,29 @@ import { memoize } from 'src/main/kotlin/g2601_2700/s2623_memoize/solution'
33
import { expect, test } from 'vitest'
44

55
test('memoize', () => {
6-
expect(1).toEqual(1)
6+
const sum = (a, b) => a + b
7+
const memoizedSum = memoize(sum)
8+
expect(memoizedSum(2, 2)).toEqual(4)
9+
// Returns 4. sum() was called as (2, 2) was not seen before.
10+
expect(memoizedSum(2, 2)).toEqual(4)
11+
// Returns 4. However sum() was not called because the same inputs were seen before.
12+
// Total call count: 1
13+
expect(memoizedSum(1, 2)).toEqual(3)
14+
// Returns 3. sum() was called as (1, 2) was not seen before.
15+
// Total call count: 2
16+
})
17+
18+
test('memoize2', () => {
19+
const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1))
20+
const memoFactorial = memoize(factorial)
21+
expect(memoFactorial(2)).toEqual(2)
22+
// Returns 2.
23+
expect(memoFactorial(3)).toEqual(6)
24+
// Returns 6.
25+
expect(memoFactorial(2)).toEqual(2)
26+
// Returns 2. However factorial was not called because 2 was seen before.
27+
// Total call count: 2
28+
expect(memoFactorial(3)).toEqual(6)
29+
// Returns 6. However factorial was not called because 3 was seen before.
30+
// Total call count: 2
731
})

src/test/kotlin/g2601_2700/s2624_snail_traversal/solution.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,25 @@ import 'src/main/kotlin/g2601_2700/s2624_snail_traversal/solution'
33
import { expect, test } from 'vitest'
44

55
test('snail', () => {
6-
expect(1).toEqual(1)
6+
let nums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15].snail(5, 4)
7+
let result = [
8+
[19, 17, 16, 15],
9+
[10, 1, 14, 4],
10+
[3, 2, 12, 20],
11+
[7, 5, 18, 11],
12+
[9, 8, 6, 13],
13+
]
14+
expect(nums).toEqual(result)
15+
})
16+
17+
test('snail2', () => {
18+
let nums = [1, 2, 3, 4].snail(1, 4)
19+
let result = [[1, 2, 3, 4]]
20+
expect(nums).toEqual(result)
21+
})
22+
23+
test('snail3', () => {
24+
let nums = [1, 3].snail(2, 2)
25+
let result = []
26+
expect(nums).toEqual(result)
727
})

src/test/kotlin/g2601_2700/s2625_flatten_deeply_nested_array/solution.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,27 @@ import { flat } from 'src/main/kotlin/g2601_2700/s2625_flatten_deeply_nested_arr
33
import { expect, test } from 'vitest'
44

55
test('flat', () => {
6-
expect(1).toEqual(1)
6+
let arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
7+
let n = 0
8+
let result = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
9+
expect(flat(arr, n)).toEqual(result)
10+
})
11+
12+
test('flat2', () => {
13+
let arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
14+
let n = 1
15+
let result = [1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
16+
expect(flat(arr, n)).toEqual(result)
17+
})
18+
19+
test('flat3', () => {
20+
let arr = [
21+
[1, 2, 3],
22+
[4, 5, 6],
23+
[7, 8, [9, 10, 11], 12],
24+
[13, 14, 15],
25+
]
26+
let n = 2
27+
let result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
28+
expect(flat(arr, n)).toEqual(result)
729
})

src/test/kotlin/g2601_2700/s2626_array_reduce_transformation/solution.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,28 @@ import { reduce } from 'src/main/kotlin/g2601_2700/s2626_array_reduce_transforma
33
import { expect, test } from 'vitest'
44

55
test('reduce', () => {
6-
expect(1).toEqual(1)
6+
let nums = [1, 2, 3, 4]
7+
let fn = function sum(accum, curr) {
8+
return accum + curr
9+
}
10+
let init = 0
11+
expect(reduce(nums, fn, init)).toEqual(10)
12+
})
13+
14+
test('reduce2', () => {
15+
let nums = [1, 2, 3, 4]
16+
let fn = function sum(accum, curr) {
17+
return accum + curr * curr
18+
}
19+
let init = 100
20+
expect(reduce(nums, fn, init)).toEqual(130)
21+
})
22+
23+
test('reduce3', () => {
24+
let nums = []
25+
let fn = function sum(accum, curr) {
26+
return 0
27+
}
28+
let init = 25
29+
expect(reduce(nums, fn, init)).toEqual(25)
730
})

src/test/kotlin/g2601_2700/s2627_debounce/solution.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@ import { debounce } from 'src/main/kotlin/g2601_2700/s2627_debounce/solution'
33
import { expect, test } from 'vitest'
44

55
test('debounce', () => {
6+
const log = debounce(console.log, 100)
7+
log('Hello')
8+
// cancelled
9+
log('Hello')
10+
// cancelled
11+
log('Hello')
12+
// Logged at t=100ms
613
expect(1).toEqual(1)
714
})

src/test/kotlin/g2601_2700/s2629_function_composition/solution.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,22 @@ import { compose } from 'src/main/kotlin/g2601_2700/s2629_function_composition/s
33
import { expect, test } from 'vitest'
44

55
test('compose', () => {
6-
expect(1).toEqual(1)
6+
let functions = [(x) => x + 1, (x) => x * x, (x) => 2 * x]
7+
let x = 4
8+
const fn = compose(functions)
9+
expect(fn(x)).toEqual(65)
10+
})
11+
12+
test('compose2', () => {
13+
let functions = [(x) => 10 * x, (x) => 10 * x, (x) => 10 * x]
14+
let x = 1
15+
const fn = compose(functions)
16+
expect(fn(x)).toEqual(1000)
17+
})
18+
19+
test('compose3', () => {
20+
let functions = []
21+
let x = 42
22+
const fn = compose(functions)
23+
expect(fn(x)).toEqual(42)
724
})

src/test/kotlin/g2601_2700/s2630_memoize_ii/solution.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,55 @@ import { memoize } from 'src/main/kotlin/g2601_2700/s2630_memoize_ii/solution'
33
import { expect, test } from 'vitest'
44

55
test('memoize', () => {
6-
expect(1).toEqual(1)
6+
let getInputs = () => [
7+
[2, 2],
8+
[2, 2],
9+
[1, 2],
10+
]
11+
let results = [4, 4, 3]
12+
let fn = function (a, b) {
13+
return a + b
14+
}
15+
let memorized = memoize(fn)
16+
let inputs = getInputs()
17+
for (let i = 0; i < inputs.length; i++) {
18+
expect(memorized(...inputs[i])).toEqual(results[i])
19+
}
20+
})
21+
22+
test('memoize2', () => {
23+
let getInputs = () => [
24+
[{}, {}],
25+
[{}, {}],
26+
[{}, {}],
27+
]
28+
let results = [{}, {}, {}]
29+
let fn = function (a, b) {
30+
return { ...a, ...b }
31+
}
32+
let memorized = memoize(fn)
33+
let inputs = getInputs()
34+
for (let i = 0; i < inputs.length; i++) {
35+
expect(memorized(...inputs[i])).toEqual(results[i])
36+
}
37+
})
38+
39+
test('memoize3', () => {
40+
let getInputs = () => {
41+
const o = {}
42+
return [
43+
[o, o],
44+
[o, o],
45+
[o, o],
46+
]
47+
}
48+
let results = [{}, {}, {}]
49+
let fn = function (a, b) {
50+
return { ...a, ...b }
51+
}
52+
let memorized = memoize(fn)
53+
let inputs = getInputs()
54+
for (let i = 0; i < inputs.length; i++) {
55+
expect(memorized(...inputs[i])).toEqual(results[i])
56+
}
757
})

src/test/kotlin/g2601_2700/s2631_group_by/solution.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,44 @@ import 'src/main/kotlin/g2601_2700/s2631_group_by/solution'
33
import { expect, test } from 'vitest'
44

55
test('groupBy', () => {
6-
expect(1).toEqual(1)
6+
let fn = function (item) {
7+
return item.id
8+
}
9+
let array = [{ id: '1' }, { id: '1' }, { id: '2' }].groupBy(fn)
10+
let result = {
11+
'1': [{ id: '1' }, { id: '1' }],
12+
'2': [{ id: '2' }],
13+
}
14+
expect(array).toEqual(result)
15+
})
16+
17+
test('groupBy2', () => {
18+
let fn = function (list) {
19+
return String(list[0])
20+
}
21+
let array = [
22+
[1, 2, 3],
23+
[1, 3, 5],
24+
[1, 5, 9],
25+
].groupBy(fn)
26+
let result = {
27+
'1': [
28+
[1, 2, 3],
29+
[1, 3, 5],
30+
[1, 5, 9],
31+
],
32+
}
33+
expect(array).toEqual(result)
34+
})
35+
36+
test('groupBy3', () => {
37+
let fn = function (n) {
38+
return String(n > 5)
39+
}
40+
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].groupBy(fn)
41+
let result = {
42+
true: [6, 7, 8, 9, 10],
43+
false: [1, 2, 3, 4, 5],
44+
}
45+
expect(array).toEqual(result)
746
})

src/test/kotlin/g2601_2700/s2634_filter_elements_from_array/solution.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,25 @@ import { filter } from 'src/main/kotlin/g2601_2700/s2634_filter_elements_from_ar
33
import { expect, test } from 'vitest'
44

55
test('filter', () => {
6-
expect(1).toEqual(1)
6+
let arr = [0, 10, 20, 30]
7+
let fn = function greaterThan10(n) {
8+
return n > 10
9+
}
10+
expect(filter(arr, fn)).toEqual([20, 30])
11+
})
12+
13+
test('filter2', () => {
14+
let arr = [-2, -1, 0, 1, 2]
15+
let fn = function plusOne(n) {
16+
return n + 1
17+
}
18+
expect(filter(arr, fn)).toEqual([-2, 0, 1, 2])
19+
})
20+
21+
test('filter3', () => {
22+
let arr = [1, 2, 3]
23+
let fn = function firstIndex(n, i) {
24+
return i === 0
25+
}
26+
expect(filter(arr, fn)).toEqual([1])
727
})

0 commit comments

Comments
 (0)