Skip to content

Commit 82668e7

Browse files
committed
Add the EN version of Ruby code.
1 parent 0acf4c3 commit 82668e7

85 files changed

Lines changed: 5924 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
=begin
2+
File: array.rb
3+
Created Time: 2024-03-18
4+
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
5+
=end
6+
7+
# ### Random access element ###
8+
def random_access(nums)
9+
# Randomly select a number in the interval [0, nums.length)
10+
random_index = Random.rand(0...nums.length)
11+
12+
# Retrieve and return the random element
13+
nums[random_index]
14+
end
15+
16+
17+
# ### Extend array length ###
18+
# Note: Ruby's Array is dynamic array, can be directly expanded
19+
# For learning purposes, this function treats Array as fixed-length array
20+
def extend(nums, enlarge)
21+
# Initialize an array with extended length
22+
res = Array.new(nums.length + enlarge, 0)
23+
24+
# Copy all elements from the original array to the new array
25+
for i in 0...nums.length
26+
res[i] = nums[i]
27+
end
28+
29+
# Return the extended new array
30+
res
31+
end
32+
33+
# ### Insert element num at index in array ###
34+
def insert(nums, num, index)
35+
# Move all elements at and after index index backward by one position
36+
for i in (nums.length - 1).downto(index + 1)
37+
nums[i] = nums[i - 1]
38+
end
39+
40+
# Assign num to the element at index index
41+
nums[index] = num
42+
end
43+
44+
45+
# ### Delete element at index ###
46+
def remove(nums, index)
47+
# Move all elements after index index forward by one position
48+
for i in index...(nums.length - 1)
49+
nums[i] = nums[i + 1]
50+
end
51+
end
52+
53+
# ### Traverse array ###
54+
def traverse(nums)
55+
count = 0
56+
57+
# Traverse array by index
58+
for i in 0...nums.length
59+
count += nums[i]
60+
end
61+
62+
# Direct traversal of array elements
63+
for num in nums
64+
count += num
65+
end
66+
end
67+
68+
# ### Find specified element in array ###
69+
def find(nums, target)
70+
for i in 0...nums.length
71+
return i if nums[i] == target
72+
end
73+
74+
-1
75+
end
76+
77+
78+
### Driver Code ###
79+
if __FILE__ == $0
80+
# Initialize array
81+
arr = Array.new(5, 0)
82+
puts "Array arr = #{arr}"
83+
nums = [1, 3, 2, 5, 4]
84+
puts "Array nums = #{nums}"
85+
86+
# Insert element
87+
random_num = random_access(nums)
88+
puts "Get random element #{random_num} from nums"
89+
90+
# Traverse array
91+
nums = extend(nums, 3)
92+
puts "Extend array length to 8, get nums = #{nums}"
93+
94+
# Insert element
95+
insert(nums, 6, 3)
96+
puts "Insert number 6 at index 3, get nums = #{nums}"
97+
98+
# Remove element
99+
remove(nums, 2)
100+
puts "Delete element at index 2, get nums = #{nums}"
101+
102+
# Traverse array
103+
traverse(nums)
104+
105+
# Find element
106+
index = find(nums, 3)
107+
puts "Find element 3 in nums, index = #{index}"
108+
end
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
=begin
2+
File: linked_list.rb
3+
Created Time: 2024-03-18
4+
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
5+
=end
6+
7+
require_relative '../utils/list_node'
8+
require_relative '../utils/print_util'
9+
10+
# ### Insert node _p after node n0 in linked list ###
11+
# Ruby's `p` is a built-in function, `P` is a constant, so use `_p` instead
12+
def insert(n0, _p)
13+
n1 = n0.next
14+
_p.next = n1
15+
n0.next = _p
16+
end
17+
18+
# ### Delete first node after node n0 in linked list ###
19+
def remove(n0)
20+
return if n0.next.nil?
21+
22+
# n0 -> remove_node -> n1
23+
remove_node = n0.next
24+
n1 = remove_node.next
25+
n0.next = n1
26+
end
27+
28+
# ### Access node at index in linked list ###
29+
def access(head, index)
30+
for i in 0...index
31+
return nil if head.nil?
32+
head = head.next
33+
end
34+
35+
head
36+
end
37+
38+
# ### Find first node with value target in linked list ###
39+
def find(head, target)
40+
index = 0
41+
while head
42+
return index if head.val == target
43+
head = head.next
44+
index += 1
45+
end
46+
47+
-1
48+
end
49+
50+
### Driver Code ###
51+
if __FILE__ == $0
52+
# Initialize linked list
53+
# Initialize each node
54+
n0 = ListNode.new(1)
55+
n1 = ListNode.new(3)
56+
n2 = ListNode.new(2)
57+
n3 = ListNode.new(5)
58+
n4 = ListNode.new(4)
59+
# Build references between nodes
60+
n0.next = n1
61+
n1.next = n2
62+
n2.next = n3
63+
n3.next = n4
64+
puts "Initialized linked list is"
65+
print_linked_list(n0)
66+
67+
# Insert node
68+
insert(n0, ListNode.new(0))
69+
print_linked_list n0
70+
71+
# Remove node
72+
remove(n0)
73+
puts "Linked list after removing node is"
74+
print_linked_list(n0)
75+
76+
# Access node
77+
node = access(n0, 3)
78+
puts "Value of node at index 3 in linked list = #{node.val}"
79+
80+
# Search node
81+
index = find(n0, 2)
82+
puts "Index of node with value 2 in linked list = #{index}"
83+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
=begin
2+
File: list.rb
3+
Created Time: 2024-03-18
4+
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
5+
=end
6+
7+
### Driver Code ###
8+
if __FILE__ == $0
9+
# Initialize list
10+
nums = [1, 3, 2, 5, 4]
11+
puts "List nums = #{nums}"
12+
13+
# Update element
14+
num = nums[1]
15+
puts "Access element at index 1, get num = #{num}"
16+
17+
# Add elements at the end
18+
nums[1] = 0
19+
puts "Update element at index 1 to 0, get nums = #{nums}"
20+
21+
# Remove element
22+
nums.clear
23+
puts "After clearing list, nums = #{nums}"
24+
25+
# Direct traversal of list elements
26+
nums << 1
27+
nums << 3
28+
nums << 2
29+
nums << 5
30+
nums << 4
31+
puts "After adding elements, nums = #{nums}"
32+
33+
# Sort list
34+
nums.insert(3, 6)
35+
puts "Insert element 6 at index 3, get nums = #{nums}"
36+
37+
# Remove element
38+
nums.delete_at(3)
39+
puts "Delete element at index 3, get nums = #{nums}"
40+
41+
# Traverse list by index
42+
count = 0
43+
for i in 0...nums.length
44+
count += nums[i]
45+
end
46+
47+
# Directly traverse list elements
48+
count = 0
49+
nums.each do |x|
50+
count += x
51+
end
52+
53+
# Concatenate two lists
54+
nums1 = [6, 8, 7, 10, 9]
55+
nums += nums1
56+
puts "After concatenating list nums1 to nums, get nums = #{nums}"
57+
58+
nums = nums.sort { |a, b| a <=> b }
59+
puts "After sorting list, nums = #{nums}"
60+
end
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
=begin
2+
File: my_list.rb
3+
Created Time: 2024-03-18
4+
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
5+
=end
6+
7+
# ### List class ###
8+
class MyList
9+
attr_reader :size # Get list length (current number of elements)
10+
attr_reader :capacity # Get list capacity
11+
12+
# ### Constructor ###
13+
def initialize
14+
@capacity = 10
15+
@size = 0
16+
@extend_ratio = 2
17+
@arr = Array.new(capacity)
18+
end
19+
20+
# ### Access element ###
21+
def get(index)
22+
# If the index is out of bounds, throw an exception, as below
23+
raise IndexError, "Index out of bounds" if index < 0 || index >= size
24+
@arr[index]
25+
end
26+
27+
# ### Access element ###
28+
def set(index, num)
29+
raise IndexError, "Index out of bounds" if index < 0 || index >= size
30+
@arr[index] = num
31+
end
32+
33+
# ### Add element at end ###
34+
def add(num)
35+
# When the number of elements exceeds capacity, trigger the extension mechanism
36+
extend_capacity if size == capacity
37+
@arr[size] = num
38+
39+
# Update the number of elements
40+
@size += 1
41+
end
42+
43+
# ### Insert element in middle ###
44+
def insert(index, num)
45+
raise IndexError, "Index out of bounds" if index < 0 || index >= size
46+
47+
# When the number of elements exceeds capacity, trigger the extension mechanism
48+
extend_capacity if size == capacity
49+
50+
# Move all elements after index index forward by one position
51+
for j in (size - 1).downto(index)
52+
@arr[j + 1] = @arr[j]
53+
end
54+
@arr[index] = num
55+
56+
# Update the number of elements
57+
@size += 1
58+
end
59+
60+
# ### Delete element ###
61+
def remove(index)
62+
raise IndexError, "Index out of bounds" if index < 0 || index >= size
63+
num = @arr[index]
64+
65+
# Move all elements after index forward by one position
66+
for j in index...size
67+
@arr[j] = @arr[j + 1]
68+
end
69+
70+
# Update the number of elements
71+
@size -= 1
72+
73+
# Return the removed element
74+
num
75+
end
76+
77+
# ### Expand list capacity ###
78+
def extend_capacity
79+
# Create new array with length extend_ratio times original, copy original array to new array
80+
arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))
81+
# Add elements at the end
82+
@capacity = arr.length
83+
end
84+
85+
# ### Convert list to array ###
86+
def to_array
87+
sz = size
88+
# Elements enqueue
89+
arr = Array.new(sz)
90+
for i in 0...sz
91+
arr[i] = get(i)
92+
end
93+
arr
94+
end
95+
end
96+
97+
### Driver Code ###
98+
if __FILE__ == $0
99+
# Initialize list
100+
nums = MyList.new
101+
102+
# Direct traversal of list elements
103+
nums.add(1)
104+
nums.add(3)
105+
nums.add(2)
106+
nums.add(5)
107+
nums.add(4)
108+
puts "List nums = #{nums.to_array}, capacity = #{nums.capacity}, length = #{nums.size}"
109+
110+
# Sort list
111+
nums.insert(3, 6)
112+
puts "Insert number 6 at index 3, get nums = #{nums.to_array}"
113+
114+
# Remove element
115+
nums.remove(3)
116+
puts "Delete element at index 3, get nums = #{nums.to_array}"
117+
118+
# Update element
119+
num = nums.get(1)
120+
puts "Access element at index 1, get num = #{num}"
121+
122+
# Add elements at the end
123+
nums.set(1, 0)
124+
puts "Update element at index 1 to 0, get nums = #{nums.to_array}"
125+
126+
# Test capacity expansion mechanism
127+
for i in 0...10
128+
# At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
129+
nums.add(i)
130+
end
131+
puts "After expansion, list nums = #{nums.to_array}, capacity = #{nums.capacity}, length = #{nums.size}"
132+
end

0 commit comments

Comments
 (0)