mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-17 20:17:14 +00:00
Add ru version (#1865)
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
This commit is contained in:
@@ -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 Algo"
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user