mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
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:
@ -119,14 +119,6 @@ Arrays can be initialized in two ways depending on the needs: either without ini
|
||||
var nums = [_]i32{ 1, 3, 2, 5, 4 };
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
# Initialize array
|
||||
arr = Array.new(5, 0) # [ 0, 0, 0, 0, 0 ]
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
```
|
||||
|
||||
### Accessing Elements
|
||||
|
||||
Elements in an array are stored in contiguous memory spaces, making it simpler to compute each element's memory address. The formula shown in the Figure below aids in determining an element's memory address, utilizing the array's memory address (specifically, the first element's address) and the element's index. This computation streamlines direct access to the desired element.
|
||||
|
||||
@ -185,22 +185,6 @@ As the code below illustrates, a `ListNode` in a linked list, besides holding a
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
# Linked List Node Class
|
||||
class ListNode
|
||||
attr_accessor :val # Node value
|
||||
attr_accessor :next # Reference to the next node
|
||||
|
||||
def initialize(val=nil, next_node=nil)
|
||||
@val = val || 0
|
||||
@next = next_node
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
## Common Operations on Linked Lists
|
||||
|
||||
### Initializing a Linked List
|
||||
@ -418,23 +402,6 @@ Constructing a linked list is a two-step process: first, initializing each node
|
||||
n3.next = &n4;
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=linked_list.rb
|
||||
# Initialize linked list 1 -> 3 -> 2 -> 5 -> 4
|
||||
# Initialize each node
|
||||
n0 = ListNode.new 1
|
||||
n1 = ListNode.new 3
|
||||
n2 = ListNode.new 2
|
||||
n3 = ListNode.new 5
|
||||
n4 = ListNode.new 4
|
||||
# Build references between nodes
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
```
|
||||
|
||||
The array as a whole is a variable, for instance, the array `nums` includes elements like `nums[0]`, `nums[1]`, and so on, whereas a linked list is made up of several distinct node objects. **We typically refer to a linked list by its head node**, for example, the linked list in the previous code snippet is referred to as `n0`.
|
||||
|
||||
### Inserting a Node
|
||||
@ -697,23 +664,6 @@ As shown in the figure, there are three common types of linked lists.
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
# Bidirectional linked list node class
|
||||
class ListNode
|
||||
attr_accessor :val # Node value
|
||||
attr_accessor :next # Reference to the successor node
|
||||
attr_accessor :prev # Reference to the predecessor node
|
||||
|
||||
def initialize(val=nil, next_node=nil, prev_node=nil)
|
||||
@val = val || 0
|
||||
@next = next_node
|
||||
@prev = prev_node
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||

|
||||
|
||||
## Typical Applications of Linked Lists
|
||||
|
||||
@ -141,16 +141,6 @@ We typically use two initialization methods: "without initial values" and "with
|
||||
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="list.rb"
|
||||
# Initialize list
|
||||
# Without initial values
|
||||
nums1 = []
|
||||
# With initial values
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
```
|
||||
|
||||
### Accessing Elements
|
||||
|
||||
Lists are essentially arrays, thus they can access and update elements in $O(1)$ time, which is very efficient.
|
||||
@ -276,15 +266,6 @@ Lists are essentially arrays, thus they can access and update elements in $O(1)$
|
||||
nums.items[1] = 0; // Update the element at index 1 to 0
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="list.rb"
|
||||
# Access elements
|
||||
num = nums[1]
|
||||
# Update elements
|
||||
nums[1] = 0
|
||||
```
|
||||
|
||||
### Inserting and Removing Elements
|
||||
|
||||
Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an $O(1)$ operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of $O(n)$.
|
||||
@ -521,26 +502,6 @@ Compared to arrays, lists offer more flexibility in adding and removing elements
|
||||
_ = nums.orderedRemove(3); // Remove the element at index 3
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="list.rb"
|
||||
# Clear list
|
||||
nums.clear
|
||||
|
||||
# Append elements at the end
|
||||
nums << 1
|
||||
nums << 3
|
||||
nums << 2
|
||||
nums << 5
|
||||
nums << 4
|
||||
|
||||
# Insert element in the middle
|
||||
nums.insert 3, 6 # Insert number 6 at index 3
|
||||
|
||||
# Remove elements
|
||||
nums.delete_at 3 # Remove the element at index 3
|
||||
```
|
||||
|
||||
### Iterating the List
|
||||
|
||||
Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element.
|
||||
@ -730,22 +691,6 @@ Similar to arrays, lists can be iterated either by using indices or by directly
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="list.rb"
|
||||
# Iterate through the list by index
|
||||
count = 0
|
||||
for i in 0...nums.length
|
||||
count += nums[i]
|
||||
end
|
||||
|
||||
# Iterate directly though list elements
|
||||
count = 0
|
||||
for num in nums
|
||||
count += num
|
||||
end
|
||||
```
|
||||
|
||||
### Concatenating Lists
|
||||
|
||||
Given a new list `nums1`, we can append it to the end of the original list.
|
||||
@ -853,14 +798,6 @@ Given a new list `nums1`, we can append it to the end of the original list.
|
||||
try nums.insertSlice(nums.items.len, nums1.items); // Concatenate nums1 to the end of nums
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="list.rb"
|
||||
# Concatenate two lists
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
```
|
||||
|
||||
### Sorting the List
|
||||
|
||||
Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as "binary search" and "two-pointer" algorithms.
|
||||
@ -954,13 +891,6 @@ Once the list is sorted, we can employ algorithms commonly used in array-related
|
||||
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="list.rb"
|
||||
# Sort the list
|
||||
nums = nums.sort { |a, b| a <=> b }
|
||||
```
|
||||
|
||||
## List Implementation
|
||||
|
||||
Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning.
|
||||
|
||||
Reference in New Issue
Block a user