Files
hello-algo/en/codes/ruby/utils/print_util.rb
Yudong Jin 2778a6f9c7 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
2025-12-31 07:44:52 +08:00

81 lines
1.5 KiB
Ruby

=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