Add multilingual exercise code (#1959)

Replace the exercise pages' Python-only snippets with source-backed implementations for the 13 visible programming languages, and localize reader-facing comments by site language. Zig remains out of scope.

Approval bypass: repository protection requires one approval but does not enforce it for administrators. All 68 completed checks succeeded; four non-required Java jobs remained queued on the existing ubuntu-20.04 workflow, with no failed checks.
This commit is contained in:
Yudong Jin
2026-08-18 04:57:57 +08:00
committed by GitHub
parent bf86c39b6c
commit 28c1e74c1d
180 changed files with 5795 additions and 230 deletions
+4
View File
@@ -11,6 +11,7 @@ let package = Package(
.executable(name: "time_complexity", targets: ["time_complexity"]),
.executable(name: "worst_best_time_complexity", targets: ["worst_best_time_complexity"]),
.executable(name: "space_complexity", targets: ["space_complexity"]),
.executable(name: "complexity_exercises", targets: ["complexity_exercises"]),
// chapter_array_and_linkedlist
.executable(name: "array", targets: ["array"]),
.executable(name: "linked_list", targets: ["linked_list"]),
@@ -70,6 +71,7 @@ let package = Package(
.executable(name: "binary_search_recur", targets: ["binary_search_recur"]),
.executable(name: "build_tree", targets: ["build_tree"]),
.executable(name: "hanota", targets: ["hanota"]),
.executable(name: "fast_power", targets: ["fast_power"]),
// chapter_backtracking
.executable(name: "preorder_traversal_i_compact", targets: ["preorder_traversal_i_compact"]),
.executable(name: "preorder_traversal_ii_compact", targets: ["preorder_traversal_ii_compact"]),
@@ -114,6 +116,7 @@ let package = Package(
.executableTarget(name: "time_complexity", path: "chapter_computational_complexity", sources: ["time_complexity.swift"]),
.executableTarget(name: "worst_best_time_complexity", path: "chapter_computational_complexity", sources: ["worst_best_time_complexity.swift"]),
.executableTarget(name: "space_complexity", dependencies: ["utils"], path: "chapter_computational_complexity", sources: ["space_complexity.swift"]),
.executableTarget(name: "complexity_exercises", path: "chapter_computational_complexity", sources: ["complexity_exercises.swift"]),
// chapter_array_and_linkedlist
.executableTarget(name: "array", path: "chapter_array_and_linkedlist", sources: ["array.swift"]),
.executableTarget(name: "linked_list", dependencies: ["utils"], path: "chapter_array_and_linkedlist", sources: ["linked_list.swift"]),
@@ -173,6 +176,7 @@ let package = Package(
.executableTarget(name: "binary_search_recur", path: "chapter_divide_and_conquer", sources: ["binary_search_recur.swift"]),
.executableTarget(name: "build_tree", dependencies: ["utils"], path: "chapter_divide_and_conquer", sources: ["build_tree.swift"]),
.executableTarget(name: "hanota", path: "chapter_divide_and_conquer", sources: ["hanota.swift"]),
.executableTarget(name: "fast_power", path: "chapter_divide_and_conquer", sources: ["fast_power.swift"]),
// chapter_backtracking
.executableTarget(name: "preorder_traversal_i_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_i_compact.swift"]),
.executableTarget(name: "preorder_traversal_ii_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_ii_compact.swift"]),
@@ -0,0 +1,62 @@
/**
File: complexity_exercises.swift
Created Time: 2026-08-18
Author: Hello Algo Team
*/
/* */
func sumIter(n: Int) -> Int {
var res = 0
for i in 1 ... n {
res += i
}
return res
}
/* */
func sumRecur(n: Int) -> Int {
if n == 1 {
return 1
}
return n + sumRecur(n: n - 1)
}
/* 线 */
func linearLoop(n: Int) -> Int {
var res = 0
for i in 0 ..< n {
res += i
}
return res
}
/* */
func quadraticLoop(n: Int) -> Int {
var res = 0
for i in 0 ..< n {
for j in i ..< n {
res += j
}
}
return res
}
/* */
func logarithmicLoop(n: Int) -> Int {
var n = n
while n > 1 {
n /= 2
}
return n
}
@main
enum ComplexityExercises {
static func main() {
assert(sumIter(n: 1) == 1 && sumRecur(n: 1) == 1)
assert(sumIter(n: 4) == 10 && sumRecur(n: 4) == 10)
assert(linearLoop(n: 4) == 6)
assert(quadraticLoop(n: 4) == 20)
assert(logarithmicLoop(n: 4) == 1 && logarithmicLoop(n: 5) == 1)
}
}
@@ -0,0 +1,26 @@
/**
File: fast_power.swift
Created Time: 2026-08-18
Author: Hello Algo Team
*/
/* */
func fastPow(x: Int, n: Int) -> Int {
if n == 0 {
return 1
}
let half = fastPow(x: x, n: n / 2)
if n % 2 == 0 {
return half * half
}
return half * half * x
}
@main
enum FastPower {
static func main() {
assert(fastPow(x: 7, n: 0) == 1)
assert(fastPow(x: 3, n: 5) == 243)
assert(fastPow(x: 2, n: 6) == 64)
}
}