Skip to content

Commit 9a4d483

Browse files
committed
Fix Ruby comments
1 parent 72855d8 commit 9a4d483

77 files changed

Lines changed: 329 additions & 329 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.

en/codes/ruby/chapter_array_and_linkedlist/array.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
55
=end
66

7-
# ### Random access element ###
7+
### Random access element ###
88
def random_access(nums)
99
# Randomly select a number in the interval [0, nums.length)
1010
random_index = Random.rand(0...nums.length)
@@ -14,7 +14,7 @@ def random_access(nums)
1414
end
1515

1616

17-
# ### Extend array length ###
17+
### Extend array length ###
1818
# Note: Ruby's Array is dynamic array, can be directly expanded
1919
# For learning purposes, this function treats Array as fixed-length array
2020
def extend(nums, enlarge)
@@ -30,7 +30,7 @@ def extend(nums, enlarge)
3030
res
3131
end
3232

33-
# ### Insert element num at index in array ###
33+
### Insert element num at index in array ###
3434
def insert(nums, num, index)
3535
# Move all elements at and after index index backward by one position
3636
for i in (nums.length - 1).downto(index + 1)
@@ -42,15 +42,15 @@ def insert(nums, num, index)
4242
end
4343

4444

45-
# ### Delete element at index ###
45+
### Delete element at index ###
4646
def remove(nums, index)
4747
# Move all elements after index index forward by one position
4848
for i in index...(nums.length - 1)
4949
nums[i] = nums[i + 1]
5050
end
5151
end
5252

53-
# ### Traverse array ###
53+
### Traverse array ###
5454
def traverse(nums)
5555
count = 0
5656

@@ -65,7 +65,7 @@ def traverse(nums)
6565
end
6666
end
6767

68-
# ### Find specified element in array ###
68+
### Find specified element in array ###
6969
def find(nums, target)
7070
for i in 0...nums.length
7171
return i if nums[i] == target

en/codes/ruby/chapter_array_and_linkedlist/linked_list.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
require_relative '../utils/list_node'
88
require_relative '../utils/print_util'
99

10-
# ### Insert node _p after node n0 in linked list ###
10+
### Insert node _p after node n0 in linked list ###
1111
# Ruby's `p` is a built-in function, `P` is a constant, so use `_p` instead
1212
def insert(n0, _p)
1313
n1 = n0.next
1414
_p.next = n1
1515
n0.next = _p
1616
end
1717

18-
# ### Delete first node after node n0 in linked list ###
18+
### Delete first node after node n0 in linked list ###
1919
def remove(n0)
2020
return if n0.next.nil?
2121

@@ -25,7 +25,7 @@ def remove(n0)
2525
n0.next = n1
2626
end
2727

28-
# ### Access node at index in linked list ###
28+
### Access node at index in linked list ###
2929
def access(head, index)
3030
for i in 0...index
3131
return nil if head.nil?
@@ -35,7 +35,7 @@ def access(head, index)
3535
head
3636
end
3737

38-
# ### Find first node with value target in linked list ###
38+
### Find first node with value target in linked list ###
3939
def find(head, target)
4040
index = 0
4141
while head

en/codes/ruby/chapter_array_and_linkedlist/my_list.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@
44
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
55
=end
66

7-
# ### List class ###
7+
### List class ###
88
class MyList
99
attr_reader :size # Get list length (current number of elements)
1010
attr_reader :capacity # Get list capacity
1111

12-
# ### Constructor ###
12+
### Constructor ###
1313
def initialize
1414
@capacity = 10
1515
@size = 0
1616
@extend_ratio = 2
1717
@arr = Array.new(capacity)
1818
end
1919

20-
# ### Access element ###
20+
### Access element ###
2121
def get(index)
2222
# If the index is out of bounds, throw an exception, as below
2323
raise IndexError, "Index out of bounds" if index < 0 || index >= size
2424
@arr[index]
2525
end
2626

27-
# ### Access element ###
27+
### Access element ###
2828
def set(index, num)
2929
raise IndexError, "Index out of bounds" if index < 0 || index >= size
3030
@arr[index] = num
3131
end
3232

33-
# ### Add element at end ###
33+
### Add element at end ###
3434
def add(num)
3535
# When the number of elements exceeds capacity, trigger the extension mechanism
3636
extend_capacity if size == capacity
@@ -40,7 +40,7 @@ def add(num)
4040
@size += 1
4141
end
4242

43-
# ### Insert element in middle ###
43+
### Insert element in middle ###
4444
def insert(index, num)
4545
raise IndexError, "Index out of bounds" if index < 0 || index >= size
4646

@@ -57,7 +57,7 @@ def insert(index, num)
5757
@size += 1
5858
end
5959

60-
# ### Delete element ###
60+
### Delete element ###
6161
def remove(index)
6262
raise IndexError, "Index out of bounds" if index < 0 || index >= size
6363
num = @arr[index]
@@ -74,15 +74,15 @@ def remove(index)
7474
num
7575
end
7676

77-
# ### Expand list capacity ###
77+
### Expand list capacity ###
7878
def extend_capacity
7979
# Create new array with length extend_ratio times original, copy original array to new array
8080
arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))
8181
# Add elements at the end
8282
@capacity = arr.length
8383
end
8484

85-
# ### Convert list to array ###
85+
### Convert list to array ###
8686
def to_array
8787
sz = size
8888
# Elements enqueue

en/codes/ruby/chapter_backtracking/n_queens.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
55
=end
66

7-
# ### Backtracking: n queens ###
7+
### Backtracking: n queens ###
88
def backtrack(row, n, state, res, cols, diags1, diags2)
99
# When all rows are placed, record the solution
1010
if row == n
@@ -31,7 +31,7 @@ def backtrack(row, n, state, res, cols, diags1, diags2)
3131
end
3232
end
3333

34-
# ### Solve n queens ###
34+
### Solve n queens ###
3535
def n_queens(n)
3636
# Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
3737
state = Array.new(n) { Array.new(n, "#") }

en/codes/ruby/chapter_backtracking/permutations_i.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
55
=end
66

7-
# ### Backtracking: permutations I ###
7+
### Backtracking: permutations I ###
88
def backtrack(state, choices, selected, res)
99
# When the state length equals the number of elements, record the solution
1010
if state.length == choices.length
@@ -28,7 +28,7 @@ def backtrack(state, choices, selected, res)
2828
end
2929
end
3030

31-
# ### Permutations I ###
31+
### Permutations I ###
3232
def permutations_i(nums)
3333
res = []
3434
backtrack([], nums, Array.new(nums.length, false), res)

en/codes/ruby/chapter_backtracking/permutations_ii.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
55
=end
66

7-
# ### Backtracking: permutations II ###
7+
### Backtracking: permutations II ###
88
def backtrack(state, choices, selected, res)
99
# When the state length equals the number of elements, record the solution
1010
if state.length == choices.length
@@ -30,7 +30,7 @@ def backtrack(state, choices, selected, res)
3030
end
3131
end
3232

33-
# ### Permutations II ###
33+
### Permutations II ###
3434
def permutations_ii(nums)
3535
res = []
3636
backtrack([], nums, Array.new(nums.length, false), res)

en/codes/ruby/chapter_backtracking/preorder_traversal_i_compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require_relative '../utils/tree_node'
88
require_relative '../utils/print_util'
99

10-
# ### Pre-order traversal: example 1 ###
10+
### Pre-order traversal: example 1 ###
1111
def pre_order(root)
1212
return unless root
1313

en/codes/ruby/chapter_backtracking/preorder_traversal_ii_compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require_relative '../utils/tree_node'
88
require_relative '../utils/print_util'
99

10-
# ### Pre-order traversal: example 2 ###
10+
### Pre-order traversal: example 2 ###
1111
def pre_order(root)
1212
return unless root
1313

en/codes/ruby/chapter_backtracking/preorder_traversal_iii_compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require_relative '../utils/tree_node'
88
require_relative '../utils/print_util'
99

10-
# ### Pre-order traversal: example 3 ###
10+
### Pre-order traversal: example 3 ###
1111
def pre_order(root)
1212
# Pruning
1313
return if !root || root.val == 3

en/codes/ruby/chapter_backtracking/preorder_traversal_iii_template.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@
77
require_relative '../utils/tree_node'
88
require_relative '../utils/print_util'
99

10-
# ### Check if current state is solution ###
10+
### Check if current state is solution ###
1111
def is_solution?(state)
1212
!state.empty? && state.last.val == 7
1313
end
1414

15-
# ### Record solution ###
15+
### Record solution ###
1616
def record_solution(state, res)
1717
res << state.dup
1818
end
1919

20-
# ### Check if choice is valid in current state ###
20+
### Check if choice is valid in current state ###
2121
def is_valid?(state, choice)
2222
choice && choice.val != 3
2323
end
2424

25-
# ### Update state ###
25+
### Update state ###
2626
def make_choice(state, choice)
2727
state << choice
2828
end
2929

30-
# ### Restore state ###
30+
### Restore state ###
3131
def undo_choice(state, choice)
3232
state.pop
3333
end
3434

35-
# ### Backtracking: example 3 ###
35+
### Backtracking: example 3 ###
3636
def backtrack(state, choices, res)
3737
# Check if it is a solution
3838
record_solution(state, res) if is_solution?(state)

0 commit comments

Comments
 (0)