8000 fix: add · github-learning/vue3-admin@fbe05fd · GitHub
[go: up one dir, main page]

Skip to content

Commit fbe05fd

Browse files
author
zjb
committed
fix: add
1 parent 9910bc0 commit fbe05fd

File tree

8 files changed

+316
-4
lines changed

8 files changed

+316
-4
lines changed

src/face/js/promise/countScheduler.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class AsyncQueue {
2+
constructor(concurrency) {
3+
// 任务队列,用于存储待执行的任务
4+
this.queue = []
5+
// 当前正在运行的任务数量
6+
this.running = 0
7+
// 最大并发数
8+
this.concurrency = concurrency
9+
}
10+
11+
addTask(task) {
12+
return new Promise((resolve, reject) => {
13+
const taskWrapper = async () => {
14+
// 开始执行任务,增加正在运行的任务数量
15+
this.running++
16+
try {
17+
// 执行任务并获取结果
18+
const result = await task()
19+
// 任务成功完成,将结果传递给 Promise 的 resolve 方法
20+
resolve(result)
21+
} catch (error) {
22+
// 任务执行出错,将错误信息传递给 Promise 的 reject 方法
23+
reject(error)
24+
} finally {
25+
// 任务执行完毕,减少正在运行的任务数量
26+
this.running--
27+
// 尝试从队列中取出下一个任务执行
28+
this.runNext()
29+
}
30+
}
31+
32+
if (this.running < this.concurrency) {
33+
// 如果当前运行的任务数小于最大并发数,立即执行任务
34+
taskWrapper()
35+
} else {
36+
// 否则将任务放入队列等待
37+
this.queue.push(taskWrapper)
38+
}
39+
})
40+
}
41+
42+
runNext() {
43+
if (this.queue.length > 0 && this.running < this.concurrency) {
44+
// 若队列中有任务且当前运行任务数小于最大并发数,取出一个任务执行
45+
const nextTask = this.queue.shift()
46+
nextTask()
47+
}
48+
}
49+
}
50+
51+
const asyncFunc = (i) => {
52+
return () =>
53+
new Promise((resolve) => {
54+
setTimeout(() => {
55+
console.log(`${i}`)
56+
resolve()
57+
}, 1000)
58+
})
59+
}
60+
61+
// 设置最大并发数为 5
62+
const concurrency = 5
63+
const queue = new AsyncQueue(concurrency)
64+
const taskCount = 1000
65+
for (let i = 0; i < taskCount; i++) {
66+
queue.addTask(asyncFunc(i))
67+
}

src/face/js/split.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
var obj = { a: { b: { c: 2 } } }
2+
// const get = (obj, path) => {
3+
// const keys = path.split('.')
4+
// let result = obj
5+
// for (let key of keys) {
6+
// result = result[key]
7+
// }
8+
// return result
9+
// }
210
const get = (obj, path) => {
311
const keys = path.split('.')
412
let result = obj

src/face/js/test/count.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class AsyncQueue {
2+
constructor(count) {
3+
this.count = count
4+
this.queue = []
5+
this.currentCount = 0
6+
}
7+
addTask(task) {
8+
return new Promise((resolve, reject) => {
9+
const wrapper = async () => {
10+
this.currentCount++
11+
try {
12+
const result = await task()
13+
resolve(result)
14+
} catch (err) {
15+
reject(err)
16+
} finally {
17+
this.currentCount--
18+
this.runTask()
19+
}
20+
}
21+
if (this.currentCount < this.count) {
22+
wrapper()
23+
} else {
24+
this.queue.push(wrapper)
25+
}
26+
})
27+
}
28+
runTask() {
29+
if (this.queue.length && this.currentCount < this.count) {
30+
const task = this.queue.shift()
31+
task()
32+
}
33+
}
34+
}
35+
const asyncFunc = (i) => {
36+
return () =>
37+
new Promise((resolve) => {
38+
setTimeout(() => {
39+
console.log(`${i}`)
40+
resolve()
41+
}, 1000)
42+
})
43+
}
44+
const count = 2
45+
const queue = new AsyncQueue(count)
46+
const totalTask = 1000
47+
for (let i = 0; i < totalTask; i++) {
48+
queue.addTask(asyncFunc(i))
49+
}

src/face/js/test/light.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const delay = (ms) => {
2+
return new Promise((resolve) => {
3+
setTimeout(resolve, ms)
4+
})
5+
}
6+
const light = async (color, ms) => {
7+
console.log(`${color}`)
8+
await delay(ms)
9+
}
10+
async function start() {
11+
while (true) {
12+
await light('33', 3000)
13+
await light('22', 2000)
14+
await light('11', 1000)
15+
}
16+
}
17+
start()

src/face/js/test/scheduler.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class AsyncQueue {
2+
constructor() {
3+
this.queue = []
4+
this.runing = false
5+
}
6+
addTask(task) {
7+
this.queue.push(task)
8+
if (!this.runing) {
9+
this.runTask()
10+
}
11+
}
12+
async runTask() {
13+
if (this.queue.length === 0) {
14+
this.runing = false
15+
return
16+
}
17+
const task = this.queue.shift()
18+
this.runing = true
19+
try {
20+
await task()
21+
this.runTask()
22+
} catch {
23+
console.log('err')
24+
}
25+
}
26+
}
27+
const asyncFunc = (i) => {
28+
return () =>
29+
new Promise((resolve) => {
30+
setTimeout(() => {
31+
console.log(`${i}`)
32+
resolve()
33+
}, 1000)
34+
})
35+
}
36+
37+
const queue = new AsyncQueue()
38+
const totalTask = 1000
39+
for (let i = 0; i < totalTask; i++) {
40+
queue.addTask(asyncFunc(i))
41+
}

src/face/js/test/tree.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,61 @@ const flatData = [
66
{ id: 5, name: '子节点1-2', parentId: 2 },
77
{ id: 6, name: '子节点2-1', parentId: 3 }
88
]
9-
const buildTree = (arr) => {
9+
const buildTree = (flatData) => {
1010
const map = new Map()
1111
const tree = []
12-
arr.forEach((el) => {
12+
flatData.forEach((el) => {
1313
map.set(el.id, { ...el, children: [] })
1414
})
15-
arr.forEach((el) => {
15+
flatData.forEach((el) => {
1616
const parentId = el.parentId
1717
if (parentId === null) {
1818
tree.push(map.get(el.id))
1919
} else {
20-
const parent = map.get(parentId)
20+
const parent = map.get(el.parentId)
2121
parent.children.push(map.get(el.id))
2222
}
2323
})
2424
return tree
2525
}
2626
const tree = buildTree(flatData)
2727
console.log(JSON.stringify(tree, null, 2))
28+
const flattenTree = (tree, parentId = null) => {
29+
let result = []
30+
for (const node of tree) {
31+
result.push({
32+
id: node.id,
33+
name: node.name,
34+
parentId: parentId
35+
})
36+
if (node.children && node.children.length) {
37+
result = result.concat(flattenTree(node.children, node.id))
38+
}
39+
}
40+
return result
41+
}
42+
43+
console.log(
44+
'%c [ ]-42',
45+
'font-size:13px; background:pink; color:#bf2c9f;',
46+
flattenTree(tree)
47+
)
48+
// const buildTree = (arr) => {
49+
// const map = new Map()
50+
// const tree = []
51+
// arr.forEach((el) => {
52+
// map.set(el.id, { ...el, children: [] })
53+
// })
54+
// arr.forEach((el) => {
55+
// const parentId = el.parentId
56+
// if (parentId === null) {
57+
// tree.push(map.get(el.id))
58+
// } else {
59+
// const parent = map.get(parentId)
60+
// parent.children.push(map.get(el.id))
61+
// }
62+
// })
63+
// return tree
64+
// }
65+
// const tree = buildTree(flatData)
66+
// console.log(JSON.stringify(tree, null, 2))

src/face/js/test/twoSum.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const nums = [2, 3, 5, 7, 9]
2+
const target = 7
3+
4+
const twoSum = (nums, target) => {
5+
const map = new Map()
6+
for (let i = 0; i < nums.length; i++) {
7+
const diff = target - nums[i]
8+
if (map.has(diff)) {
9+
return [map.get(diff), i]
10+
}
11+
map.set(nums[i], i)
12+
}
13+
return []
14+
}
15+
console.log(twoSum(nums, target))

src/test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const classScores = {
2+
一班: {
3+
张小丙: 87,
4+
张小甲: 98,
5+
张小乙: 90
6+
},
7+
二班: {
8+
王七六: 76,
9+
王九七: 97,
10+
胡八一: 81,
11+
王六零: 60,
12+
刘八一: 81,
13+
李八一: 81
14+
}
15+
}
16+
17+
function calculateRankings(scores) {
18+
const results = []
19+
20+
// 处理每个班级
21+
for (const [className, students] of Object.entries(scores)) {
22+
// 将学生转为数组并排序
23+
const sortedStudents = Object.entries(students)
24+
.map(([name, score]) => ({ name, score }))
25+
.sort((a, b) => b.score - a.score)
26+
27+
// 计算排名
28+
let rank = 1
29+
for (let i = 0; i < sortedStudents.length; i++) {
30+
// 如果不是第一个学生,且分数小于前一个学生,则更新rank
31+
if (i > 0 && sortedStudents[i].score < sortedStudents[i - 1].score) {
32+
rank = i + 1
33+
}
34+
// 保存结果
35+
results.push({
36+
className,
37+
name: sortedStudents[i].name,
38+
rank
39+
})
40+
}
41+
}
42+
43+
// 输出结果(按题目示例顺序)
44+
// 一班
45+
console.log(
46+
`一班 张小丙 第${results.find((r) => r.name === '张小丙').rank}名`
47+
)
48+
console.log(
49+
`一班 张小甲 第${results.find((r) => r.name === '张小甲').rank}名`
50+
)
51+
console.log(
52+
`一班 张小乙 第${results.find((r) => r.name === '张小乙').rank}名`
53+
)
54+
// 二班
55+
console.log(
56+
`二班 李八一 第${results.find((r) => r.name === '李八一').rank}名`
57+
)
58+
console.log(
59+
`二班 刘八一 第${results.find((r) => r.name === '刘八一').rank}名`
60+
)
61+
console.log(
62+
`二班 胡八一 第${results.find((r) => r.name === '胡八一').rank}名`
63+
)
64+
console.log(
65+
`二班 王七六 第${results.find((r) => r.name === '王七六').rank}名`
66+
)
67+
console.log(
68+
`二班 王九七 第${results.find((r) => r.name === '王九七').rank}名`
69+
)
70+
console.log(
71+
`二班 王六零 第${results.find((r) => r.name === '王六零').rank}名`
72+
)
73+
}
74+
75+
// 执行计算
76+
calculateRankings(classScores)

0 commit comments

Comments
 (0)
0