Skip to content

Commit 1a91821

Browse files
authored
Improved tasks 2693, 2715, 2725
1 parent abe461b commit 1a91821

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

src/main/kotlin/g2701_2800/s2725_interval_cancellation/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #Easy #2023_08_03_Time_52_ms_(99.60%)_Space_43.1_MB_(49.00%)
1+
// #Easy #2023_09_19_Time_51_ms_(98.87%)_Space_43.3_MB_(30.92%)
22

33
function cancellable(fn: Function, args: any[], t: number): Function {
44
fn(...args)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// tslint:disable:no-magic-numbers
2+
import 'src/main/java/g2601_2700/s2693_call_function_with_custom_context/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('callPolyfill', () => {
6+
const fn = function add(b) {
7+
return this.a + b
8+
}
9+
const args = [{"a": 5}, 7]
10+
expect(fn.callPolyfill(...args)).toEqual(12)
11+
})
12+
13+
test('callPolyfill2', () => {
14+
const fn = function tax(price, taxRate) {
15+
return `The cost of the ${this.item} is ${price * taxRate}`
16+
}
17+
const args = [{"item": "burger"}, 10, 1.1]
18+
expect(fn.callPolyfill(...args)).toEqual('The cost of the burger is 11')
19+
})
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// tslint:disable:no-magic-numbers
2+
import { cancellable } from 'src/main/java/g2701_2800/s2715_timeout_cancellation/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('cancellable', () => {
6+
const result = []
7+
const fn = (x) => x * 5
8+
const t = 20
9+
const cancelT = 50
10+
const start = Date.now()
11+
const log = (...argsArr) => {
12+
const diff = Math.floor(Date.now() - start)
13+
result.push({ time: diff, returned: fn(...argsArr) })
14+
}
15+
const cancel = cancellable(log, [2], 20)
16+
const maxT = Math.max(t, 50)
17+
setTimeout(cancel, cancelT)
18+
setTimeout(() => {
19+
expect(result).toEqual([{ time: 20, returned: 10 }])
20+
}, 65)
21+
})
22+
23+
test('cancellable2', () => {
24+
const result = []
25+
const fn = (x) => x ** 2
26+
const t = 100
27+
const cancelT = 50
28+
const start = Date.now()
29+
const log = (...argsArr) => {
30+
const diff = Math.floor(Date.now() - start)
31+
result.push({ time: diff, returned: fn(...argsArr) })
32+
}
33+
const cancel = cancellable(log, [2], 20)
34+
const maxT = Math.max(t, 50)
35+
setTimeout(cancel, cancelT)
36+
setTimeout(() => {
37+
expect(result).toEqual([])
38+
}, 65)
39+
})
40+
41+
test('cancellable3', () => {
42+
const result = []
43+
const fn = (x1, x2) => x1 * x2
44+
const t = 30
45+
const cancelT = 100
46+
const start = Date.now()
47+
const log = (...argsArr) => {
48+
const diff = Math.floor(Date.now() - start)
49+
result.push({ time: diff, returned: fn(...argsArr) })
50+
}
51+
const cancel = cancellable(log, [2, 4], 20)
52+
const maxT = Math.max(t, 50)
53+
setTimeout(cancel, cancelT)
54+
setTimeout(() => {
55+
expect(result).toEqual([{ time: 30, returned: 8 }])
56+
}, 65)
57+
})
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { cancellable } from 'src/main/java/g2701_2800/s2725_interval_cancellation/solution'
2+
import { expect, test } from 'vitest'
3+
4+
test('cancellable', () => {
5+
const result = []
6+
const expected = [
7+
{ time: 0, returned: 8 },
8+
{ time: 35, returned: 8 },
9+
{ time: 70, returned: 8 },
10+
{ time: 105, returned: 8 },
11+
{ time: 140, returned: 8 },
12+
{ time: 175, returned: 8 },
13+
]
14+
const fn = (x) => x * 2
15+
const args = [4]
16+
const t = 35
17+
const cancelT = 190
18+
19+
const start = Date.now()
20+
21+
const log = (...argsArr) => {
22+
const diff = Math.floor(Date.now() - start)
23+
result.push({ time: diff, returned: fn(...argsArr) })
24+
}
25+
26+
const cancel = cancellable(log, args, t)
27+
setTimeout(cancel, cancelT)
28+
29+
setTimeout(() => {
30+
expect(result).toEqual(expected)
31+
}, cancelT + t + 15)
32+
})
33+
34+
test('cancellable2', () => {
35+
const result = []
36+
const expected = [
37+
{ time: 0, returned: 10 },
38+
{ time: 30, returned: 10 },
39+
{ time: 60, returned: 10 },
40+
{ time: 90, returned: 10 },
41+
{ time: 120, returned: 10 },
42+
{ time: 150, returned: 10 },
43+
]
44+
const fn = (x1, x2) => x1 * x2
45+
const args = [2, 5]
46+
const t = 30
47+
const cancelT = 165
48+
49+
const start = Date.now()
50+
51+
const log = (...argsArr) => {
52+
const diff = Math.floor(Date.now() - start)
53+
result.push({ time: diff, returned: fn(...argsArr) })
54+
}
55+
56+
const cancel = cancellable(log, args, t)
57+
setTimeout(cancel, cancelT)
58+
59+
setTimeout(() => {
60+
expect(result).toEqual(expected)
61+
}, cancelT + t + 15)
62+
})
63+
64+
test('cancellable3', () => {
65+
const result = []
66+
const expected = [
67+
{ time: 0, returned: 9 },
68+
{ time: 50, returned: 9 },
69+
{ time: 100, returned: 9 },
70+
{ time: 150, returned: 9 },
71+
]
72+
const fn = (x1, x2, x3) => x1 + x2 + x3
73+
const args = [5, 1, 3]
74+
const t = 50
75+
const cancelT = 180
76+
77+
const start = Date.now()
78+
79+
const log = (...argsArr) => {
80+
const diff = Math.floor(Date.now() - start)
81+
result.push({ time: diff, returned: fn(...argsArr) })
82+
}
83+
84+
const cancel = cancellable(log, args, t)
85+
setTimeout(cancel, cancelT)
86+
87+
setTimeout(() => {
88+
expect(result).toEqual(expected)
89+
}, cancelT + t + 15)
90+
})

0 commit comments

Comments
 (0)