mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08: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:
@@ -18,7 +18,7 @@ class ArrayHashMap:
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
# Initialize an array, containing 100 buckets
|
||||
# Initialize array with 100 buckets
|
||||
self.buckets: list[Pair | None] = [None] * 100
|
||||
|
||||
def hash_func(self, key: int) -> int:
|
||||
@@ -26,7 +26,7 @@ class ArrayHashMap:
|
||||
index = key % 100
|
||||
return index
|
||||
|
||||
def get(self, key: int) -> str:
|
||||
def get(self, key: int) -> str | None:
|
||||
"""Query operation"""
|
||||
index: int = self.hash_func(key)
|
||||
pair: Pair = self.buckets[index]
|
||||
@@ -35,7 +35,7 @@ class ArrayHashMap:
|
||||
return pair.val
|
||||
|
||||
def put(self, key: int, val: str):
|
||||
"""Add operation"""
|
||||
"""Add and update operation"""
|
||||
pair = Pair(key, val)
|
||||
index: int = self.hash_func(key)
|
||||
self.buckets[index] = pair
|
||||
@@ -43,7 +43,7 @@ class ArrayHashMap:
|
||||
def remove(self, key: int):
|
||||
"""Remove operation"""
|
||||
index: int = self.hash_func(key)
|
||||
# Set to None, representing removal
|
||||
# Set to None to represent removal
|
||||
self.buckets[index] = None
|
||||
|
||||
def entry_set(self) -> list[Pair]:
|
||||
@@ -84,18 +84,18 @@ if __name__ == "__main__":
|
||||
|
||||
# Add operation
|
||||
# Add key-value pair (key, value) to the hash table
|
||||
hmap.put(12836, "Ha")
|
||||
hmap.put(15937, "Luo")
|
||||
hmap.put(16750, "Suan")
|
||||
hmap.put(13276, "Fa")
|
||||
hmap.put(10583, "Ya")
|
||||
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")
|
||||
print("\nAfter adding, the hash table is\nKey -> Value")
|
||||
hmap.print()
|
||||
|
||||
# Query operation
|
||||
# Enter key to the hash table, get value
|
||||
# Input key into the hash table to get value
|
||||
name = hmap.get(15937)
|
||||
print("\nEnter student ID 15937, found name " + name)
|
||||
print("\nInput student ID 15937, found name " + name)
|
||||
|
||||
# Remove operation
|
||||
# Remove key-value pair (key, value) from the hash table
|
||||
@@ -108,10 +108,10 @@ if __name__ == "__main__":
|
||||
for pair in hmap.entry_set():
|
||||
print(pair.key, "->", pair.val)
|
||||
|
||||
print("\nIndividually traverse keys Key")
|
||||
print("\nTraverse keys only Key")
|
||||
for key in hmap.key_set():
|
||||
print(key)
|
||||
|
||||
print("\nIndividually traverse values Value")
|
||||
print("\nTraverse values only Value")
|
||||
for val in hmap.value_set():
|
||||
print(val)
|
||||
|
||||
@@ -14,24 +14,24 @@ from modules import ListNode
|
||||
if __name__ == "__main__":
|
||||
num = 3
|
||||
hash_num = hash(num)
|
||||
print(f"Integer {num}'s hash value is {hash_num}")
|
||||
print(f"Hash value of integer {num} is {hash_num}")
|
||||
|
||||
bol = True
|
||||
hash_bol = hash(bol)
|
||||
print(f"Boolean {bol}'s hash value is {hash_bol}")
|
||||
print(f"Hash value of boolean {bol} is {hash_bol}")
|
||||
|
||||
dec = 3.14159
|
||||
hash_dec = hash(dec)
|
||||
print(f"Decimal {dec}'s hash value is {hash_dec}")
|
||||
print(f"Hash value of decimal {dec} is {hash_dec}")
|
||||
|
||||
str = "Hello algorithm"
|
||||
str = "Hello algo"
|
||||
hash_str = hash(str)
|
||||
print(f"String {str}'s hash value is {hash_str}")
|
||||
print(f"Hash value of string {str} is {hash_str}")
|
||||
|
||||
tup = (12836, "Ha")
|
||||
tup = (12836, "Xiao Ha")
|
||||
hash_tup = hash(tup)
|
||||
print(f"Tuple {tup}'s hash value is {hash(hash_tup)}")
|
||||
print(f"Hash value of tuple {tup} is {hash(hash_tup)}")
|
||||
|
||||
obj = ListNode(0)
|
||||
hash_obj = hash(obj)
|
||||
print(f"Node object {obj}'s hash value is {hash_obj}")
|
||||
print(f"Hash value of node object {obj} is {hash_obj}")
|
||||
|
||||
@@ -17,18 +17,18 @@ if __name__ == "__main__":
|
||||
|
||||
# Add operation
|
||||
# Add key-value pair (key, value) to the hash table
|
||||
hmap[12836] = "Ha"
|
||||
hmap[15937] = "Luo"
|
||||
hmap[16750] = "Suan"
|
||||
hmap[13276] = "Fa"
|
||||
hmap[10583] = "Ya"
|
||||
hmap[12836] = "Xiao Ha"
|
||||
hmap[15937] = "Xiao Luo"
|
||||
hmap[16750] = "Xiao Suan"
|
||||
hmap[13276] = "Xiao Fa"
|
||||
hmap[10583] = "Xiao Ya"
|
||||
print("\nAfter adding, the hash table is\nKey -> Value")
|
||||
print_dict(hmap)
|
||||
|
||||
# Query operation
|
||||
# Enter key to the hash table, get value
|
||||
# Input key into the hash table to get value
|
||||
name: str = hmap[15937]
|
||||
print("\nEnter student ID 15937, found name " + name)
|
||||
print("\nInput student ID 15937, found name " + name)
|
||||
|
||||
# Remove operation
|
||||
# Remove key-value pair (key, value) from the hash table
|
||||
@@ -41,10 +41,10 @@ if __name__ == "__main__":
|
||||
for key, value in hmap.items():
|
||||
print(key, "->", value)
|
||||
|
||||
print("\nIndividually traverse keys Key")
|
||||
print("\nTraverse keys only Key")
|
||||
for key in hmap.keys():
|
||||
print(key)
|
||||
|
||||
print("\nIndividually traverse values Value")
|
||||
print("\nTraverse values only Value")
|
||||
for val in hmap.values():
|
||||
print(val)
|
||||
|
||||
@@ -12,7 +12,7 @@ from chapter_hashing.array_hash_map import Pair
|
||||
|
||||
|
||||
class HashMapChaining:
|
||||
"""Chained address hash table"""
|
||||
"""Hash table with separate chaining"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
@@ -34,26 +34,26 @@ class HashMapChaining:
|
||||
"""Query operation"""
|
||||
index = self.hash_func(key)
|
||||
bucket = self.buckets[index]
|
||||
# Traverse the bucket, if the key is found, return the corresponding val
|
||||
# Traverse bucket, if key is found, return corresponding val
|
||||
for pair in bucket:
|
||||
if pair.key == key:
|
||||
return pair.val
|
||||
# If the key is not found, return None
|
||||
# If key is not found, return None
|
||||
return None
|
||||
|
||||
def put(self, key: int, val: str):
|
||||
"""Add operation"""
|
||||
# When the load factor exceeds the threshold, perform expansion
|
||||
# When load factor exceeds threshold, perform expansion
|
||||
if self.load_factor() > self.load_thres:
|
||||
self.extend()
|
||||
index = self.hash_func(key)
|
||||
bucket = self.buckets[index]
|
||||
# Traverse the bucket, if the specified key is encountered, update the corresponding val and return
|
||||
# Traverse bucket, if specified key is encountered, update corresponding val and return
|
||||
for pair in bucket:
|
||||
if pair.key == key:
|
||||
pair.val = val
|
||||
return
|
||||
# If the key is not found, add the key-value pair to the end
|
||||
# If key does not exist, append key-value pair to the end
|
||||
pair = Pair(key, val)
|
||||
bucket.append(pair)
|
||||
self.size += 1
|
||||
@@ -62,7 +62,7 @@ class HashMapChaining:
|
||||
"""Remove operation"""
|
||||
index = self.hash_func(key)
|
||||
bucket = self.buckets[index]
|
||||
# Traverse the bucket, remove the key-value pair from it
|
||||
# Traverse bucket and remove key-value pair from it
|
||||
for pair in bucket:
|
||||
if pair.key == key:
|
||||
bucket.remove(pair)
|
||||
@@ -70,14 +70,14 @@ class HashMapChaining:
|
||||
break
|
||||
|
||||
def extend(self):
|
||||
"""Extend hash table"""
|
||||
"""Expand hash table"""
|
||||
# Temporarily store the original hash table
|
||||
buckets = self.buckets
|
||||
# Initialize the extended new hash table
|
||||
# Initialize expanded new hash table
|
||||
self.capacity *= self.extend_ratio
|
||||
self.buckets = [[] for _ in range(self.capacity)]
|
||||
self.size = 0
|
||||
# Move key-value pairs from the original hash table to the new hash table
|
||||
# Move key-value pairs from original hash table to new hash table
|
||||
for bucket in buckets:
|
||||
for pair in bucket:
|
||||
self.put(pair.key, pair.val)
|
||||
@@ -98,18 +98,18 @@ if __name__ == "__main__":
|
||||
|
||||
# Add operation
|
||||
# Add key-value pair (key, value) to the hash table
|
||||
hashmap.put(12836, "Ha")
|
||||
hashmap.put(15937, "Luo")
|
||||
hashmap.put(16750, "Suan")
|
||||
hashmap.put(13276, "Fa")
|
||||
hashmap.put(10583, "Ya")
|
||||
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")
|
||||
print("\nAfter adding, the hash table is\n[Key1 -> Value1, Key2 -> Value2, ...]")
|
||||
hashmap.print()
|
||||
|
||||
# Query operation
|
||||
# Enter key to the hash table, get value
|
||||
# Input key into the hash table to get value
|
||||
name = hashmap.get(13276)
|
||||
print("\nEnter student ID 13276, found name " + name)
|
||||
print("\nInput student ID 13276, found name " + name)
|
||||
|
||||
# Remove operation
|
||||
# Remove key-value pair (key, value) from the hash table
|
||||
|
||||
@@ -12,7 +12,7 @@ from chapter_hashing.array_hash_map import Pair
|
||||
|
||||
|
||||
class HashMapOpenAddressing:
|
||||
"""Open addressing hash table"""
|
||||
"""Hash table with open addressing"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
@@ -21,7 +21,7 @@ class HashMapOpenAddressing:
|
||||
self.load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion
|
||||
self.extend_ratio = 2 # Expansion multiplier
|
||||
self.buckets: list[Pair | None] = [None] * self.capacity # Bucket array
|
||||
self.TOMBSTONE = Pair(-1, "-1") # Removal mark
|
||||
self.TOMBSTONE = Pair(-1, "-1") # Removal marker
|
||||
|
||||
def hash_func(self, key: int) -> int:
|
||||
"""Hash function"""
|
||||
@@ -32,70 +32,70 @@ class HashMapOpenAddressing:
|
||||
return self.size / self.capacity
|
||||
|
||||
def find_bucket(self, key: int) -> int:
|
||||
"""Search for the bucket index corresponding to key"""
|
||||
"""Search for bucket index corresponding to key"""
|
||||
index = self.hash_func(key)
|
||||
first_tombstone = -1
|
||||
# Linear probing, break when encountering an empty bucket
|
||||
while self.buckets[index] is not None:
|
||||
# If the key is encountered, return the corresponding bucket index
|
||||
# If key is encountered, return the corresponding bucket index
|
||||
if self.buckets[index].key == key:
|
||||
# If a removal mark was encountered earlier, move the key-value pair to that index
|
||||
# If a removal marker was encountered before, move the key-value pair to that index
|
||||
if first_tombstone != -1:
|
||||
self.buckets[first_tombstone] = self.buckets[index]
|
||||
self.buckets[index] = self.TOMBSTONE
|
||||
return first_tombstone # Return the moved bucket index
|
||||
return index # Return bucket index
|
||||
# Record the first encountered removal mark
|
||||
# Record the first removal marker encountered
|
||||
if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:
|
||||
first_tombstone = index
|
||||
# Calculate the bucket index, return to the head if exceeding the tail
|
||||
# Calculate bucket index, wrap around to the head if past the tail
|
||||
index = (index + 1) % self.capacity
|
||||
# If the key does not exist, return the index of the insertion point
|
||||
# If key does not exist, return the index for insertion
|
||||
return index if first_tombstone == -1 else first_tombstone
|
||||
|
||||
def get(self, key: int) -> str:
|
||||
"""Query operation"""
|
||||
# Search for the bucket index corresponding to key
|
||||
# Search for bucket index corresponding to key
|
||||
index = self.find_bucket(key)
|
||||
# If the key-value pair is found, return the corresponding val
|
||||
# If key-value pair is found, return corresponding val
|
||||
if self.buckets[index] not in [None, self.TOMBSTONE]:
|
||||
return self.buckets[index].val
|
||||
# If the key-value pair does not exist, return None
|
||||
# If key-value pair does not exist, return None
|
||||
return None
|
||||
|
||||
def put(self, key: int, val: str):
|
||||
"""Add operation"""
|
||||
# When the load factor exceeds the threshold, perform expansion
|
||||
# When load factor exceeds threshold, perform expansion
|
||||
if self.load_factor() > self.load_thres:
|
||||
self.extend()
|
||||
# Search for the bucket index corresponding to key
|
||||
# Search for bucket index corresponding to key
|
||||
index = self.find_bucket(key)
|
||||
# If the key-value pair is found, overwrite val and return
|
||||
# If key-value pair is found, overwrite val and return
|
||||
if self.buckets[index] not in [None, self.TOMBSTONE]:
|
||||
self.buckets[index].val = val
|
||||
return
|
||||
# If the key-value pair does not exist, add the key-value pair
|
||||
# If key-value pair does not exist, add the key-value pair
|
||||
self.buckets[index] = Pair(key, val)
|
||||
self.size += 1
|
||||
|
||||
def remove(self, key: int):
|
||||
"""Remove operation"""
|
||||
# Search for the bucket index corresponding to key
|
||||
# Search for bucket index corresponding to key
|
||||
index = self.find_bucket(key)
|
||||
# If the key-value pair is found, cover it with a removal mark
|
||||
# If key-value pair is found, overwrite it with removal marker
|
||||
if self.buckets[index] not in [None, self.TOMBSTONE]:
|
||||
self.buckets[index] = self.TOMBSTONE
|
||||
self.size -= 1
|
||||
|
||||
def extend(self):
|
||||
"""Extend hash table"""
|
||||
"""Expand hash table"""
|
||||
# Temporarily store the original hash table
|
||||
buckets_tmp = self.buckets
|
||||
# Initialize the extended new hash table
|
||||
# Initialize expanded new hash table
|
||||
self.capacity *= self.extend_ratio
|
||||
self.buckets = [None] * self.capacity
|
||||
self.size = 0
|
||||
# Move key-value pairs from the original hash table to the new hash table
|
||||
# Move key-value pairs from original hash table to new hash table
|
||||
for pair in buckets_tmp:
|
||||
if pair not in [None, self.TOMBSTONE]:
|
||||
self.put(pair.key, pair.val)
|
||||
@@ -118,18 +118,18 @@ if __name__ == "__main__":
|
||||
|
||||
# Add operation
|
||||
# Add key-value pair (key, val) to the hash table
|
||||
hashmap.put(12836, "Ha")
|
||||
hashmap.put(15937, "Luo")
|
||||
hashmap.put(16750, "Suan")
|
||||
hashmap.put(13276, "Fa")
|
||||
hashmap.put(10583, "Ya")
|
||||
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")
|
||||
print("\nAfter adding, the hash table is\nKey -> Value")
|
||||
hashmap.print()
|
||||
|
||||
# Query operation
|
||||
# Enter key to the hash table, get value val
|
||||
# Input key into the hash table to get val
|
||||
name = hashmap.get(13276)
|
||||
print("\nEnter student ID 13276, found name " + name)
|
||||
print("\nInput student ID 13276, found name " + name)
|
||||
|
||||
# Remove operation
|
||||
# Remove key-value pair (key, val) from the hash table
|
||||
|
||||
@@ -43,7 +43,7 @@ def rot_hash(key: str) -> int:
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
key = "Hello algorithm"
|
||||
key = "Hello algo"
|
||||
|
||||
hash = add_hash(key)
|
||||
print(f"Additive hash value is {hash}")
|
||||
|
||||
Reference in New Issue
Block a user