mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-04 03:14:09 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
122 lines
2.4 KiB
Ruby
122 lines
2.4 KiB
Ruby
=begin
|
|
File: array_hash_map.rb
|
|
Created Time: 2024-04-13
|
|
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|
=end
|
|
|
|
### Key-value pair ###
|
|
class Pair
|
|
attr_accessor :key, :val
|
|
|
|
def initialize(key, val)
|
|
@key = key
|
|
@val = val
|
|
end
|
|
end
|
|
|
|
### Hash map based on array ###
|
|
class ArrayHashMap
|
|
### Constructor ###
|
|
def initialize
|
|
# Initialize array with 100 buckets
|
|
@buckets = Array.new(100)
|
|
end
|
|
|
|
### Hash function ###
|
|
def hash_func(key)
|
|
index = key % 100
|
|
end
|
|
|
|
### Query operation ###
|
|
def get(key)
|
|
index = hash_func(key)
|
|
pair = @buckets[index]
|
|
|
|
return if pair.nil?
|
|
pair.val
|
|
end
|
|
|
|
### Add operation ###
|
|
def put(key, val)
|
|
pair = Pair.new(key, val)
|
|
index = hash_func(key)
|
|
@buckets[index] = pair
|
|
end
|
|
|
|
### Delete operation ###
|
|
def remove(key)
|
|
index = hash_func(key)
|
|
# Set to nil to delete
|
|
@buckets[index] = nil
|
|
end
|
|
|
|
### Get all key-value pairs ###
|
|
def entry_set
|
|
result = []
|
|
@buckets.each { |pair| result << pair unless pair.nil? }
|
|
result
|
|
end
|
|
|
|
### Get all keys ###
|
|
def key_set
|
|
result = []
|
|
@buckets.each { |pair| result << pair.key unless pair.nil? }
|
|
result
|
|
end
|
|
|
|
### Get all values ###
|
|
def value_set
|
|
result = []
|
|
@buckets.each { |pair| result << pair.val unless pair.nil? }
|
|
result
|
|
end
|
|
|
|
### Print hash table ###
|
|
def print
|
|
@buckets.each { |pair| puts "#{pair.key} -> #{pair.val}" unless pair.nil? }
|
|
end
|
|
end
|
|
|
|
### Driver Code ###
|
|
if __FILE__ == $0
|
|
# Initialize hash table
|
|
hmap = ArrayHashMap.new
|
|
|
|
# Add operation
|
|
# Add key-value pair (key, value) to the hash table
|
|
hmap.put(12836, "Xiao Ha")
|
|
hmap.put(15937, "Xiao Luo")
|
|
hmap.put(16750, "Xiao Suan")
|
|
hmap.put(13276, "Xiao Fa")
|
|
hmap.put(10583, "Xiao Ya")
|
|
puts "\nAfter adding is complete, hash table is\nKey -> Value"
|
|
hmap.print
|
|
|
|
# Query operation
|
|
# Input key to hash table, get value
|
|
name = hmap.get(15937)
|
|
puts "\nInput student ID 15937, found name #{name}"
|
|
|
|
# Remove operation
|
|
# Delete key-value pair (key, value) from hash table
|
|
hmap.remove(10583)
|
|
puts "\nAfter removing 10583, hash table is\nKey -> Value"
|
|
hmap.print
|
|
|
|
# Traverse hash table
|
|
puts "\nTraverse key-value pairs Key->Value"
|
|
for pair in hmap.entry_set
|
|
puts "#{pair.key} -> #{pair.val}"
|
|
end
|
|
|
|
puts "\nTraverse keys separately"
|
|
for key in hmap.key_set
|
|
puts key
|
|
end
|
|
|
|
puts "\nTraverse values only Value"
|
|
for val in hmap.value_set
|
|
puts val
|
|
end
|
|
end
|