From e1f1fbc5e16ab432456c484bfd5f587a16c3c745 Mon Sep 17 00:00:00 2001 From: Renkin <906155099@qq.com> Date: Tue, 21 Nov 2023 11:09:04 +0800 Subject: [PATCH 1/2] fix: fixed i18n for "Code" --- src/locales/en.js | 1 + src/locales/zh.js | 4 ++-- src/solutionTemplate/index.jsx | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/locales/en.js b/src/locales/en.js index e2f68a2..0abddcf 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -295,6 +295,7 @@ const en = { explanationTemplate: { name: "Explanation Template", + code: "Code", goToTheWebsiteToUse: "Go to the website to use", problemAddress: "Problem Address", problemDesc: "Problem Description", diff --git a/src/locales/zh.js b/src/locales/zh.js index 06d2c66..630a4d8 100644 --- a/src/locales/zh.js +++ b/src/locales/zh.js @@ -208,7 +208,6 @@ const zh = { item3: "寻找最右边的满足条件的值", item4: "寻找最左插入位置", item5: "寻找最右插入位置", - }, BFS: { item1: "带层信息", @@ -236,7 +235,7 @@ const zh = { title: "前缀树", item1: "标准前缀树", }, - + uf: { title: "并查集", item1: "不带权并查集", @@ -287,6 +286,7 @@ const zh = { explanationTemplate: { name: "题解模板", + code: "代码", goToTheWebsiteToUse: "去网站使用", problemAddress: "题目地址", problemDesc: "题目描述", diff --git a/src/solutionTemplate/index.jsx b/src/solutionTemplate/index.jsx index 0f5a8dd..accd528 100644 --- a/src/solutionTemplate/index.jsx +++ b/src/solutionTemplate/index.jsx @@ -114,7 +114,7 @@ ${desc} - ${keyword} -## Code +## ${t("Locale.explanationTemplate.code")} - ${t("Locale.explanationTemplate.languageSupport")}:${displayLanguage( language From ada249c5061b35cce80a904982ea29b93876159c Mon Sep 17 00:00:00 2001 From: Renkin <906155099@qq.com> Date: Tue, 21 Nov 2023 11:48:52 +0800 Subject: [PATCH 2/2] feat: i18n update --- src/codeTemplates/grapth.js | 34 ++++++++++++++++++++++++++-------- src/codeTemplates/preSum.js | 36 +++++++++++++++++++++++++++++------- src/locales/en.js | 2 ++ src/locales/zh.js | 2 ++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/codeTemplates/grapth.js b/src/codeTemplates/grapth.js index 20f39b7..473383f 100644 --- a/src/codeTemplates/grapth.js +++ b/src/codeTemplates/grapth.js @@ -15,8 +15,8 @@ module.exports = () => ({ { language: "py", desc: ` - -比如一个图是这样的: +${t("Locale.codeTemplate.graph.item1_desc1")} + \`\`\` E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -25,7 +25,8 @@ E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F -------- 2 ---------> G ------- 1 ------ \`\`\` -我们使用邻接矩阵来构造: +${t("Locale.codeTemplate.graph.item1_desc2")} + \`\`\`py G = { @@ -47,6 +48,7 @@ import heapq def dijkstra(graph, start, end): # 堆里的数据都是 (cost, i) 的二元祖,其含义是“从 start 走到 i 的距离是 cost”。 + # The data in the heap consists of tuples (cost, i), where it signifies "the distance from start to i is cost". heap = [(0, start)] visited = set() while heap: @@ -79,7 +81,10 @@ def dijkstra(graph, start, end): language: "py", text: ` # graph 是邻接矩阵,n 是顶点个数 +# The graph is represented as an adjacency matrix, where n represents the number of vertices. + # graph 形如: graph[u][v] = w +# graph is like: graph[u][v] = w def floyd_warshall(graph, n): dist = [[float("inf") for _ in range(n)] for _ in range(n)] @@ -87,10 +92,13 @@ def floyd_warshall(graph, n): for j in range(n): dist[i][j] = graph[i][j] + # 将顶点k与所有其他顶点(i, j)进行比较 # check vertex k against all other vertices (i, j) for k in range(n): + # 循环遍历图数组的行 # looping through rows of graph array for i in range(n): + # 循环遍历图数组的列 # looping through columns of graph array for j in range(n): if ( @@ -116,7 +124,9 @@ def floyd_warshall(graph, n): { language: "py", text: ` +# 如果不存在,返回-1 # return -1 for not exsit +# 如果存在,返回 dis map,dis[v]表示从点s到点v的最小花费 # else return dis map where dis[v] means for point s the least cost to point v def bell_man(edges, s): dis = defaultdict(lambda: math.inf) @@ -245,7 +255,8 @@ def PrimsAlgorithm(l): # noqa: E741 set_position(positions[start], temp) top_to_bottom(heap, m, size, positions) - + + # 如果最小堆中任意节点的值减小,则更新函数 # Update function if value of any node in min-heap decreases def bottom_to_top(val, index, heap, position): temp = position[index] @@ -283,10 +294,16 @@ def PrimsAlgorithm(l): # noqa: E741 return temp visited = [0 for i in range(len(l))] - Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex + # 所选顶点的邻近树顶点 + # Neighboring Tree Vertex of selected vertex + Nbr_TV = [-1 for i in range(len(l))] + # 部分树的探索顶点到邻近顶点的最小距离 # Minimum Distance of explored vertex with neighboring vertex of partial tree + # 以图表形式呈现 # formed in graph - Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex + # 堆顶点到相邻顶点的距离 + # Heap of Distance of vertices from their neighboring vertex + Distance_TV = [] Positions = [] for x in range(len(l)): @@ -339,8 +356,9 @@ if __name__ == "__main__": # pragma: no cover text: ` def topologicalSort(graph): """ + Kahn算法是使用广度优先搜索(BFS)来找到有向无环图(Directed Acyclic Graph)的拓扑排序的算法。 Kahn's Algorithm is used to find Topological ordering of Directed Acyclic Graph - using BFS + using BFS. """ indegree = [0] * len(graph) queue = collections.deque([]) @@ -369,7 +387,7 @@ def topologicalSort(graph): else: print(topo) - +# 图的邻接表 # Adjacency List of Graph graph = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topologicalSort(graph) diff --git a/src/codeTemplates/preSum.js b/src/codeTemplates/preSum.js index b4a6042..fe7ed57 100644 --- a/src/codeTemplates/preSum.js +++ b/src/codeTemplates/preSum.js @@ -2,53 +2,75 @@ const { t } = require("../locales"); const pre1dJSCode = ` // 建立 + // build const pre = [0] for(const num of nums) { pre.push(pre[pre.length-1] + num) } // 使用,等价于 nums[i] + nums[i + 1] + ... + nums[j] + // Use, equivalent to nums[i] + nums[i + 1] + ... + nums[j] pre[j+1] - pre[i] `; const pre1dPythonCode = ` # 建立 + # build pre = [] for num in nums: pre.append(pre[-1] + num) # 使用,等价于 nums[i] + nums[i + 1] + ... + nums[j] + # Use, equivalent to nums[i] + nums[i + 1] + ... + nums[j] pre[j+1] - pre[i] `; const pre2dPythonCode = ` m,n = len(matrix), len(matrix[0]) # 建立 + # build pre = [[0 for _ in range(n + 1)] for _ in range(m + 1)] for i in range(1, m+1): for j in range(1, n +1): pre[i][j] = pre[i-1][j]+ pre[i][j-1] - pre[i-1][j-1] + matrix[i-1][j-1] # 使用,等价于以(x1,y1)为矩阵左上角以(x2,y2)为矩阵右下角的所有格子的和 + # Use, equivalent to the sum of all cells with (x1, y1) as the upper left corner and (x2, y2) as the lower right corner pre[x2+1][y2+1] + pre[x1][y1] - pre[x1][y2+1] - pre[x2+1][y1] `; const diff1dPythonCode = ` # 差分数组一般是对一个数组的若干区间进行若干次加减操作,求最终更新后的数组。 - d = [0] * n # 差分数组 - ans = [0] * n # 经过若干次操作后的最终数组 - for start, end, inc in updates: # updates 就是一系列操作,start 是开始坐标,end 是结束坐标,inc 是增加的值(可为负数)。 + # The difference array is generally to perform several addition and subtraction operations on several intervals of an array to obtain the finally updated array. + + # 差分数组 + # difference array + d = [0] * n + # 经过若干次操作后的最终数组 + # the final array after several operations + ans = [0] * n + # updates 就是一系列操作,start 是开始坐标,end 是结束坐标,inc 是增加的值(可为负数)。 + # updates is a series of operations, start is the starting coordinate, end is the ending coordinate, and inc is the added value (can be negative). + for start, end, inc in updates: d[start] += seats if end+1 < n: d[end+1] -= inc return list(accumulate(d)) `; const diff2dPythonCode = ` - matrix = [[0] * n for _ in range(n)] # 经过若干次操作后的最终数组 - diff = [[0] * (n+1) for _ in range(n+1)] # 差分数组 - for r1, c1, r2, c2, inc in updates: # updates r1,c1 是左上角坐标,r2, c2 是右下角坐标,inc 是增加的值(可为负数)。 + # 经过若干次操作后的最终数组 + # the final array after several operations + matrix = [[0] * n for _ in range(n)] + # 差分数组 + # difference array + diff = [[0] * (n+1) for _ in range(n+1)] + # updates r1,c1 是左上角坐标,r2, c2 是右下角坐标,inc 是增加的值(可为负数)。 + # updates r1,c1 is the upper left corner coordinate, r2, c2 is the lower right corner coordinate, and inc is the added value (can be negative). + for r1, c1, r2, c2, inc in updates: diff[r1][c1] += inc diff[r1][c2+1] -= inc diff[r2+1][c1] -= inc - diff[r2+1][c2+1] += inc # 别忘记了,由于我们在两个地方对减去 1, 因此在右下角会多减去一个,加上去即可。 + # 别忘记了,由于我们在两个地方对减去 1, 因此在右下角会多减去一个,加上去即可。 + # Don't forget, because we subtract 1 in two places, one more will be subtracted in the lower right corner, so add it back. + diff[r2+1][c2+1] += inc for i in range(n): for j in range(n): matrix[i][j] = diff[i][j] diff --git a/src/locales/en.js b/src/locales/en.js index 0abddcf..f9f8a4a 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -200,6 +200,8 @@ const en = { graph: { title: "Graph", item1: "dijkstra(single-source greedy shortest path)", + item1_desc1: "For example, consider a graph like this:", + item1_desc2: "We construct it using an adjacency matrix:", item2: "floyd_warshall(multi-source dynamic programming shortest path)", item3: "Bellman–Ford(single-source dynamic programming shortest path)", item4: diff --git a/src/locales/zh.js b/src/locales/zh.js index 630a4d8..72aa22a 100644 --- a/src/locales/zh.js +++ b/src/locales/zh.js @@ -195,6 +195,8 @@ const zh = { graph: { title: "图", item1: "dijkstra(单源贪心最短路径)", + item1_desc1: "比如一个图是这样的:", + item1_desc2: "我们使用邻接矩阵来构造:", item2: "floyd_warshall(多源动态规划最短路径)", item3: "Bellman–Ford(单源动态规划最短路径)", item4: "Kruskal(又称加边法,是一种最小生成树算法)",