mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-26 21:16:07 +00:00
Migrate to Zensical (#1869)
* Fix Russian Ruby code extraction. * Add zensical configs.
This commit is contained in:
@@ -4,7 +4,7 @@ Created Time: 2024-04-13
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Пара ключ-значение ###
|
||||
### Пара ключ-значение ###
|
||||
class Pair
|
||||
attr_accessor :key, :val
|
||||
|
||||
@@ -14,20 +14,20 @@ class Pair
|
||||
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]
|
||||
@@ -36,42 +36,42 @@ class ArrayHashMap
|
||||
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
|
||||
|
||||
@@ -6,9 +6,9 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
|
||||
require_relative './array_hash_map'
|
||||
|
||||
# ## Хеш-таблица с цепочками ###
|
||||
### Хеш-таблица с цепочками ###
|
||||
class HashMapChaining
|
||||
# ## Конструктор ###
|
||||
### Конструктор ###
|
||||
def initialize
|
||||
@size = 0 # Число пар ключ-значение
|
||||
@capacity = 4 # Вместимость хеш-таблицы
|
||||
@@ -17,17 +17,17 @@ class HashMapChaining
|
||||
@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]
|
||||
@@ -39,7 +39,7 @@ class HashMapChaining
|
||||
nil
|
||||
end
|
||||
|
||||
# ## Операция добавления ###
|
||||
### Операция добавления ###
|
||||
def put(key, val)
|
||||
# Когда коэффициент загрузки превышает порог, выполнить расширение
|
||||
extend if load_factor > @load_thres
|
||||
@@ -58,7 +58,7 @@ class HashMapChaining
|
||||
@size += 1
|
||||
end
|
||||
|
||||
# ## Операция удаления ###
|
||||
### Операция удаления ###
|
||||
def remove(key)
|
||||
index = hash_func(key)
|
||||
bucket = @buckets[index]
|
||||
@@ -72,7 +72,7 @@ class HashMapChaining
|
||||
end
|
||||
end
|
||||
|
||||
# ## Расширение хеш-таблицы ###
|
||||
### Расширение хеш-таблицы ###
|
||||
def extend
|
||||
# Временно сохранить исходную хеш-таблицу
|
||||
buckets = @buckets
|
||||
@@ -88,7 +88,7 @@ class HashMapChaining
|
||||
end
|
||||
end
|
||||
|
||||
# ## Вывести хеш-таблицу ###
|
||||
### Вывести хеш-таблицу ###
|
||||
def print
|
||||
for bucket in @buckets
|
||||
res = []
|
||||
|
||||
@@ -6,11 +6,11 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
|
||||
require_relative './array_hash_map'
|
||||
|
||||
# ## Хеш-таблица с открытой адресацией ###
|
||||
### Хеш-таблица с открытой адресацией ###
|
||||
class HashMapOpenAddressing
|
||||
TOMBSTONE = Pair.new(-1, '-1') # Удалить метку
|
||||
|
||||
# ## Конструктор ###
|
||||
### Конструктор ###
|
||||
def initialize
|
||||
@size = 0 # Число пар ключ-значение
|
||||
@capacity = 4 # Вместимость хеш-таблицы
|
||||
@@ -19,17 +19,17 @@ class HashMapOpenAddressing
|
||||
@buckets = Array.new(@capacity) # Массив корзин
|
||||
end
|
||||
|
||||
# ## Хеш-функция ###
|
||||
### Хеш-функция ###
|
||||
def hash_func(key)
|
||||
key % @capacity
|
||||
end
|
||||
|
||||
# ## Коэффициент загрузки ###
|
||||
### Коэффициент загрузки ###
|
||||
def load_factor
|
||||
@size / @capacity
|
||||
end
|
||||
|
||||
# ## Найти индекс корзины, соответствующий key ###
|
||||
### Найти индекс корзины, соответствующий key ###
|
||||
def find_bucket(key)
|
||||
index = hash_func(key)
|
||||
first_tombstone = -1
|
||||
@@ -54,7 +54,7 @@ class HashMapOpenAddressing
|
||||
first_tombstone == -1 ? index : first_tombstone
|
||||
end
|
||||
|
||||
# ## Операция поиска ###
|
||||
### Операция поиска ###
|
||||
def get(key)
|
||||
# Найти индекс корзины, соответствующий key
|
||||
index = find_bucket(key)
|
||||
@@ -64,7 +64,7 @@ class HashMapOpenAddressing
|
||||
nil
|
||||
end
|
||||
|
||||
# ## Операция добавления ###
|
||||
### Операция добавления ###
|
||||
def put(key, val)
|
||||
# Когда коэффициент загрузки превышает порог, выполнить расширение
|
||||
extend if load_factor > @load_thres
|
||||
@@ -80,7 +80,7 @@ class HashMapOpenAddressing
|
||||
@size += 1
|
||||
end
|
||||
|
||||
# ## Операция удаления ###
|
||||
### Операция удаления ###
|
||||
def remove(key)
|
||||
# Найти индекс корзины, соответствующий key
|
||||
index = find_bucket(key)
|
||||
@@ -91,7 +91,7 @@ class HashMapOpenAddressing
|
||||
end
|
||||
end
|
||||
|
||||
# ## Расширение хеш-таблицы ###
|
||||
### Расширение хеш-таблицы ###
|
||||
def extend
|
||||
# Временно сохранить исходную хеш-таблицу
|
||||
buckets_tmp = @buckets
|
||||
@@ -105,7 +105,7 @@ class HashMapOpenAddressing
|
||||
end
|
||||
end
|
||||
|
||||
# ## Вывести хеш-таблицу ###
|
||||
### Вывести хеш-таблицу ###
|
||||
def print
|
||||
for pair in @buckets
|
||||
if pair.nil?
|
||||
|
||||
@@ -4,7 +4,7 @@ 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
|
||||
@@ -14,7 +14,7 @@ def add_hash(key)
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## Мультипликативное хеширование ###
|
||||
### Мультипликативное хеширование ###
|
||||
def mul_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
@@ -24,7 +24,7 @@ def mul_hash(key)
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## XOR-хеширование ###
|
||||
### XOR-хеширование ###
|
||||
def xor_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
@@ -34,7 +34,7 @@ def xor_hash(key)
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## Хеширование с циклическим сдвигом ###
|
||||
### Хеширование с циклическим сдвигом ###
|
||||
def rot_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
Reference in New Issue
Block a user