docs: add Japanese translate documents (#1812)

* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
Ikko Eltociear Ashimine
2025-10-17 06:04:43 +09:00
committed by GitHub
parent 2487a27036
commit 954c45864b
886 changed files with 33569 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
"""
File: array_hash_map.py
Created Time: 2022-12-14
Author: msk397 (machangxinq@gmail.com)
"""
class Pair:
"""キー値ペア"""
def __init__(self, key: int, val: str):
self.key = key
self.val = val
class ArrayHashMap:
"""配列実装に基づくハッシュテーブル"""
def __init__(self):
"""コンストラクタ"""
# 100個のバケットを含む配列を初期化
self.buckets: list[Pair | None] = [None] * 100
def hash_func(self, key: int) -> int:
"""ハッシュ関数"""
index = key % 100
return index
def get(self, key: int) -> str:
"""照会操作"""
index: int = self.hash_func(key)
pair: Pair = self.buckets[index]
if pair is None:
return None
return pair.val
def put(self, key: int, val: str):
"""追加操作"""
pair = Pair(key, val)
index: int = self.hash_func(key)
self.buckets[index] = pair
def remove(self, key: int):
"""削除操作"""
index: int = self.hash_func(key)
# None に設定し、削除を表現
self.buckets[index] = None
def entry_set(self) -> list[Pair]:
"""すべてのキー値ペアを取得"""
result: list[Pair] = []
for pair in self.buckets:
if pair is not None:
result.append(pair)
return result
def key_set(self) -> list[int]:
"""すべてのキーを取得"""
result = []
for pair in self.buckets:
if pair is not None:
result.append(pair.key)
return result
def value_set(self) -> list[str]:
"""すべての値を取得"""
result = []
for pair in self.buckets:
if pair is not None:
result.append(pair.val)
return result
def print(self):
"""ハッシュテーブルを出力"""
for pair in self.buckets:
if pair is not None:
print(pair.key, "->", pair.val)
"""Driver Code"""
if __name__ == "__main__":
# ハッシュテーブルを初期化
hmap = ArrayHashMap()
# 追加操作
# キー値ペア (key, value) をハッシュテーブルに追加
hmap.put(12836, "Ha")
hmap.put(15937, "Luo")
hmap.put(16750, "Suan")
hmap.put(13276, "Fa")
hmap.put(10583, "Ya")
print("\n追加後、ハッシュテーブルは\nKey -> Value")
hmap.print()
# 照会操作
# ハッシュテーブルにキーを入力し、値を取得
name = hmap.get(15937)
print("\n学生ID 15937 を入力、名前 " + name + " が見つかりました")
# 削除操作
# ハッシュテーブルからキー値ペア (key, value) を削除
hmap.remove(10583)
print("\n10583 を削除後、ハッシュテーブルは\nKey -> Value")
hmap.print()
# ハッシュテーブルを走査
print("\nキー値ペアを走査 Key->Value")
for pair in hmap.entry_set():
print(pair.key, "->", pair.val)
print("\nキーを個別に走査 Key")
for key in hmap.key_set():
print(key)
print("\n値を個別に走査 Value")
for val in hmap.value_set():
print(val)

View File

@@ -0,0 +1,37 @@
"""
File: built_in_hash.py
Created Time: 2023-06-15
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import ListNode
"""Driver Code"""
if __name__ == "__main__":
num = 3
hash_num = hash(num)
print(f"整数 {num} のハッシュ値は {hash_num}")
bol = True
hash_bol = hash(bol)
print(f"ブール値 {bol} のハッシュ値は {hash_bol}")
dec = 3.14159
hash_dec = hash(dec)
print(f"小数 {dec} のハッシュ値は {hash_dec}")
str = "Hello algorithm"
hash_str = hash(str)
print(f"文字列 {str} のハッシュ値は {hash_str}")
tup = (12836, "Ha")
hash_tup = hash(tup)
print(f"タプル {tup} のハッシュ値は {hash(hash_tup)}")
obj = ListNode(0)
hash_obj = hash(obj)
print(f"ノードオブジェクト {obj} のハッシュ値は {hash_obj}")

View 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__":
# ハッシュテーブルを初期化
hmap = dict[int, str]()
# 追加操作
# キー値ペア (key, value) をハッシュテーブルに追加
hmap[12836] = "Ha"
hmap[15937] = "Luo"
hmap[16750] = "Suan"
hmap[13276] = "Fa"
hmap[10583] = "Ya"
print("\n追加後、ハッシュテーブルは\nKey -> Value")
print_dict(hmap)
# 照会操作
# ハッシュテーブルにキーを入力し、値を取得
name: str = hmap[15937]
print("\n学生ID 15937 を入力、名前 " + name + " が見つかりました")
# 削除操作
# ハッシュテーブルからキー値ペア (key, value) を削除
hmap.pop(10583)
print("\n10583 を削除後、ハッシュテーブルは\nKey -> Value")
print_dict(hmap)
# ハッシュテーブルを走査
print("\nキー値ペアを走査 Key->Value")
for key, value in hmap.items():
print(key, "->", value)
print("\nキーを個別に走査 Key")
for key in hmap.keys():
print(key)
print("\n値を個別に走査 Value")
for val in hmap.values():
print(val)

View File

@@ -0,0 +1,118 @@
"""
File: hash_map_chaining.py
Created Time: 2023-06-13
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from chapter_hashing.array_hash_map import Pair
class HashMapChaining:
"""チェーンアドレス法ハッシュテーブル"""
def __init__(self):
"""コンストラクタ"""
self.size = 0 # キー値ペアの数
self.capacity = 4 # ハッシュテーブルの容量
self.load_thres = 2.0 / 3.0 # 拡張をトリガーする負荷率の閾値
self.extend_ratio = 2 # 拡張の倍数
self.buckets = [[] for _ in range(self.capacity)] # バケット配列
def hash_func(self, key: int) -> int:
"""ハッシュ関数"""
return key % self.capacity
def load_factor(self) -> float:
"""負荷率"""
return self.size / self.capacity
def get(self, key: int) -> str | None:
"""照会操作"""
index = self.hash_func(key)
bucket = self.buckets[index]
# バケットを走査し、キーが見つかれば対応する val を返す
for pair in bucket:
if pair.key == key:
return pair.val
# キーが見つからない場合、None を返す
return None
def put(self, key: int, val: str):
"""追加操作"""
# 負荷率が閾値を超えた場合、拡張を実行
if self.load_factor() > self.load_thres:
self.extend()
index = self.hash_func(key)
bucket = self.buckets[index]
# バケットを走査し、指定されたキーに遭遇した場合、対応する val を更新して返す
for pair in bucket:
if pair.key == key:
pair.val = val
return
# キーが見つからない場合、キー値ペアを末尾に追加
pair = Pair(key, val)
bucket.append(pair)
self.size += 1
def remove(self, key: int):
"""削除操作"""
index = self.hash_func(key)
bucket = self.buckets[index]
# バケットを走査し、その中からキー値ペアを削除
for pair in bucket:
if pair.key == key:
bucket.remove(pair)
self.size -= 1
break
def extend(self):
"""ハッシュテーブルを拡張"""
# 元のハッシュテーブルを一時的に保存
buckets = self.buckets
# 拡張された新しいハッシュテーブルを初期化
self.capacity *= self.extend_ratio
self.buckets = [[] for _ in range(self.capacity)]
self.size = 0
# 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
for bucket in buckets:
for pair in bucket:
self.put(pair.key, pair.val)
def print(self):
"""ハッシュテーブルを出力"""
for bucket in self.buckets:
res = []
for pair in bucket:
res.append(str(pair.key) + " -> " + pair.val)
print(res)
"""Driver Code"""
if __name__ == "__main__":
# ハッシュテーブルを初期化
hashmap = HashMapChaining()
# 追加操作
# キー値ペア (key, value) をハッシュテーブルに追加
hashmap.put(12836, "Ha")
hashmap.put(15937, "Luo")
hashmap.put(16750, "Suan")
hashmap.put(13276, "Fa")
hashmap.put(10583, "Ya")
print("\n追加後、ハッシュテーブルは\n[Key1 -> Value1, Key2 -> Value2, ...]")
hashmap.print()
# 照会操作
# ハッシュテーブルにキーを入力し、値を取得
name = hashmap.get(13276)
print("\n学生ID 13276 を入力、名前 " + name + " が見つかりました")
# 削除操作
# ハッシュテーブルからキー値ペア (key, value) を削除
hashmap.remove(12836)
print("\n12836 を削除後、ハッシュテーブルは\n[Key1 -> Value1, Key2 -> Value2, ...]")
hashmap.print()

View File

@@ -0,0 +1,138 @@
"""
File: hash_map_open_addressing.py
Created Time: 2023-06-13
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from chapter_hashing.array_hash_map import Pair
class HashMapOpenAddressing:
"""オープンアドレス法ハッシュテーブル"""
def __init__(self):
"""コンストラクタ"""
self.size = 0 # キー値ペアの数
self.capacity = 4 # ハッシュテーブルの容量
self.load_thres = 2.0 / 3.0 # 拡張をトリガーする負荷率の閾値
self.extend_ratio = 2 # 拡張の倍数
self.buckets: list[Pair | None] = [None] * self.capacity # バケット配列
self.TOMBSTONE = Pair(-1, "-1") # 削除マーク
def hash_func(self, key: int) -> int:
"""ハッシュ関数"""
return key % self.capacity
def load_factor(self) -> float:
"""負荷率"""
return self.size / self.capacity
def find_bucket(self, key: int) -> int:
"""key に対応するバケットインデックスを検索"""
index = self.hash_func(key)
first_tombstone = -1
# 線形探査、空のバケットに遭遇したらブレーク
while self.buckets[index] is not None:
# キーに遭遇した場合、対応するバケットインデックスを返す
if self.buckets[index].key == key:
# 削除マークが以前に遭遇していた場合、キー値ペアをそのインデックスに移動
if first_tombstone != -1:
self.buckets[first_tombstone] = self.buckets[index]
self.buckets[index] = self.TOMBSTONE
return first_tombstone # 移動されたバケットインデックスを返す
return index # バケットインデックスを返す
# 最初に遭遇した削除マークを記録
if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:
first_tombstone = index
# バケットインデックスを計算、末尾を超えた場合は先頭に戻る
index = (index + 1) % self.capacity
# キーが存在しない場合、挿入ポイントのインデックスを返す
return index if first_tombstone == -1 else first_tombstone
def get(self, key: int) -> str:
"""照会操作"""
# key に対応するバケットインデックスを検索
index = self.find_bucket(key)
# キー値ペアが見つかれば、対応する val を返す
if self.buckets[index] not in [None, self.TOMBSTONE]:
return self.buckets[index].val
# キー値ペアが存在しない場合、None を返す
return None
def put(self, key: int, val: str):
"""追加操作"""
# 負荷率が閾値を超えた場合、拡張を実行
if self.load_factor() > self.load_thres:
self.extend()
# key に対応するバケットインデックスを検索
index = self.find_bucket(key)
# キー値ペアが見つかれば、val を上書きして返す
if self.buckets[index] not in [None, self.TOMBSTONE]:
self.buckets[index].val = val
return
# キー値ペアが存在しない場合、キー値ペアを追加
self.buckets[index] = Pair(key, val)
self.size += 1
def remove(self, key: int):
"""削除操作"""
# key に対応するバケットインデックスを検索
index = self.find_bucket(key)
# キー値ペアが見つかれば、削除マークで覆う
if self.buckets[index] not in [None, self.TOMBSTONE]:
self.buckets[index] = self.TOMBSTONE
self.size -= 1
def extend(self):
"""ハッシュテーブルを拡張"""
# 元のハッシュテーブルを一時的に保存
buckets_tmp = self.buckets
# 拡張された新しいハッシュテーブルを初期化
self.capacity *= self.extend_ratio
self.buckets = [None] * self.capacity
self.size = 0
# 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
for pair in buckets_tmp:
if pair not in [None, self.TOMBSTONE]:
self.put(pair.key, pair.val)
def print(self):
"""ハッシュテーブルを出力"""
for pair in self.buckets:
if pair is None:
print("None")
elif pair is self.TOMBSTONE:
print("TOMBSTONE")
else:
print(pair.key, "->", pair.val)
"""Driver Code"""
if __name__ == "__main__":
# ハッシュテーブルを初期化
hashmap = HashMapOpenAddressing()
# 追加操作
# キー値ペア (key, val) をハッシュテーブルに追加
hashmap.put(12836, "Ha")
hashmap.put(15937, "Luo")
hashmap.put(16750, "Suan")
hashmap.put(13276, "Fa")
hashmap.put(10583, "Ya")
print("\n追加後、ハッシュテーブルは\nKey -> Value")
hashmap.print()
# 照会操作
# ハッシュテーブルにキーを入力し、値 val を取得
name = hashmap.get(13276)
print("\n学生ID 13276 を入力、名前 " + name + " が見つかりました")
# 削除操作
# ハッシュテーブルからキー値ペア (key, val) を削除
hashmap.remove(16750)
print("\n16750 を削除後、ハッシュテーブルは\nKey -> Value")
hashmap.print()

View File

@@ -0,0 +1,58 @@
"""
File: simple_hash.py
Created Time: 2023-06-15
Author: krahets (krahets@163.com)
"""
def add_hash(key: str) -> int:
"""加法ハッシュ"""
hash = 0
modulus = 1000000007
for c in key:
hash += ord(c)
return hash % modulus
def mul_hash(key: str) -> int:
"""乗法ハッシュ"""
hash = 0
modulus = 1000000007
for c in key:
hash = 31 * hash + ord(c)
return hash % modulus
def xor_hash(key: str) -> int:
"""XORハッシュ"""
hash = 0
modulus = 1000000007
for c in key:
hash ^= ord(c)
return hash % modulus
def rot_hash(key: str) -> int:
"""回転ハッシュ"""
hash = 0
modulus = 1000000007
for c in key:
hash = (hash << 4) ^ (hash >> 28) ^ ord(c)
return hash % modulus
"""Driver Code"""
if __name__ == "__main__":
key = "Hello algorithm"
hash = add_hash(key)
print(f"加法ハッシュ値は {hash}")
hash = mul_hash(key)
print(f"乗法ハッシュ値は {hash}")
hash = xor_hash(key)
print(f"XORハッシュ値は {hash}")
hash = rot_hash(key)
print(f"回転ハッシュ値は {hash}")