mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
63 lines
1006 B
Ruby
63 lines
1006 B
Ruby
=begin
|
|
File: simple_hash.rb
|
|
Created Time: 2024-04-14
|
|
Author: Xuan Khoa Tu Nguyen ([email protected])
|
|
=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
|
|
|
|
### 실행 코드 ###
|
|
if __FILE__ == $0
|
|
key = "Hello 算法"
|
|
|
|
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
|