Skip to content

Commit

Permalink
refactor: ✅ 更新Rust示例notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
ARCJ137442 committed Feb 11, 2024
1 parent ddd050f commit efb7307
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions examples/rust.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
" * @param n 所求数字\n",
" * @returns 斐波那契数\n",
" */\n",
"fn fib(n: i32) -> i32 {\n",
"fn fib(n: usize) -> usize {\n",
" if n < 2 {\n",
" return n;\n",
" n\n",
" } else {\n",
" return fib(n - 1) + fib(n - 2);\n",
" fib(n - 1) + fib(n - 2)\n",
" }\n",
"}"
]
Expand All @@ -77,19 +77,16 @@
" * @param n 所求数字\n",
" * @returns 斐波那契数\n",
" */\n",
"fn fib_DP(n:i32) -> i32{\n",
" // 构建线性序列\n",
" // 初始化数组 & 设置初态\n",
" let mut arr: Vec<i32> = vec![0, 1];\n",
"fn fib_DP(n: usize) -> usize {\n",
" // 构建线性序列 & 初始化数组 & 设置初态\n",
" let mut arr: Vec<usize> = vec![0, 1];\n",
" // 设置状态转移方程\n",
" let i: usize; // 只需声明类型,后续在循环中初始化\n",
" let n: usize = n as usize;\n",
" for i in 2..(n+1) {\n",
" // 状态转移:fib(i>2) => fib(i-1) + fib(i-2)\n",
" arr.push(arr[i - 1] + arr[i - 2]);\n",
" arr.push(arr[i-1] + arr[i-2]);\n",
" }\n",
" // 返回最终结果\n",
" return arr[n]\n",
" arr[n]\n",
"}"
]
},
Expand All @@ -102,7 +99,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 4,
"metadata": {
"vscode": {
"languageId": "rust"
Expand All @@ -114,9 +111,15 @@
"output_type": "stream",
"text": [
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]\n",
"递归方法: 0.5693288 s\n",
"递归方法: 0.8210494 s\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]\n",
"动态规划: 0.0000222 s\n"
"动态规划: 0.0000837 s\n"
]
}
],
Expand All @@ -126,17 +129,19 @@
"use std::time::Instant;\n",
"use std::time::Duration;\n",
"\n",
"/** 简单的数组打印函数 */\n",
"fn println_vec(v: Vec<i32>) {\n",
"\tlet i: usize;\n",
"\tprint!(\"[\");\n",
"\tfor i in 0..v.len() {\n",
"\t\tif i != 0 {\n",
"\t\t\tprint!(\", \");\n",
"\t\t}\n",
"\t\tprint!(\"{}\", v[i])\n",
"/**\n",
" * 根据函数和长度,生成一个指定长度的向量\n",
" * @template T 要生成的向量的类型\n",
" * @param f 函数\n",
" * @param len 长度\n",
" * @return Vec<T> 长度为len、内部元素为`v[i] = f(i)`的向量\n",
" */\n",
"fn generate_vec<T>(f: fn(usize) -> T, len: usize) -> Vec<T> {\n",
"\tlet mut v = Vec::new();\n",
"\tfor i in 0..len {\n",
"\t\tv.push(f(i));\n",
"\t}\n",
"\tprintln!(\"]\");\n",
"\tv\n",
"}\n",
"\n",
"/** 打印时间函数 */ \n",
Expand All @@ -149,19 +154,16 @@
"\n",
"/** 定义主函数,允许后续编译成可执行文件 */\n",
"fn main() {\n",
"\tconst N: i32 = 40;\n",
"\tconst N: usize = 40;\n",
"\n",
"\tlet t: Instant = Instant::now();\n",
"\tlet mut arr: Vec<i32> = vec![];\n",
"\tfor i in 0..N { arr.push(fib(i)); }\n",
"\tprintln_vec(arr);\n",
"\tlet arr: Vec<usize> = generate_vec(fib, N);\n",
"\tprintln!(\"{arr:?}\");\n",
"\tprintln_duration_sec(\"递归方法\", t.elapsed());\n",
"\t\n",
"\tlet t: Instant = Instant::now();\n",
"\tlet mut arr: Vec<i32> = vec![];\n",
"\t// arr.clear();\n",
"\tfor i in 0..N { arr.push(fib_DP(i)); }\n",
"\tprintln_vec(arr);\n",
"\tlet arr: Vec<usize> = generate_vec(fib_DP, N);\n",
"\tprintln!(\"{arr:?}\");\n",
"\tprintln_duration_sec(\"动态规划\", t.elapsed());\n",
"}\n",
"// %ignore-below\n",
Expand Down

0 comments on commit efb7307

Please sign in to comment.