From 26ad485dd271a6520bd94ff41623202c8b34eac0 Mon Sep 17 00:00:00 2001 From: machangxin Date: Fri, 16 Dec 2022 10:55:28 +0800 Subject: [PATCH] Add function PrintMap() in Go --- codes/go/chapter_hashing/hash_map_test.go | 10 ++++------ codes/go/pkg/print_utils.go | 9 ++++++++- codes/python/chapter_hashing/array_hash_map.py | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/codes/go/chapter_hashing/hash_map_test.go b/codes/go/chapter_hashing/hash_map_test.go index 10b2c967..c53d3991 100644 --- a/codes/go/chapter_hashing/hash_map_test.go +++ b/codes/go/chapter_hashing/hash_map_test.go @@ -7,6 +7,8 @@ package chapter_hashing import ( "fmt" "testing" + + . "github.com/krahets/hello-algo/pkg" ) func TestHashmap(t *testing.T) { @@ -21,9 +23,7 @@ func TestHashmap(t *testing.T) { mapp[13276] = "小法" mapp[10583] = "小鸭" fmt.Println("\n添加完成后,哈希表为\nKey -> Value") - for key, value := range mapp { - fmt.Printf("%d -> %s\n", key, value) - } + PrintMap(mapp) /* 查询操作 */ // 向哈希表输入键 key ,得到值 value @@ -34,9 +34,7 @@ func TestHashmap(t *testing.T) { // 在哈希表中删除键值对 (key, value) delete(mapp, 10583) fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value") - for key, value := range mapp { - fmt.Printf("%d -> %s\n", key, value) - } + PrintMap(mapp) /* 遍历哈希表 */ // 遍历键值对 key->value diff --git a/codes/go/pkg/print_utils.go b/codes/go/pkg/print_utils.go index e42d4068..41a8ed26 100644 --- a/codes/go/pkg/print_utils.go +++ b/codes/go/pkg/print_utils.go @@ -1,6 +1,6 @@ // File: print_utils.go // Created Time: 2022-12-03 -// Author: Reanon (793584285@qq.com), Krahets (krahets@163.com) +// Author: Reanon (793584285@qq.com), Krahets (krahets@163.com), msk397 (machangxinq@gmail.com) package pkg @@ -96,3 +96,10 @@ func showTrunk(t *trunk) { showTrunk(t.prev) fmt.Print(t.str) } + +// PrintHashMap Print a hash map +func PrintMap(m map[int]string) { + for key, value := range m { + fmt.Printf("%d -> %s\n", key, value) + } +} \ No newline at end of file diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py index 046e5c61..a65f1544 100644 --- a/codes/python/chapter_hashing/array_hash_map.py +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -39,7 +39,7 @@ class ArrayHashMap: """ 删除操作 """ def remove(self, key): index = self.hashFunc(key) - # 置为空字符,代表删除 + # 置为None,代表删除 self.bucket[index] = None """ 获取所有键值对 """