From 043085d0eaca8eeef3e34da748ca5941ea34ab9d Mon Sep 17 00:00:00 2001 From: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com> Date: Wed, 3 Apr 2024 20:01:29 +0700 Subject: [PATCH] fix: adapt missing ruby style guide (#1216) --- .../chapter_array_and_linkedlist/array.rb | 16 +++++------ .../linked_list.rb | 22 +++++++-------- .../ruby/chapter_array_and_linkedlist/list.rb | 4 +-- .../chapter_array_and_linkedlist/my_list.rb | 26 ++++++++--------- .../recursion.rb | 12 ++++---- .../space_complexity.rb | 28 +++++++++---------- .../time_complexity.rb | 22 +++++++-------- .../worst_best_time_complexity.rb | 4 +-- codes/ruby/utils/list_node.rb | 4 +-- codes/ruby/utils/print_util.rb | 8 +++--- .../linked_list.md | 10 +++---- docs/chapter_array_and_linkedlist/list.md | 4 +-- .../space_complexity.md | 8 +++--- 13 files changed, 84 insertions(+), 84 deletions(-) diff --git a/codes/ruby/chapter_array_and_linkedlist/array.rb b/codes/ruby/chapter_array_and_linkedlist/array.rb index cfba45f18..db450eb97 100644 --- a/codes/ruby/chapter_array_and_linkedlist/array.rb +++ b/codes/ruby/chapter_array_and_linkedlist/array.rb @@ -7,7 +7,7 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) ### 随机访问元素 ### def random_access(nums) # 在区间 [0, nums.length) 中随机抽取一个数字 - random_index = Random.rand 0...(nums.length - 1) + random_index = Random.rand(0...nums.length) # 获取并返回随机元素 nums[random_index] @@ -84,24 +84,24 @@ nums = [1, 3, 2, 5, 4] puts "数组 nums = #{nums}" # 随机访问 -random_num = random_access nums +random_num = random_access(nums) puts "在 nums 中获取随机元素 #{random_num}" # 长度扩展 -nums = extend nums, 3 +nums = extend(nums, 3) puts "将数组长度扩展至 8 ,得到 nums = #{nums}" # 插入元素 -insert nums, 6, 3 +insert(nums, 6, 3) puts "在索引 3 处插入数字 6 ,得到 nums = #{nums}" # 删除元素 -remove nums, 2 +remove(nums, 2) puts "删除索引 2 处的元素,得到 nums = #{nums}" # 遍历数组 -traverse nums +traverse(nums) # 查找元素 -index = find nums, 3 -puts "在 nums 中查找元素 3 ,得到索引 = #{index}"; +index = find(nums, 3) +puts "在 nums 中查找元素 3 ,得到索引 = #{index}" diff --git a/codes/ruby/chapter_array_and_linkedlist/linked_list.rb b/codes/ruby/chapter_array_and_linkedlist/linked_list.rb index 42e2beb77..7b05f53cc 100644 --- a/codes/ruby/chapter_array_and_linkedlist/linked_list.rb +++ b/codes/ruby/chapter_array_and_linkedlist/linked_list.rb @@ -51,32 +51,32 @@ end # 初始化链表 # 初始化各个节点 -n0 = ListNode.new 1 -n1 = ListNode.new 3 -n2 = ListNode.new 2 -n3 = ListNode.new 5 -n4 = ListNode.new 4 +n0 = ListNode.new(1) +n1 = ListNode.new(3) +n2 = ListNode.new(2) +n3 = ListNode.new(5) +n4 = ListNode.new(4) # 构建节点之间的引用 n0.next = n1 n1.next = n2 n2.next = n3 n3.next = n4 puts "初始化的链表为" -print_linked_list n0 +print_linked_list(n0) # 插入节点 -insert n0, ListNode.new(0) +insert(n0, ListNode.new(0)) print_linked_list n0 # 删除节点 -remove n0 +remove(n0) puts "删除节点后的链表为" -print_linked_list n0 +print_linked_list(n0) # 访问节点 -node = access n0, 3 +node = access(n0, 3) puts "链表中索引 3 处的节点的值 = #{node.val}" # 查找节点 -index = find n0, 2 +index = find(n0, 2) puts "链表中值为 2 的节点的索引 = #{index}" diff --git a/codes/ruby/chapter_array_and_linkedlist/list.rb b/codes/ruby/chapter_array_and_linkedlist/list.rb index 7a4dd9bf0..a7d0590e6 100644 --- a/codes/ruby/chapter_array_and_linkedlist/list.rb +++ b/codes/ruby/chapter_array_and_linkedlist/list.rb @@ -31,11 +31,11 @@ nums << 4 puts "添加元素后 nums = #{nums}" # 在中间插入元素 -nums.insert 3, 6 +nums.insert(3, 6) puts "在索引 3 处插入元素 6 ,得到 nums = #{nums}" # 删除元素 -nums.delete_at 3 +nums.delete_at(3) puts "删除索引 3 处的元素,得到 nums = #{nums}" # 通过索引遍历列表 diff --git a/codes/ruby/chapter_array_and_linkedlist/my_list.rb b/codes/ruby/chapter_array_and_linkedlist/my_list.rb index c90c4a69f..bb29646ac 100644 --- a/codes/ruby/chapter_array_and_linkedlist/my_list.rb +++ b/codes/ruby/chapter_array_and_linkedlist/my_list.rb @@ -14,7 +14,7 @@ class MyList @capacity = 10 @size = 0 @extend_ratio = 2 - @arr = Array.new capacity + @arr = Array.new(capacity) end ### 访问元素 ### @@ -86,9 +86,9 @@ class MyList def to_array sz = size # 仅转换有效长度范围内的列表元素 - arr = Array.new sz + arr = Array.new(sz) for i in 0...sz - arr[i] = get i + arr[i] = get(i) end arr end @@ -100,32 +100,32 @@ end nums = MyList.new # 在尾部添加元素 -nums.add 1 -nums.add 3 -nums.add 2 -nums.add 5 -nums.add 4 +nums.add(1) +nums.add(3) +nums.add(2) +nums.add(5) +nums.add(4) puts "列表 nums = #{nums.to_array} ,容量 = #{nums.capacity} ,长度 = #{nums.size}" # 在中间插入元素 -nums.insert 3, 6 +nums.insert(3, 6) puts "在索引 3 处插入数字 6 ,得到 nums = #{nums.to_array}" # 删除元素 -nums.remove 3 +nums.remove(3) puts "删除索引 3 的元素,得到 nums = #{nums.to_array}" # 访问元素 -num = nums.get 1 +num = nums.get(1) puts "访问索引 1 处的元素,得到 num = #{num}" # 更新元素 -nums.set 1, 0 +nums.set(1, 0) puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums.to_array}" # 测试扩容机制 for i in 0...10 # 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 - nums.add i + nums.add(i) end puts "扩容后的列表 nums = #{nums.to_array} ,容量 = #{nums.capacity} ,长度 = #{nums.size}" diff --git a/codes/ruby/chapter_computational_complexity/recursion.rb b/codes/ruby/chapter_computational_complexity/recursion.rb index 6fae30851..f0d68782f 100644 --- a/codes/ruby/chapter_computational_complexity/recursion.rb +++ b/codes/ruby/chapter_computational_complexity/recursion.rb @@ -9,7 +9,7 @@ def recur(n) # 终止条件 return 1 if n == 1 # 递:递归调用 - res = recur n - 1 + res = recur(n - 1) # 归:返回结果 n + res end @@ -39,7 +39,7 @@ def tail_recur(n, res) # 终止条件 return res if n == 0 # 尾递归调用 - tail_recur n - 1, res + n + tail_recur(n - 1, res + n) end ### 斐波那契数列:递归 ### @@ -56,14 +56,14 @@ end n = 5 -res = recur n +res = recur(n) puts "\n递归函数的求和结果 res = #{res}" -res = for_loop_recur n +res = for_loop_recur(n) puts "\n使用迭代模拟递归求和结果 res = #{res}" -res = tail_recur n, 0 +res = tail_recur(n, 0) puts "\n尾递归函数的求和结果 res = #{res}" -res = fib n +res = fib(n) puts "\n斐波那契数列的第 #{n} 项为 #{res}" diff --git a/codes/ruby/chapter_computational_complexity/space_complexity.rb b/codes/ruby/chapter_computational_complexity/space_complexity.rb index cad8f530d..8172aca60 100644 --- a/codes/ruby/chapter_computational_complexity/space_complexity.rb +++ b/codes/ruby/chapter_computational_complexity/space_complexity.rb @@ -30,7 +30,7 @@ end ### 线性阶 ### def linear(n) # 长度为 n 的列表占用 O(n) 空间 - nums = Array.new n, 0 + nums = Array.new(n, 0) # 长度为 n 的哈希表占用 O(n) 空间 hmap = {} @@ -43,13 +43,13 @@ end def linear_recur(n) puts "递归 n = #{n}" return if n == 1 - linear_recur n - 1 + linear_recur(n - 1) end ### 平方阶 ### def quadratic(n) # 二维列表占用 O(n^2) 空间 - Array.new(n) { Array.new n, 0 } + Array.new(n) { Array.new(n, 0) } end ### 平方阶(递归实现)### @@ -57,8 +57,8 @@ def quadratic_recur(n) return 0 unless n > 0 # 数组 nums 长度为 n, n-1, ..., 2, 1 - nums = Array.new n, 0 - quadratic_recur n - 1 + nums = Array.new(n, 0) + quadratic_recur(n - 1) end ### 指数阶(建立满二叉树)### @@ -66,8 +66,8 @@ def build_tree(n) return if n == 0 TreeNode.new.tap do |root| - root.left = build_tree n - 1 - root.right = build_tree n - 1 + root.left = build_tree(n - 1) + root.right = build_tree(n - 1) end end @@ -76,16 +76,16 @@ end n = 5 # 常数阶 -constant n +constant(n) # 线性阶 -linear n -linear_recur n +linear(n) +linear_recur(n) # 平方阶 -quadratic n -quadratic_recur n +quadratic(n) +quadratic_recur(n) # 指数阶 -root = build_tree n -print_tree root +root = build_tree(n) +print_tree(root) diff --git a/codes/ruby/chapter_computational_complexity/time_complexity.rb b/codes/ruby/chapter_computational_complexity/time_complexity.rb index 105ce9346..49bd8dc86 100644 --- a/codes/ruby/chapter_computational_complexity/time_complexity.rb +++ b/codes/ruby/chapter_computational_complexity/time_complexity.rb @@ -133,32 +133,32 @@ end n = 8 puts "输入数据大小 n = #{n}" -count = constant n +count = constant(n) puts "常数阶的操作数量 = #{count}" -count = linear n +count = linear(n) puts "线性阶的操作数量 = #{count}" -count = array_traversal Array.new n, 0 +count = array_traversal(Array.new(n, 0)) puts "线性阶(遍历数组)的操作数量 = #{count}" -count = quadratic n +count = quadratic(n) puts "平方阶的操作数量 = #{count}" nums = Array.new(n) { |i| n - i } # [n, n-1, ..., 2, 1] -count = bubble_sort nums +count = bubble_sort(nums) puts "平方阶(冒泡排序)的操作数量 = #{count}" -count = exponential n +count = exponential(n) puts "指数阶(循环实现)的操作数量 = #{count}" -count = exp_recur n +count = exp_recur(n) puts "指数阶(递归实现)的操作数量 = #{count}" -count = logarithmic n +count = logarithmic(n) puts "对数阶(循环实现)的操作数量 = #{count}" -count = log_recur n +count = log_recur(n) puts "对数阶(递归实现)的操作数量 = #{count}" -count = linear_log_recur n +count = linear_log_recur(n) puts "线性对数阶(递归实现)的操作数量 = #{count}" -count = factorial_recur n +count = factorial_recur(n) puts "阶乘阶(递归实现)的操作数量 = #{count}" diff --git a/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb b/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb index eef3e3ae5..4610c4012 100644 --- a/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb +++ b/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb @@ -27,8 +27,8 @@ end for i in 0...10 n = 100 - nums = random_number n - index = find_one nums + nums = random_number(n) + index = find_one(nums) puts "\n数组 [ 1, 2, ..., n ] 被打乱后 = #{nums}" puts "数字 1 的索引为 #{index}" end diff --git a/codes/ruby/utils/list_node.rb b/codes/ruby/utils/list_node.rb index de46e67b5..278a81d28 100644 --- a/codes/ruby/utils/list_node.rb +++ b/codes/ruby/utils/list_node.rb @@ -17,10 +17,10 @@ end ### 将列表反序列化为链表 ### def arr_to_linked_list(arr) - head = current = ListNode.new arr[0] + head = current = ListNode.new(arr[0]) for i in 1...arr.length - current.next = ListNode.new arr[i] + current.next = ListNode.new(arr[i]) current = current.next end diff --git a/codes/ruby/utils/print_util.rb b/codes/ruby/utils/print_util.rb index dee5c7bbe..59fa42415 100644 --- a/codes/ruby/utils/print_util.rb +++ b/codes/ruby/utils/print_util.rb @@ -37,8 +37,8 @@ def print_tree(root, prev=nil, is_right=false) return if root.nil? prev_str = " " - trunk = Trunk.new prev, prev_str - print_tree root.right, trunk, true + trunk = Trunk.new(prev, prev_str) + print_tree(root.right, trunk, true) if prev.nil? trunk.str = "———" @@ -50,9 +50,9 @@ def print_tree(root, prev=nil, is_right=false) prev.str = prev_str end - show_trunk trunk + show_trunk(trunk) puts " #{root.val}" prev.str = prev_str if prev trunk.str = " |" - print_tree root.left, trunk, false + print_tree(root.left, trunk, false) end diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index c08b556f2..03bf5f917 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -421,11 +421,11 @@ ```ruby title="linked_list.rb" # 初始化链表 1 -> 3 -> 2 -> 5 -> 4 # 初始化各个节点 - n0 = ListNode.new 1 - n1 = ListNode.new 3 - n2 = ListNode.new 2 - n3 = ListNode.new 5 - n4 = ListNode.new 4 + n0 = ListNode.new(1) + n1 = ListNode.new(3) + n2 = ListNode.new(2) + n3 = ListNode.new(5) + n4 = ListNode.new(4) # 构建节点之间的引用 n0.next = n1 n1.next = n2 diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index a6c1193f9..10cb41275 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -545,10 +545,10 @@ nums << 4 # 在中间插入元素 - nums.insert 3, 6 # 在索引 3 处插入数字 6 + nums.insert(3, 6) # 在索引 3 处插入数字 6 # 删除元素 - nums.delete_at 3 # 删除索引 3 处的元素 + nums.delete_at(3) # 删除索引 3 处的元素 ``` === "Zig" diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index d2665ae6b..7ccf2cda5 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -355,7 +355,7 @@ def algorithm(n) # 输入数据 a = 0 # 暂存数据(常量) b = 0 # 暂存数据(变量) - node = Node.new 0 # 暂存数据(对象) + node = Node.new(0) # 暂存数据(对象) c = function # 栈帧空间(调用函数) a + b + c # 输出数据 end @@ -524,8 +524,8 @@ ```ruby title="" def algorithm(n) a = 0 # O(1) - b = Array.new 10000 # O(1) - nums = Array.new n if n > 10 # O(n) + b = Array.new(10000) # O(1) + nums = Array.new(n) if n > 10 # O(n) end ``` @@ -803,7 +803,7 @@ ### 递归的空间复杂度为 O(n) ### def recur(n) return if n == 1 - recur n - 1 + recur(n - 1) end ```