mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-12 08:48:50 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -14,11 +14,11 @@ class Pair:
|
||||
|
||||
|
||||
class ArrayHashMap:
|
||||
"""Hash table based on array implementation"""
|
||||
"""Hash table implemented with an array"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
# Initialize array with 100 buckets
|
||||
# Initialize an array containing 100 buckets
|
||||
self.buckets: list[Pair | None] = [None] * 100
|
||||
|
||||
def hash_func(self, key: int) -> int:
|
||||
@@ -27,7 +27,7 @@ class ArrayHashMap:
|
||||
return index
|
||||
|
||||
def get(self, key: int) -> str | None:
|
||||
"""Query operation"""
|
||||
"""Lookup operation"""
|
||||
index: int = self.hash_func(key)
|
||||
pair: Pair = self.buckets[index]
|
||||
if pair is None:
|
||||
@@ -35,7 +35,7 @@ class ArrayHashMap:
|
||||
return pair.val
|
||||
|
||||
def put(self, key: int, val: str):
|
||||
"""Add and update operation"""
|
||||
"""Insert and update operations"""
|
||||
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 to represent removal
|
||||
# Set to None to mark removal
|
||||
self.buckets[index] = None
|
||||
|
||||
def entry_set(self) -> list[Pair]:
|
||||
@@ -71,47 +71,47 @@ class ArrayHashMap:
|
||||
return result
|
||||
|
||||
def print(self):
|
||||
"""Print hash table"""
|
||||
"""Print the hash table"""
|
||||
for pair in self.buckets:
|
||||
if pair is not None:
|
||||
print(pair.key, "->", pair.val)
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize hash table
|
||||
# Initialize the hash table
|
||||
hmap = ArrayHashMap()
|
||||
|
||||
# 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")
|
||||
print("\nAfter adding, the hash table is\nKey -> Value")
|
||||
# Insert operation
|
||||
# Insert the key-value pair (key, value) into the hash table
|
||||
hmap.put(12836, "小哈")
|
||||
hmap.put(15937, "小啰")
|
||||
hmap.put(16750, "小算")
|
||||
hmap.put(13276, "小法")
|
||||
hmap.put(10583, "小鸭")
|
||||
print("\nHash table after insertion:\nKey -> Value")
|
||||
hmap.print()
|
||||
|
||||
# Query operation
|
||||
# Input key into the hash table to get value
|
||||
# Lookup operation
|
||||
# Look up key in the hash table to obtain value
|
||||
name = hmap.get(15937)
|
||||
print("\nInput student ID 15937, found name " + name)
|
||||
print("\nLook up student ID 15937 to obtain the name " + name)
|
||||
|
||||
# Remove operation
|
||||
# Remove key-value pair (key, value) from the hash table
|
||||
# Remove the key-value pair (key, value) from the hash table
|
||||
hmap.remove(10583)
|
||||
print("\nAfter removing 10583, the hash table is\nKey -> Value")
|
||||
print("\nHash table after removing 10583:\nKey -> Value")
|
||||
hmap.print()
|
||||
|
||||
# Traverse hash table
|
||||
print("\nTraverse key-value pairs Key->Value")
|
||||
# Traverse the hash table
|
||||
print("\nTraverse key-value pairs: Key->Value")
|
||||
for pair in hmap.entry_set():
|
||||
print(pair.key, "->", pair.val)
|
||||
|
||||
print("\nTraverse keys only Key")
|
||||
print("\nTraverse keys only: Key")
|
||||
for key in hmap.key_set():
|
||||
print(key)
|
||||
|
||||
print("\nTraverse values only Value")
|
||||
print("\nTraverse values only: Value")
|
||||
for val in hmap.value_set():
|
||||
print(val)
|
||||
|
||||
Reference in New Issue
Block a user