Files
hello-algo/ru/codes/python/chapter_searching/hashing_search.py
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

52 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
File: hashing_search.py
Created Time: 2022-11-26
Author: timi (xisunyy@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import ListNode, list_to_linked_list
def hashing_search_array(hmap: dict[int, int], target: int) -> int:
"""Хеш-поиск (массив)"""
# key хеш-таблицы: целевой элемент, value: индекс
# Если такого key нет в хеш-таблице, вернуть -1
return hmap.get(target, -1)
def hashing_search_linkedlist(
hmap: dict[int, ListNode], target: int
) -> ListNode | None:
"""Хеш-поиск (связный список)"""
# key хеш-таблицы: целевой элемент, value: объект узла
# Если такого key нет в хеш-таблице, вернуть None
return hmap.get(target, None)
"""Driver Code"""
if __name__ == "__main__":
target = 3
# Хеш-поиск (массив)
nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]
# Инициализация хеш-таблицы
map0 = dict[int, int]()
for i in range(len(nums)):
map0[nums[i]] = i # key: элемент, value: индекс
index: int = hashing_search_array(map0, target)
print("Индекс целевого элемента 3 =", index)
# Хеш-поиск (связный список)
head: ListNode = list_to_linked_list(nums)
# Инициализация хеш-таблицы
map1 = dict[int, ListNode]()
while head:
map1[head.val] = head # key: значение узла, value: узел
head = head.next
node: ListNode = hashing_search_linkedlist(map1, target)
print("Объект узла со значением 3 =", node)