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:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions

View File

@@ -0,0 +1,121 @@
=begin
File: array_hash_map.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Key-value pair ###
class Pair
attr_accessor :key, :val
def initialize(key, val)
@key = key
@val = val
end
end
### Hash map based on array ###
class ArrayHashMap
### Constructor ###
def initialize
# Initialize array with 100 buckets
@buckets = Array.new(100)
end
### Hash function ###
def hash_func(key)
index = key % 100
end
### Query operation ###
def get(key)
index = hash_func(key)
pair = @buckets[index]
return if pair.nil?
pair.val
end
### Add operation ###
def put(key, val)
pair = Pair.new(key, val)
index = hash_func(key)
@buckets[index] = pair
end
### Delete operation ###
def remove(key)
index = hash_func(key)
# Set to nil to delete
@buckets[index] = nil
end
### Get all key-value pairs ###
def entry_set
result = []
@buckets.each { |pair| result << pair unless pair.nil? }
result
end
### Get all keys ###
def key_set
result = []
@buckets.each { |pair| result << pair.key unless pair.nil? }
result
end
### Get all values ###
def value_set
result = []
@buckets.each { |pair| result << pair.val unless pair.nil? }
result
end
### Print hash table ###
def print
@buckets.each { |pair| puts "#{pair.key} -> #{pair.val}" unless pair.nil? }
end
end
### Driver Code ###
if __FILE__ == $0
# Initialize hash table
hmap = ArrayHashMap.new
# Add operation
# Add key-value pair (key, value) to the hash table
hmap.put(12836, "Xiao Ha")
hmap.put(15937, "Xiao Luo")
hmap.put(16750, "Xiao Suan")
hmap.put(13276, "Xiao Fa")
hmap.put(10583, "Xiao Ya")
puts "\nAfter adding is complete, hash table is\nKey -> Value"
hmap.print
# Query operation
# Input key to hash table, get value
name = hmap.get(15937)
puts "\nInput student ID 15937, found name #{name}"
# Remove operation
# Delete key-value pair (key, value) from hash table
hmap.remove(10583)
puts "\nAfter removing 10583, hash table is\nKey -> Value"
hmap.print
# Traverse hash table
puts "\nTraverse key-value pairs Key->Value"
for pair in hmap.entry_set
puts "#{pair.key} -> #{pair.val}"
end
puts "\nTraverse keys separately"
for key in hmap.key_set
puts key
end
puts "\nTraverse values only Value"
for val in hmap.value_set
puts val
end
end

View File

@@ -0,0 +1,34 @@
=begin
File: built_in_hash.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/list_node'
### Driver Code ###
if __FILE__ == $0
num = 3
hash_num = num.hash
puts "Hash value of integer #{num} is #{hash_num}"
bol = true
hash_bol = bol.hash
puts "Hash value of boolean #{bol} is #{hash_bol}"
dec = 3.14159
hash_dec = dec.hash
puts "Hash value of decimal #{dec} is #{hash_dec}"
str = "Hello Algo"
hash_str = str.hash
puts "Hash value of string #{str} is #{hash_str}"
tup = [12836, 'Xiao Ha']
hash_tup = tup.hash
puts "Hash value of tuple #{tup} is #{hash_tup}"
obj = ListNode.new(0)
hash_obj = obj.hash
puts "Hash value of object #{obj} is #{hash_obj}"
end

View File

@@ -0,0 +1,44 @@
=begin
File: hash_map.rb
Created Time: 2024-04-14
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/print_util'
### Driver Code ###
if __FILE__ == $0
# Initialize hash table
hmap = {}
# Add operation
# Add key-value pair (key, value) to the hash table
hmap[12836] = "Xiao Ha"
hmap[15937] = "Xiao Luo"
hmap[16750] = "Xiao Suan"
hmap[13276] = "Xiao Fa"
hmap[10583] = "Xiao Ya"
puts "\nAfter adding is complete, hash table is\nKey -> Value"
print_hash_map(hmap)
# Query operation
# Input key into hash table to get value
name = hmap[15937]
puts "\nInput student ID 15937, found name #{name}"
# Remove operation
# Remove key-value pair (key, value) from hash table
hmap.delete(10583)
puts "\nAfter removing 10583, hash table is\nKey -> Value"
print_hash_map(hmap)
# Traverse hash table
puts "\nTraverse key-value pairs Key->Value"
hmap.entries.each { |key, value| puts "#{key} -> #{value}" }
puts "\nTraverse keys only Key"
hmap.keys.each { |key| puts key }
puts "\nTraverse values only Value"
hmap.values.each { |val| puts val }
end

View File

@@ -0,0 +1,128 @@
=begin
File: hash_map_chaining.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative './array_hash_map'
### Hash map with chaining ###
class HashMapChaining
### Constructor ###
def initialize
@size = 0 # Number of key-value pairs
@capacity = 4 # Hash table capacity
@load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion
@extend_ratio = 2 # Expansion multiplier
@buckets = Array.new(@capacity) { [] } # Bucket array
end
### Hash function ###
def hash_func(key)
key % @capacity
end
### Load factor ###
def load_factor
@size / @capacity
end
### Query operation ###
def get(key)
index = hash_func(key)
bucket = @buckets[index]
# Traverse bucket, if key is found, return corresponding val
for pair in bucket
return pair.val if pair.key == key
end
# Return nil if key not found
nil
end
### Add operation ###
def put(key, val)
# When load factor exceeds threshold, perform expansion
extend if load_factor > @load_thres
index = hash_func(key)
bucket = @buckets[index]
# Traverse bucket, if specified key is encountered, update corresponding val and return
for pair in bucket
if pair.key == key
pair.val = val
return
end
end
# If key does not exist, append key-value pair to the end
pair = Pair.new(key, val)
bucket << pair
@size += 1
end
### Delete operation ###
def remove(key)
index = hash_func(key)
bucket = @buckets[index]
# Traverse bucket and remove key-value pair from it
for pair in bucket
if pair.key == key
bucket.delete(pair)
@size -= 1
break
end
end
end
### Expand hash table ###
def extend
# Temporarily store original hash table
buckets = @buckets
# Initialize expanded new hash table
@capacity *= @extend_ratio
@buckets = Array.new(@capacity) { [] }
@size = 0
# Move key-value pairs from original hash table to new hash table
for bucket in buckets
for pair in bucket
put(pair.key, pair.val)
end
end
end
### Print hash table ###
def print
for bucket in @buckets
res = []
for pair in bucket
res << "#{pair.key} -> #{pair.val}"
end
pp res
end
end
end
### Driver Code ###
if __FILE__ == $0
### Initialize hash table
hashmap = HashMapChaining.new
# Add operation
# Add key-value pair (key, value) to the hash table
hashmap.put(12836, "Xiao Ha")
hashmap.put(15937, "Xiao Luo")
hashmap.put(16750, "Xiao Suan")
hashmap.put(13276, "Xiao Fa")
hashmap.put(10583, "Xiao Ya")
puts "\nAfter adding, hash table is\n[Key1 -> Value1, Key2 -> Value2, ...]"
hashmap.print
# Query operation
# Input key into hash table to get value
name = hashmap.get(13276)
puts "\nInput student ID 13276, found name #{name}"
# Remove operation
# Remove key-value pair (key, value) from hash table
hashmap.remove(12836)
puts "\nAfter deleting 12836, hash table is\n[Key1 -> Value1, Key2 -> Value2, ...]"
hashmap.print
end

View File

@@ -0,0 +1,147 @@
=begin
File: hash_map_open_addressing.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative './array_hash_map'
### Hash map with open addressing ###
class HashMapOpenAddressing
TOMBSTONE = Pair.new(-1, '-1') # Removal marker
### Constructor ###
def initialize
@size = 0 # Number of key-value pairs
@capacity = 4 # Hash table capacity
@load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion
@extend_ratio = 2 # Expansion multiplier
@buckets = Array.new(@capacity) # Bucket array
end
### Hash function ###
def hash_func(key)
key % @capacity
end
### Load factor ###
def load_factor
@size / @capacity
end
### Search bucket index for key ###
def find_bucket(key)
index = hash_func(key)
first_tombstone = -1
# Linear probing, break when encountering an empty bucket
while !@buckets[index].nil?
# If key is encountered, return the corresponding bucket index
if @buckets[index].key == key
# If a removal marker was encountered before, move the key-value pair to that index
if first_tombstone != -1
@buckets[first_tombstone] = @buckets[index]
@buckets[index] = TOMBSTONE
return first_tombstone # Return the moved bucket index
end
return index # Return bucket index
end
# Record the first removal marker encountered
first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE
# Calculate bucket index, wrap around to the head if past the tail
index = (index + 1) % @capacity
end
# If key does not exist, return the index for insertion
first_tombstone == -1 ? index : first_tombstone
end
### Query operation ###
def get(key)
# Search for bucket index corresponding to key
index = find_bucket(key)
# If key-value pair is found, return corresponding val
return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])
# Return nil if key-value pair does not exist
nil
end
### Add operation ###
def put(key, val)
# When load factor exceeds threshold, perform expansion
extend if load_factor > @load_thres
# Search for bucket index corresponding to key
index = find_bucket(key)
# If key-value pair found, overwrite val and return
unless [nil, TOMBSTONE].include?(@buckets[index])
@buckets[index].val = val
return
end
# If key-value pair does not exist, add the key-value pair
@buckets[index] = Pair.new(key, val)
@size += 1
end
### Delete operation ###
def remove(key)
# Search for bucket index corresponding to key
index = find_bucket(key)
# If key-value pair is found, overwrite it with removal marker
unless [nil, TOMBSTONE].include?(@buckets[index])
@buckets[index] = TOMBSTONE
@size -= 1
end
end
### Expand hash table ###
def extend
# Temporarily store the original hash table
buckets_tmp = @buckets
# Initialize expanded new hash table
@capacity *= @extend_ratio
@buckets = Array.new(@capacity)
@size = 0
# Move key-value pairs from original hash table to new hash table
for pair in buckets_tmp
put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)
end
end
### Print hash table ###
def print
for pair in @buckets
if pair.nil?
puts "Nil"
elsif pair == TOMBSTONE
puts "TOMBSTONE"
else
puts "#{pair.key} -> #{pair.val}"
end
end
end
end
### Driver Code ###
if __FILE__ == $0
# Initialize hash table
hashmap = HashMapOpenAddressing.new
# Add operation
# Add key-value pair (key, val) to the hash table
hashmap.put(12836, "Xiao Ha")
hashmap.put(15937, "Xiao Luo")
hashmap.put(16750, "Xiao Suan")
hashmap.put(13276, "Xiao Fa")
hashmap.put(10583, "Xiao Ya")
puts "\nAfter adding is complete, hash table is\nKey -> Value"
hashmap.print
# Query operation
# Input key into hash table to get value val
name = hashmap.get(13276)
puts "\nInput student ID 13276, found name #{name}"
# Remove operation
# Remove key-value pair (key, val) from hash table
hashmap.remove(16750)
puts "\nAfter removing 16750, hash table is\nKey -> Value"
hashmap.print
end

View File

@@ -0,0 +1,62 @@
=begin
File: simple_hash.rb
Created Time: 2024-04-14
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Additive hash ###
def add_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash += c.ord }
hash % modulus
end
### Multiplicative hash ###
def mul_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash = 31 * hash + c.ord }
hash % modulus
end
### XOR hash ###
def xor_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash ^= c.ord }
hash % modulus
end
### Rotational hash ###
def rot_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }
hash % modulus
end
### Driver Code ###
if __FILE__ == $0
key = "Hello Algo"
hash = add_hash(key)
puts "Additive hash value is #{hash}"
hash = mul_hash(key)
puts "Multiplicative hash value is #{hash}"
hash = xor_hash(key)
puts "XOR hash value is #{hash}"
hash = rot_hash(key)
puts "Rotational hash value is #{hash}"
end