Skip to content

fix: fixed i18n for "Code" #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/codeTemplates/grapth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = () => ({
{
language: "py",
desc: `

比如一个图是这样的:
${t("Locale.codeTemplate.graph.item1_desc1")}


\`\`\`
E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F
Expand All @@ -25,7 +25,8 @@ E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F
-------- 2 ---------> G ------- 1 ------
\`\`\`

我们使用邻接矩阵来构造:
${t("Locale.codeTemplate.graph.item1_desc2")}


\`\`\`py
G = {
Expand All @@ -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:
Expand Down Expand Up @@ -79,18 +81,24 @@ 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)]

for i in range(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 (
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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([])
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 29 additions & 7 deletions src/codeTemplates/preSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -295,6 +297,7 @@ const en = {

explanationTemplate: {
name: "Explanation Template",
code: "Code",
goToTheWebsiteToUse: "Go to the website to use",
problemAddress: "Problem Address",
problemDesc: "Problem Description",
Expand Down
6 changes: 4 additions & 2 deletions src/locales/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ const zh = {
graph: {
title: "图",
item1: "dijkstra(单源贪心最短路径)",
item1_desc1: "比如一个图是这样的:",
item1_desc2: "我们使用邻接矩阵来构造:",
item2: "floyd_warshall(多源动态规划最短路径)",
item3: "Bellman–Ford(单源动态规划最短路径)",
item4: "Kruskal(又称加边法,是一种最小生成树算法)",
Expand All @@ -208,7 +210,6 @@ const zh = {
item3: "寻找最右边的满足条件的值",
item4: "寻找最左插入位置",
item5: "寻找最右插入位置",

},
BFS: {
item1: "带层信息",
Expand Down Expand Up @@ -236,7 +237,7 @@ const zh = {
title: "前缀树",
item1: "标准前缀树",
},

uf: {
title: "并查集",
item1: "不带权并查集",
Expand Down Expand Up @@ -287,6 +288,7 @@ const zh = {

explanationTemplate: {
name: "题解模板",
code: "代码",
goToTheWebsiteToUse: "去网站使用",
problemAddress: "题目地址",
problemDesc: "题目描述",
Expand Down
2 changes: 1 addition & 1 deletion src/solutionTemplate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ${desc}

- ${keyword}

## Code
## ${t("Locale.explanationTemplate.code")}

- ${t("Locale.explanationTemplate.languageSupport")}:${displayLanguage(
language
Expand Down