-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproquint.rb
More file actions
72 lines (55 loc) · 1.94 KB
/
proquint.rb
File metadata and controls
72 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def uid_to_proquint(uid)
# Step 1: Convert UID to binary
binary_representation =
if uid.is_a?(Numeric)
uid.to_s(2)
elsif uid.is_a?(String)
uid.unpack('B*').first
else
raise ArgumentError, 'UID must be a Numeric or String'
end
# Step 2: Split Binary into 5-bit Chunks
chunks = binary_representation.chars.each_slice(5).map { |chunk| chunk.join.ljust(5, '0') }
# Step 3: Map to Proquints
proquints = chunks.map { |chunk| map_to_proquint(chunk) }
# Step 4: Combine Proquints with Dash and Remove Trailing Dash
proquint_result = proquints.join
proquint_result = proquint_result.gsub(/(.{3})(?!$)/, '\1-')
# Step 5: Shorten Result
proquint_result = proquint_result[0, 11].chomp('-')
return proquint_result
end
def map_to_proquint(chunk)
# Create a mapping table for 5-bit binary to consonant-vowel pairs
mapping_table = "bdfghjklmnpqrstvwxz"
vowels = "aeiou"
# Convert binary to decimal and map to consonant-vowel pair
decimal_value = chunk.to_i(2)
consonant = mapping_table[decimal_value / 5]
vowel = vowels[decimal_value % 5]
return "#{consonant}#{vowel}"
end
# Example usage with numeric UID
numeric_uid = 123_456_789
proquint_result_numeric = uid_to_proquint(numeric_uid)
puts "Numeric UID: #{numeric_uid} -> Proquint: #{proquint_result_numeric}"
# Example usage with string UID
string_uid = "abc123"
proquint_result_string = uid_to_proquint(string_uid)
puts "String UID: #{string_uid} -> Proquint: #{proquint_result_string}"
# ...
# From Scratch
def generate_random_uid(length)
rand(2**(length - 1)..2**length - 1)
end
def create_proquint_from_scratch(length)
# Step 1: Generate a random UID
uid = generate_random_uid(length)
# Step 2: Convert UID to Proquint
proquint_result = uid_to_proquint(uid)
return uid, proquint_result
end
# Example usage
uid_length = 22
uid, proquint_result = create_proquint_from_scratch(uid_length)
puts "Generated UID: #{uid} -> Proquint: #{proquint_result}"