mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
50
en/codes/python/chapter_hashing/hash_map.py
Normal file
50
en/codes/python/chapter_hashing/hash_map.py
Normal file
@ -0,0 +1,50 @@
|
||||
"""
|
||||
File: hash_map.py
|
||||
Created Time: 2022-12-14
|
||||
Author: msk397 (machangxinq@gmail.com)
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
from modules import print_dict
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize hash table
|
||||
hmap = dict[int, str]()
|
||||
|
||||
# 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"
|
||||
print("\nAfter adding, the hash table is\nKey -> Value")
|
||||
print_dict(hmap)
|
||||
|
||||
# Query operation
|
||||
# Enter key to the hash table, get value
|
||||
name: str = hmap[15937]
|
||||
print("\nEnter student ID 15937, found name " + name)
|
||||
|
||||
# Remove operation
|
||||
# Remove key-value pair (key, value) from the hash table
|
||||
hmap.pop(10583)
|
||||
print("\nAfter removing 10583, the hash table is\nKey -> Value")
|
||||
print_dict(hmap)
|
||||
|
||||
# Traverse hash table
|
||||
print("\nTraverse key-value pairs Key->Value")
|
||||
for key, value in hmap.items():
|
||||
print(key, "->", value)
|
||||
|
||||
print("\nIndividually traverse keys Key")
|
||||
for key in hmap.keys():
|
||||
print(key)
|
||||
|
||||
print("\nIndividually traverse values Value")
|
||||
for val in hmap.values():
|
||||
print(val)
|
||||
Reference in New Issue
Block a user