mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-06 12:44:19 +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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user