Skip to content

Commit ff2c81a

Browse files
authored
Improved tasks
1 parent e714fe5 commit ff2c81a

File tree

7 files changed

+68
-76
lines changed

7 files changed

+68
-76
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// #Easy #2023_08_02_Time_64_ms_(91.95%)_Space_42.3_MB_(96.51%)
22

33
function cancellable(fn: Function, args: any[], t: number): Function {
4-
let cancelled: boolean = false;
4+
let cancelled: boolean = false
55
setTimeout(() => {
66
if (!cancelled) {
77
fn(...args)
88
}
9-
}, t);
9+
}, t)
1010
return () => {
1111
cancelled = true
1212
}
@@ -18,17 +18,17 @@ function cancellable(fn: Function, args: any[], t: number): Function {
1818
* const fn = (x) => x * 5
1919
* const args = [2], t = 20, cancelT = 50
2020
*
21-
* const start = performance.now()
21+
* const start = performance.now()
2222
*
2323
* const log = (...argsArr) => {
2424
* const diff = Math.floor(performance.now() - start);
2525
* result.push({"time": diff, "returned": fn(...argsArr))
2626
* }
27-
*
27+
*
2828
* const cancel = cancellable(log, args, t);
2929
*
3030
* const maxT = Math.max(t, cancelT)
31-
*
31+
*
3232
* setTimeout(() => {
3333
* cancel()
3434
* }, cancelT)
@@ -38,4 +38,4 @@ function cancellable(fn: Function, args: any[], t: number): Function {
3838
* }, maxT + 15)
3939
*/
4040

41-
export { cancellable }
41+
export { cancellable }
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
// #Medium #2023_08_02_Time_63_ms_(99.09%)_Space_43_MB_(82.94%)
22

33
async function promiseAll<T>(functions: (() => Promise<T>)[]): Promise<T[]> {
4-
const resolved = []
5-
let counter = 0
4+
const resolved = []
5+
let counter = 0
66

7-
return new Promise((resolve, reject) => {
8-
for (let i = 0; i < functions.length; i++) {
9-
functions[i]().then((res) => {
10-
// must specify index of array
11-
resolved[i] = res
12-
counter++
13-
if (counter === functions.length) {
14-
resolve(resolved)
7+
return new Promise((resolve, reject) => {
8+
for (let i = 0; i < functions.length; i++) {
9+
functions[i]()
10+
.then((res) => {
11+
// must specify index of array
12+
resolved[i] = res
13+
counter++
14+
if (counter === functions.length) {
15+
resolve(resolved)
16+
}
17+
})
18+
.catch((err) => {
19+
reject(err)
20+
})
1521
}
16-
}).catch((err) => {
17-
reject(err)
18-
})
19-
}
20-
})
22+
})
2123
}
2224

2325
/*
2426
* const promise = promiseAll([() => new Promise(res => res(42))])
2527
* promise.then(console.log); // [42]
2628
*/
2729

28-
export { promiseAll }
30+
export { promiseAll }

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
function join(arr1: any[], arr2: any[]): any[] {
44
const result: any = {}
5-
for(let obj of arr1) {
5+
for (let obj of arr1) {
66
result[obj.id] = obj
77
}
8-
for(let obj of arr2) {
9-
if(result[obj.id]) {
10-
for(let key in obj) {
8+
for (let obj of arr2) {
9+
if (result[obj.id]) {
10+
for (let key in obj) {
1111
result[obj.id][key] = obj[key]
1212
}
1313
} else {
1414
result[obj.id] = obj
1515
}
1616
}
17-
return Object.values(result)
17+
return Object.values(result)
1818
}
1919

2020
export { join }
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// #Easy #2023_08_02_Time_56_ms_(97.63%)_Space_43.1_MB_(45.64%)
22

33
async function addTwoPromises(promise1: Promise<number>, promise2: Promise<number>): Promise<number> {
4-
return await promise1 + await promise2
5-
};
4+
return (await promise1) + (await promise2)
5+
}
66

77
/*
88
* addTwoPromises(Promise.resolve(2), Promise.resolve(2))
99
* .then(console.log); // 4
1010
*/
1111

12-
export { addTwoPromises }
12+
export { addTwoPromises }

src/test/kotlin/g2701_2800/s2721_execute_asynchronous_functions_in_parallel/solution.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ import { promiseAll } from 'src/main/kotlin/g2701_2800/s2721_execute_asynchronou
33
import { expect, test } from 'vitest'
44

55
test('promiseAll', () => {
6-
const functions = [
7-
() => new Promise(resolve => setTimeout(() => resolve(5), 200))
8-
]
9-
promiseAll(functions).then((e)=> expect(e).toEqual({"t": 200, "resolved": [5]}))
6+
const functions = [() => new Promise((resolve) => setTimeout(() => resolve(5), 200))]
7+
promiseAll(functions).then((e) => expect(e).toEqual({ t: 200, resolved: [5] }))
108
})
119

1210
test('promiseAll2', () => {
1311
const functions = [
14-
() => new Promise(resolve => setTimeout(() => resolve(1), 200)),
15-
() => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))
16-
]
17-
promiseAll(functions).then((e)=> expect(e).toEqual({"t": 100, "rejected": "Error"}))
12+
() => new Promise((resolve) => setTimeout(() => resolve(1), 200)),
13+
() => new Promise((resolve, reject) => setTimeout(() => reject('Error'), 100)),
14+
]
15+
promiseAll(functions).then((e) => expect(e).toEqual({ t: 100, rejected: 'Error' }))
1816
})
1917

2018
test('promiseAll3', () => {
2119
const functions = [
22-
() => new Promise(resolve => setTimeout(() => resolve(4), 50)),
23-
() => new Promise(resolve => setTimeout(() => resolve(10), 150)),
24-
() => new Promise(resolve => setTimeout(() => resolve(16), 100))
25-
]
26-
promiseAll(functions).then((e)=> expect(e).toEqual({"t": 150, "resolved": [4, 10, 16]}))
20+
() => new Promise((resolve) => setTimeout(() => resolve(4), 50)),
21+
() => new Promise((resolve) => setTimeout(() => resolve(10), 150)),
22+
() => new Promise((resolve) => setTimeout(() => resolve(16), 100)),
23+
]
24+
promiseAll(functions).then((e) => expect(e).toEqual({ t: 150, resolved: [4, 10, 16] }))
2725
})

src/test/kotlin/g2701_2800/s2722_join_two_arrays_by_id/solution.test.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,41 @@ import { expect, test } from 'vitest'
44

55
test('join', () => {
66
const arr1 = [
7-
{"id": 1, "x": 1},
8-
{"id": 2, "x": 9}
9-
]
10-
const arr2 = [
11-
{"id": 3, "x": 5}
7+
{ id: 1, x: 1 },
8+
{ id: 2, x: 9 },
129
]
10+
const arr2 = [{ id: 3, x: 5 }]
1311

1412
const result = [
15-
{"id": 1, "x": 1},
16-
{"id": 2, "x": 9},
17-
{"id": 3, "x": 5}
18-
]
13+
{ id: 1, x: 1 },
14+
{ id: 2, x: 9 },
15+
{ id: 3, x: 5 },
16+
]
1917
expect(join(arr1, arr2)).toEqual(result)
2018
})
2119

2220
test('join2', () => {
23-
const arr1 = [
24-
{"id": 1, "x": 2, "y": 3},
25-
{"id": 2, "x": 3, "y": 6}
21+
const arr1 = [
22+
{ id: 1, x: 2, y: 3 },
23+
{ id: 2, x: 3, y: 6 },
2624
]
2725
const arr2 = [
28-
{"id": 2, "x": 10, "y": 20},
29-
{"id": 3, "x": 0, "y": 0}
26+
{ id: 2, x: 10, y: 20 },
27+
{ id: 3, x: 0, y: 0 },
3028
]
3129

3230
const result = [
33-
{"id": 1, "x": 2, "y": 3},
34-
{"id": 2, "x": 10, "y": 20},
35-
{"id": 3, "x": 0, "y": 0}
36-
]
31+
{ id: 1, x: 2, y: 3 },
32+
{ id: 2, x: 10, y: 20 },
33+
{ id: 3, x: 0, y: 0 },
34+
]
3735
expect(join(arr1, arr2)).toEqual(result)
3836
})
3937

4038
test('join3', () => {
41-
const arr1 = [
42-
{"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}
43-
]
44-
const arr2 = [
45-
{"id": 1, "b": {"c": 84}, "v": [1, 3]}
46-
]
39+
const arr1 = [{ id: 1, b: { b: 94 }, v: [4, 3], y: 48 }]
40+
const arr2 = [{ id: 1, b: { c: 84 }, v: [1, 3] }]
4741

48-
const result = [
49-
{"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
50-
]
42+
const result = [{ id: 1, b: { c: 84 }, v: [1, 3], y: 48 }]
5143
expect(join(arr1, arr2)).toEqual(result)
5244
})

src/test/kotlin/g2701_2800/s2723_add_two_promises/solution.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { addTwoPromises } from 'src/main/kotlin/g2701_2800/s2723_add_two_promise
33
import { expect, test } from 'vitest'
44

55
test('addTwoPromises', () => {
6-
const promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20))
7-
const promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60))
8-
addTwoPromises(promise1, promise2).then(e => expect(e).toEqual(7))
6+
const promise1 = new Promise((resolve) => setTimeout(() => resolve(2), 20))
7+
const promise2 = new Promise((resolve) => setTimeout(() => resolve(5), 60))
8+
addTwoPromises(promise1, promise2).then((e) => expect(e).toEqual(7))
99
})
1010

1111
test('addTwoPromises2', () => {
12-
const promise1 = new Promise(resolve => setTimeout(() => resolve(10), 50))
13-
const promise2 = new Promise(resolve => setTimeout(() => resolve(-12), 30))
14-
addTwoPromises(promise1, promise2).then(e => expect(e).toEqual(-2))
12+
const promise1 = new Promise((resolve) => setTimeout(() => resolve(10), 50))
13+
const promise2 = new Promise((resolve) => setTimeout(() => resolve(-12), 30))
14+
addTwoPromises(promise1, promise2).then((e) => expect(e).toEqual(-2))
1515
})

0 commit comments

Comments
 (0)