mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Bug fixes and improvements (#1298)
* Fix is_empty() implementation in the stack and queue chapter * Update en/CONTRIBUTING.md * Remove "剩余" from the state definition of knapsack problem * Sync zh and zh-hant versions * Update the stylesheets of code tabs * Fix quick_sort.rb * Fix TS code * Update chapter_paperbook * Upload the manuscript of 0.1 section * Fix binary_tree_dfs.rb * Bug fixes * Update README * Update README * Update README * Update README.md * Update README * Sync zh and zh-hant versions * Bug fixes
This commit is contained in:
121
zh-hant/codes/ruby/chapter_hashing/array_hash_map.rb
Normal file
121
zh-hant/codes/ruby/chapter_hashing/array_hash_map.rb
Normal file
@ -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 "\n刪除 10583 後,雜湊表為\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
|
||||
34
zh-hant/codes/ruby/chapter_hashing/built_in_hash.rb
Normal file
34
zh-hant/codes/ruby/chapter_hashing/built_in_hash.rb
Normal file
@ -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
zh-hant/codes/ruby/chapter_hashing/hash_map.rb
Normal file
44
zh-hant/codes/ruby/chapter_hashing/hash_map.rb
Normal 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 "\n刪除 10583 後,雜湊表為\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
|
||||
128
zh-hant/codes/ruby/chapter_hashing/hash_map_chaining.rb
Normal file
128
zh-hant/codes/ruby/chapter_hashing/hash_map_chaining.rb
Normal file
@ -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 "\n刪除 12836 後,雜湊表為\n[Key1 -> Value1, Key2 -> Value2, ...]"
|
||||
hashmap.print
|
||||
end
|
||||
147
zh-hant/codes/ruby/chapter_hashing/hash_map_open_addressing.rb
Normal file
147
zh-hant/codes/ruby/chapter_hashing/hash_map_open_addressing.rb
Normal file
@ -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 "\n刪除 16750 後,雜湊表為\nKey -> Value"
|
||||
hashmap.print
|
||||
end
|
||||
62
zh-hant/codes/ruby/chapter_hashing/simple_hash.rb
Normal file
62
zh-hant/codes/ruby/chapter_hashing/simple_hash.rb
Normal file
@ -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
|
||||
|
||||
### 互斥或雜湊 ###
|
||||
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 "互斥或雜湊值為 #{hash}"
|
||||
|
||||
hash = rot_hash(key)
|
||||
puts "旋轉雜湊值為 #{hash}"
|
||||
end
|
||||
Reference in New Issue
Block a user