Add Ruby code blocks to the documents (#1200)

* Add Ruby code blocks to documents

* Remove Ruby code from en/docs
This commit is contained in:
Yudong Jin
2024-03-31 03:57:11 +08:00
committed by GitHub
parent 034ee65e9a
commit 57bdfd6284
21 changed files with 259 additions and 255 deletions

View File

@@ -113,14 +113,6 @@
var nums = intArrayOf(1, 3, 2, 5, 4)
```
=== "Zig"
```zig title="array.zig"
// 初始化数组
var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }
var nums = [_]i32{ 1, 3, 2, 5, 4 };
```
=== "Ruby"
```ruby title="array.rb"
@@ -129,6 +121,14 @@
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
```zig title="array.zig"
// 初始化数组
var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }
var nums = [_]i32{ 1, 3, 2, 5, 4 };
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0Aarr%20%3D%20%5B0%5D%20*%205%20%20%23%20%5B%200,%200,%200,%200,%200%20%5D%0Anums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -170,6 +170,21 @@
}
```
=== "Ruby"
```ruby title=""
# 链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向下一节点的引用
def initialize(val=nil, next_node=nil)
@val = val || 0
@next = next_node
end
end
```
=== "Zig"
```zig title=""
@@ -190,21 +205,6 @@
}
```
=== "Ruby"
```ruby title=""
# 链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向下一节点的引用
def initialize(val=nil, next_node=nil)
@val = val || 0
@next = next_node
end
end
```
## 链表常用操作
### 初始化链表
@@ -416,6 +416,23 @@
n3.next = n4;
```
=== "Ruby"
```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.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
```
=== "Zig"
```zig title="linked_list.zig"
@@ -433,23 +450,6 @@
n3.next = &n4;
```
=== "Ruby"
```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.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%201%20-%3E%203%20-%3E%202%20-%3E%205%20-%3E%204%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@@ -700,6 +700,23 @@
}
```
=== "Ruby"
```ruby title=""
# 双向链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向后继节点的引用
attr_accessor :prev # 指向前驱节点的引用
def initialize(val=nil, next_node=nil, prev_node=nil)
@val = val || 0
@next = next_node
@prev = prev_node
end
end
```
=== "Zig"
```zig title=""
@@ -722,23 +739,6 @@
}
```
=== "Ruby"
```ruby title=""
# 双向链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向后继节点的引用
attr_accessor :prev # 指向前驱节点的引用
def initialize(val=nil, next_node=nil, prev_node=nil)
@val = val || 0
@next = next_node
@prev = prev_node
end
end
```
![常见链表种类](linked_list.assets/linkedlist_common_types.png)
## 链表典型应用

View File

@@ -137,15 +137,6 @@
var nums = numbers.toMutableList()
```
=== "Zig"
```zig title="list.zig"
// 初始化列表
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
```
=== "Ruby"
```ruby title="list.rb"
@@ -156,6 +147,15 @@
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
```zig title="list.zig"
// 初始化列表
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20%23%20%E6%97%A0%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=4&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@@ -278,6 +278,15 @@
nums[1] = 0 // 将索引 1 处的元素更新为 0
```
=== "Ruby"
```ruby title="list.rb"
# 访问元素
num = nums[1]
# 更新元素
nums[1] = 0
```
=== "Zig"
```zig title="list.zig"
@@ -288,15 +297,6 @@
nums.items[1] = 0; // 将索引 1 处的元素更新为 0
```
=== "Ruby"
```ruby title="list.rb"
# 访问元素
num = nums[1]
# 更新元素
nums[1] = 0
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%E8%AE%BF%E9%97%AE%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%0A%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%E6%9B%B4%E6%96%B0%E4%B8%BA%200&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@@ -531,26 +531,6 @@
nums.remove(3); // 删除索引 3 处的元素
```
=== "Zig"
```zig title="list.zig"
// 清空列表
nums.clearRetainingCapacity();
// 在尾部添加元素
try nums.append(1);
try nums.append(3);
try nums.append(2);
try nums.append(5);
try nums.append(4);
// 在中间插入元素
try nums.insert(3, 6); // 在索引 3 处插入数字 6
// 删除元素
_ = nums.orderedRemove(3); // 删除索引 3 处的元素
```
=== "Ruby"
```ruby title="list.rb"
@@ -571,6 +551,26 @@
nums.delete_at 3
```
=== "Zig"
```zig title="list.zig"
// 清空列表
nums.clearRetainingCapacity();
// 在尾部添加元素
try nums.append(1);
try nums.append(3);
try nums.append(2);
try nums.append(5);
try nums.append(4);
// 在中间插入元素
try nums.insert(3, 6); // 在索引 3 处插入数字 6
// 删除元素
_ = nums.orderedRemove(3); // 删除索引 3 处的元素
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B8%85%E7%A9%BA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.insert%283,%206%29%20%20%23%20%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@@ -756,6 +756,22 @@
}
```
=== "Ruby"
```ruby title="list.rb"
# 通过索引遍历列表
count = 0
for i in 0...nums.length
count += nums[i]
end
# 直接遍历列表元素
count = 0
for num in nums
count += num
end
```
=== "Zig"
```zig title="list.zig"
@@ -773,22 +789,6 @@
}
```
=== "Ruby"
```ruby title="list.rb"
# 通过索引遍历列表
count = 0
for i in 0...nums.length
count += nums[i]
end
# 直接遍历列表元素
count = 0
for num in nums
count += num
end
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@@ -892,6 +892,14 @@
nums.addAll(nums1) // 将列表 nums1 拼接到 nums 之后
```
=== "Ruby"
```ruby title="list.rb"
# 拼接两个列表
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
=== "Zig"
```zig title="list.zig"
@@ -902,14 +910,6 @@
try nums.insertSlice(nums.items.len, nums1.items); // 将列表 nums1 拼接到 nums 之后
```
=== "Ruby"
```ruby title="list.rb"
# 拼接两个列表
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8B%BC%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums1%20%3D%20%5B6,%208,%207,%2010,%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%E5%B0%86%E5%88%97%E8%A1%A8%20nums1%20%E6%8B%BC%E6%8E%A5%E5%88%B0%20nums%20%E4%B9%8B%E5%90%8E&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@@ -1001,13 +1001,6 @@
nums.sort() // 排序后,列表元素从小到大排列
```
=== "Zig"
```zig title="list.zig"
// 排序列表
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
```
=== "Ruby"
```ruby title="list.rb"
@@ -1015,6 +1008,13 @@
nums = nums.sort { |a, b| a <=> b }
```
=== "Zig"
```zig title="list.zig"
// 排序列表
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8E%92%E5%BA%8F%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E6%8E%92%E5%BA%8F%E5%90%8E%EF%BC%8C%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%88%97&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -403,6 +403,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -332,6 +332,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@@ -490,6 +496,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@@ -748,6 +760,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -186,6 +186,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@@ -465,6 +471,12 @@ $$
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@@ -673,6 +685,12 @@ $$
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@@ -949,6 +967,12 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -163,6 +163,12 @@
val bools = BooleanArray(5)
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -371,6 +371,12 @@ $$
// 节点对象 utils.ListNode@1d81eb93 的哈希值为 495053715
```
=== "Ruby"
```ruby title="built_in_hash.rb"
```
=== "Zig"
```zig title="built_in_hash.zig"

View File

@@ -290,6 +290,12 @@
map.remove(10583)
```
=== "Ruby"
```ruby title="hash_map.rb"
```
=== "Zig"
```zig title="hash_map.zig"
@@ -503,6 +509,12 @@
}
```
=== "Ruby"
```ruby title="hash_map.rb"
```
=== "Zig"
```zig title="hash_map.zig"

View File

@@ -402,6 +402,12 @@
minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))
```
=== "Ruby"
```ruby title="heap.rb"
```
=== "Zig"
```zig title="heap.zig"

View File

@@ -169,6 +169,12 @@
*/
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -352,6 +352,12 @@
val isEmpty = deque.isEmpty()
```
=== "Ruby"
```ruby title="deque.rb"
```
=== "Zig"
```zig title="deque.zig"

View File

@@ -328,6 +328,12 @@
val isEmpty = queue.isEmpty()
```
=== "Ruby"
```ruby title="queue.rb"
```
=== "Zig"
```zig title="queue.zig"

View File

@@ -322,6 +322,12 @@
val isEmpty = stack.isEmpty()
```
=== "Ruby"
```ruby title="stack.rb"
```
=== "Zig"
```zig title="stack.zig"

View File

@@ -120,6 +120,12 @@
val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -211,6 +211,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View File

@@ -186,6 +186,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@@ -423,6 +429,12 @@
n2.right = n5
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
=== "Zig"
```zig title="binary_tree.zig"
@@ -579,6 +591,12 @@
n1.left = n2
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
=== "Zig"
```zig title="binary_tree.zig"