mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 22:46: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
|
||||
|
||||
Reference in New Issue
Block a user