mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-30 11:57:13 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
=begin
|
||||
File: list_node.rb
|
||||
Created Time: 2024-03-18
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Linked list node class ###
|
||||
class ListNode
|
||||
attr_accessor :val # Node value
|
||||
attr_accessor :next # Reference to next node
|
||||
|
||||
def initialize(val=0, next_node=nil)
|
||||
@val = val
|
||||
@next = next_node
|
||||
end
|
||||
end
|
||||
|
||||
### Deserialize list to linked list ###
|
||||
def arr_to_linked_list(arr)
|
||||
head = current = ListNode.new(arr[0])
|
||||
|
||||
for i in 1...arr.length
|
||||
current.next = ListNode.new(arr[i])
|
||||
current = current.next
|
||||
end
|
||||
|
||||
head
|
||||
end
|
||||
|
||||
### Serialize linked list to list ###
|
||||
def linked_list_to_arr(head)
|
||||
arr = []
|
||||
|
||||
while head
|
||||
arr << head.val
|
||||
head = head.next
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,80 @@
|
||||
=begin
|
||||
File: print_util.rb
|
||||
Created Time: 2024-03-18
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative "./tree_node"
|
||||
|
||||
### Print matrix ###
|
||||
def print_matrix(mat)
|
||||
s = []
|
||||
mat.each { |arr| s << " #{arr.to_s}" }
|
||||
puts "[\n#{s.join(",\n")}\n]"
|
||||
end
|
||||
|
||||
### Print linked list ###
|
||||
def print_linked_list(head)
|
||||
list = []
|
||||
while head
|
||||
list << head.val
|
||||
head = head.next
|
||||
end
|
||||
puts "#{list.join(" -> ")}"
|
||||
end
|
||||
|
||||
class Trunk
|
||||
attr_accessor :prev, :str
|
||||
|
||||
def initialize(prev, str)
|
||||
@prev = prev
|
||||
@str = str
|
||||
end
|
||||
end
|
||||
|
||||
def show_trunk(p)
|
||||
return if p.nil?
|
||||
|
||||
show_trunk(p.prev)
|
||||
print p.str
|
||||
end
|
||||
|
||||
### Print binary tree ###
|
||||
# This tree printer is borrowed from TECHIE DELIGHT
|
||||
# https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
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)
|
||||
|
||||
if prev.nil?
|
||||
trunk.str = "———"
|
||||
elsif is_right
|
||||
trunk.str = "/———"
|
||||
prev_str = " |"
|
||||
else
|
||||
trunk.str = "\\———"
|
||||
prev.str = prev_str
|
||||
end
|
||||
|
||||
show_trunk(trunk)
|
||||
puts " #{root.val}"
|
||||
prev.str = prev_str if prev
|
||||
trunk.str = " |"
|
||||
print_tree(root.left, trunk, false)
|
||||
end
|
||||
|
||||
### Print hash table ###
|
||||
def print_hash_map(hmap)
|
||||
hmap.entries.each { |key, value| puts "#{key} -> #{value}" }
|
||||
end
|
||||
|
||||
### Print heap ###
|
||||
def print_heap(heap)
|
||||
puts "Array representation of heap: #{heap}"
|
||||
puts "Heap tree representation:"
|
||||
root = arr_to_tree(heap)
|
||||
print_tree(root)
|
||||
end
|
||||
@@ -0,0 +1,53 @@
|
||||
=begin
|
||||
File: tree_node.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Binary tree node class ###
|
||||
class TreeNode
|
||||
attr_accessor :val # Node value
|
||||
attr_accessor :height # Node height
|
||||
attr_accessor :left # Reference to left child node
|
||||
attr_accessor :right # Reference to right child node
|
||||
|
||||
def initialize(val=0)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
|
||||
### Deserialize list to binary tree: recursion ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
# Return nil if index exceeds array length or element is nil
|
||||
return if i < 0 || i >= arr.length || arr[i].nil?
|
||||
# Build the current node
|
||||
root = TreeNode.new(arr[i])
|
||||
# Recursively build the left and right subtrees
|
||||
root.left = arr_to_tree_dfs(arr, 2 * i + 1)
|
||||
root.right = arr_to_tree_dfs(arr, 2 * i + 2)
|
||||
root
|
||||
end
|
||||
|
||||
### Deserialize list to binary tree ###
|
||||
def arr_to_tree(arr)
|
||||
arr_to_tree_dfs(arr, 0)
|
||||
end
|
||||
|
||||
### Serialize binary tree to list: recursion ###
|
||||
def tree_to_arr_dfs(root, i, res)
|
||||
return if root.nil?
|
||||
|
||||
res += Array.new(i - res.length + 1) if i >= res.length
|
||||
res[i] = root.val
|
||||
|
||||
tree_to_arr_dfs(root.left, 2 * i + 1, res)
|
||||
tree_to_arr_dfs(root.right, 2 * i + 2, res)
|
||||
end
|
||||
|
||||
### Serialize binary tree to list ###
|
||||
def tree_to_arr(root)
|
||||
res = []
|
||||
tree_to_arr_dfs(root, 0, res)
|
||||
res
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
=begin
|
||||
File: vertex.rb
|
||||
Created Time: 2024-04-25
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Vertex class ###
|
||||
class Vertex
|
||||
attr_accessor :val
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
end
|
||||
end
|
||||
|
||||
### Input value list vals, return vertex list vets ###
|
||||
def vals_to_vets(vals)
|
||||
Array.new(vals.length) { |i| Vertex.new(vals[i]) }
|
||||
end
|
||||
|
||||
### Input vertex list vets, return value list vals ###
|
||||
def vets_to_vals(vets)
|
||||
Array.new(vets.length) { |i| vets[i].val }
|
||||
end
|
||||
Reference in New Issue
Block a user