Re-translate the Japanese version (#1871)

* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
This commit is contained in:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -0,0 +1,121 @@
=begin
File: array_hash_map.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### キーと値のペア ###
class Pair
attr_accessor :key, :val
def initialize(key, val)
@key = key
@val = val
end
end
### 配列で実装したハッシュテーブル ###
class ArrayHashMap
### コンストラクタ ###
def initialize
# 100 個のバケットを含む配列を初期化
@buckets = Array.new(100)
end
### ハッシュ関数 ###
def hash_func(key)
index = key % 100
end
### 検索操作 ###
def get(key)
index = hash_func(key)
pair = @buckets[index]
return if pair.nil?
pair.val
end
### 追加操作 ###
def put(key, val)
pair = Pair.new(key, val)
index = hash_func(key)
@buckets[index] = pair
end
### 削除操作 ###
def remove(key)
index = hash_func(key)
# nil に設定し、削除を表す
@buckets[index] = nil
end
### すべてのキーと値のペアを取得 ###
def entry_set
result = []
@buckets.each { |pair| result << pair unless pair.nil? }
result
end
### すべてのキーを取得 ###
def key_set
result = []
@buckets.each { |pair| result << pair.key unless pair.nil? }
result
end
### すべての値を取得 ###
def value_set
result = []
@buckets.each { |pair| result << pair.val unless pair.nil? }
result
end
### ハッシュテーブルを出力 ###
def print
@buckets.each { |pair| puts "#{pair.key} -> #{pair.val}" unless pair.nil? }
end
end
### Driver Code ###
if __FILE__ == $0
# ハッシュテーブルを初期化
hmap = ArrayHashMap.new
# 追加操作
# ハッシュテーブルにキーと値の組 (key, value) を追加する
hmap.put(12836, "シャオハー")
hmap.put(15937, "シャオルオ")
hmap.put(16750, "シャオスワン")
hmap.put(13276, "シャオファー")
hmap.put(10583, "シャオヤー")
puts "\n追加完了後、ハッシュテーブルは\nKey -> Value"
hmap.print
# 検索操作
# ハッシュテーブルにキー `key` を入力し、値 `value` を取得する
name = hmap.get(15937)
puts "\n学籍番号 15937 を入力すると、名前 #{name} が見つかりました"
# 削除操作
# ハッシュテーブルからキーと値の組 (key, value) を削除する
hmap.remove(10583)
puts "\n10583 を削除した後、ハッシュテーブルは\nKey -> Value"
hmap.print
# ハッシュテーブルを走査
puts "\nキーと値のペア Key->Value を走査"
for pair in hmap.entry_set
puts "#{pair.key} -> #{pair.val}"
end
puts "\nキー Key のみを個別に走査"
for key in hmap.key_set
puts key
end
puts "\n値 Value のみを個別に走査"
for val in hmap.value_set
puts val
end
end
@@ -0,0 +1,34 @@
=begin
File: built_in_hash.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/list_node'
### Driver Code ###
if __FILE__ == $0
num = 3
hash_num = num.hash
puts "整数 #{num} のハッシュ値は #{hash_num}"
bol = true
hash_bol = bol.hash
puts "ブール値 #{bol} のハッシュ値は #{hash_bol}"
dec = 3.14159
hash_dec = dec.hash
puts "小数 #{dec} のハッシュ値は #{hash_dec}"
str = "Hello アルゴリズム"
hash_str = str.hash
puts "文字列 #{str} のハッシュ値は #{hash_str}"
tup = [12836, 'シャオハー']
hash_tup = tup.hash
puts "タプル #{tup} のハッシュ値は #{hash_tup}"
obj = ListNode.new(0)
hash_obj = obj.hash
puts "ノードオブジェクト #{obj} のハッシュ値は #{hash_obj}"
end
+44
View File
@@ -0,0 +1,44 @@
=begin
File: hash_map.rb
Created Time: 2024-04-14
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/print_util'
### Driver Code ###
if __FILE__ == $0
# ハッシュテーブルを初期化
hmap = {}
# 追加操作
# ハッシュテーブルにキーと値の組 (key, value) を追加する
hmap[12836] = "シャオハー"
hmap[15937] = "シャオルオ"
hmap[16750] = "シャオスワン"
hmap[13276] = "シャオファー"
hmap[10583] = "シャオヤー"
puts "\n追加完了後、ハッシュテーブルは\nKey -> Value"
print_hash_map(hmap)
# 検索操作
# ハッシュテーブルにキー key を入力し、値 value を取得する
name = hmap[15937]
puts "\n学籍番号 15937 を入力すると、名前 #{name} が見つかりました"
# 削除操作
# ハッシュテーブルからキーと値の組 (key, value) を削除する
hmap.delete(10583)
puts "\n10583 を削除した後、ハッシュテーブルは\nKey -> Value"
print_hash_map(hmap)
# ハッシュテーブルを走査
puts "\nキーと値のペア Key->Value を走査"
hmap.entries.each { |key, value| puts "#{key} -> #{value}" }
puts "\nキー Key のみを個別に走査"
hmap.keys.each { |key| puts key }
puts "\n値 Value のみを個別に走査"
hmap.values.each { |val| puts val }
end
@@ -0,0 +1,128 @@
=begin
File: hash_map_chaining.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative './array_hash_map'
### キーアドレス法ハッシュテーブル ###
class HashMapChaining
### コンストラクタ ###
def initialize
@size = 0 # キーと値のペア数
@capacity = 4 # ハッシュテーブル容量
@load_thres = 2.0 / 3.0 # リサイズを発動する負荷率のしきい値
@extend_ratio = 2 # 拡張倍率
@buckets = Array.new(@capacity) { [] } # バケット配列
end
### ハッシュ関数 ###
def hash_func(key)
key % @capacity
end
### 負荷率 ###
def load_factor
@size / @capacity
end
### 検索操作 ###
def get(key)
index = hash_func(key)
bucket = @buckets[index]
# バケットを走査し、key が見つかれば対応する val を返す
for pair in bucket
return pair.val if pair.key == key
end
# `key` が見つからなければ `nil` を返す
nil
end
### 追加操作 ###
def put(key, val)
# 負荷率がしきい値を超えたら、リサイズを実行
extend if load_factor > @load_thres
index = hash_func(key)
bucket = @buckets[index]
# バケットを走査し、指定した key が見つかれば対応する val を更新して返す
for pair in bucket
if pair.key == key
pair.val = val
return
end
end
# その key が存在しなければ、キーと値のペアを末尾に追加
pair = Pair.new(key, val)
bucket << pair
@size += 1
end
### 削除操作 ###
def remove(key)
index = hash_func(key)
bucket = @buckets[index]
# バケットを走査してキーと値のペアを削除
for pair in bucket
if pair.key == key
bucket.delete(pair)
@size -= 1
break
end
end
end
### ハッシュテーブルを拡張 ###
def extend
# 元のハッシュテーブルを一時保存
buckets = @buckets
# リサイズ後の新しいハッシュテーブルを初期化
@capacity *= @extend_ratio
@buckets = Array.new(@capacity) { [] }
@size = 0
# キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
for bucket in buckets
for pair in bucket
put(pair.key, pair.val)
end
end
end
### ハッシュテーブルを出力 ###
def print
for bucket in @buckets
res = []
for pair in bucket
res << "#{pair.key} -> #{pair.val}"
end
pp res
end
end
end
### Driver Code ###
if __FILE__ == $0
# ## ハッシュテーブルを初期化
hashmap = HashMapChaining.new
# 追加操作
# ハッシュテーブルにキーと値の組 (key, value) を追加する
hashmap.put(12836, "シャオハー")
hashmap.put(15937, "シャオルオ")
hashmap.put(16750, "シャオスワン")
hashmap.put(13276, "シャオファー")
hashmap.put(10583, "シャオヤー")
puts "\n追加完了後、ハッシュテーブルは\n[Key1 -> Value1, Key2 -> Value2, ...]"
hashmap.print
# 検索操作
# ハッシュテーブルにキー key を入力し、値 value を取得する
name = hashmap.get(13276)
puts "\n学籍番号 13276 を入力すると、名前 #{name} が見つかりました"
# 削除操作
# ハッシュテーブルからキーと値の組 (key, value) を削除する
hashmap.remove(12836)
puts "\n12836 を削除した後、ハッシュテーブルは\n[Key1 -> Value1, Key2 -> Value2, ...]"
hashmap.print
end
@@ -0,0 +1,147 @@
=begin
File: hash_map_open_addressing.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative './array_hash_map'
### オープンアドレス法ハッシュテーブル ###
class HashMapOpenAddressing
TOMBSTONE = Pair.new(-1, '-1') # 削除済みマーク
### コンストラクタ ###
def initialize
@size = 0 # キーと値のペア数
@capacity = 4 # ハッシュテーブル容量
@load_thres = 2.0 / 3.0 # リサイズを発動する負荷率のしきい値
@extend_ratio = 2 # 拡張倍率
@buckets = Array.new(@capacity) # バケット配列
end
### ハッシュ関数 ###
def hash_func(key)
key % @capacity
end
### 負荷率 ###
def load_factor
@size / @capacity
end
### key に対応するバケットインデックスを検索 ###
def find_bucket(key)
index = hash_func(key)
first_tombstone = -1
# 線形プロービングを行い、空バケットに達したら終了
while !@buckets[index].nil?
# key が見つかったら、対応するバケットのインデックスを返す
if @buckets[index].key == key
# 以前に削除マークが見つかっていれば、そのインデックスへキーと値のペアを移動
if first_tombstone != -1
@buckets[first_tombstone] = @buckets[index]
@buckets[index] = TOMBSTONE
return first_tombstone # 移動後のバケットインデックスを返す
end
return index # バケットのインデックスを返す
end
# 最初に見つかった削除マークを記録
first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE
# バケットのインデックスを計算し、末尾を越えたら先頭に戻る
index = (index + 1) % @capacity
end
# key が存在しない場合は追加位置のインデックスを返す
first_tombstone == -1 ? index : first_tombstone
end
### 検索操作 ###
def get(key)
# key に対応するバケットインデックスを探す
index = find_bucket(key)
# キーと値の組が見つかったら、対応する val を返す
return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])
# キーと値のペアが存在しない場合は `nil` を返す
nil
end
### 追加操作 ###
def put(key, val)
# 負荷率がしきい値を超えたら、リサイズを実行
extend if load_factor > @load_thres
# key に対応するバケットインデックスを探す
index = find_bucket(key)
# キーと値のペアが見つかった場合は、`val` を上書きして返す
unless [nil, TOMBSTONE].include?(@buckets[index])
@buckets[index].val = val
return
end
# キーと値の組が存在しない場合は、その組を追加する
@buckets[index] = Pair.new(key, val)
@size += 1
end
### 削除操作 ###
def remove(key)
# key に対応するバケットインデックスを探す
index = find_bucket(key)
# キーと値の組が見つかったら、削除マーカーで上書きする
unless [nil, TOMBSTONE].include?(@buckets[index])
@buckets[index] = TOMBSTONE
@size -= 1
end
end
### ハッシュテーブルを拡張 ###
def extend
# 元のハッシュテーブルを一時保存
buckets_tmp = @buckets
# リサイズ後の新しいハッシュテーブルを初期化
@capacity *= @extend_ratio
@buckets = Array.new(@capacity)
@size = 0
# キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
for pair in buckets_tmp
put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)
end
end
### ハッシュテーブルを出力 ###
def print
for pair in @buckets
if pair.nil?
puts "Nil"
elsif pair == TOMBSTONE
puts "TOMBSTONE"
else
puts "#{pair.key} -> #{pair.val}"
end
end
end
end
### Driver Code ###
if __FILE__ == $0
# ハッシュテーブルを初期化
hashmap = HashMapOpenAddressing.new
# 追加操作
# ハッシュテーブルにキーと値の組 (key, val) を追加する
hashmap.put(12836, "シャオハー")
hashmap.put(15937, "シャオルオ")
hashmap.put(16750, "シャオスワン")
hashmap.put(13276, "シャオファー")
hashmap.put(10583, "シャオヤー")
puts "\n追加完了後、ハッシュテーブルは\nKey -> Value"
hashmap.print
# 検索操作
# ハッシュテーブルにキー key を入力し、値 val を得る
name = hashmap.get(13276)
puts "\n学籍番号 13276 を入力すると、名前 #{name} が見つかりました"
# 削除操作
# ハッシュテーブルからキーと値の組 (key, val) を削除する
hashmap.remove(16750)
puts "\n16750 を削除した後、ハッシュテーブルは\nKey -> Value"
hashmap.print
end
@@ -0,0 +1,62 @@
=begin
File: simple_hash.rb
Created Time: 2024-04-14
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### 加算ハッシュ ###
def add_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash += c.ord }
hash % modulus
end
### 乗算ハッシュ ###
def mul_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash = 31 * hash + c.ord }
hash % modulus
end
### XOR ハッシュ ###
def xor_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash ^= c.ord }
hash % modulus
end
### 回転ハッシュ ###
def rot_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }
hash % modulus
end
### Driver Code ###
if __FILE__ == $0
key = "Hello アルゴリズム"
hash = add_hash(key)
puts "加算ハッシュ値は #{hash}"
hash = mul_hash(key)
puts "乗算ハッシュ値は #{hash}"
hash = xor_hash(key)
puts "XORハッシュ値は #{hash}"
hash = rot_hash(key)
puts "ローテーションハッシュ値は #{hash}"
end