Created
September 14, 2018 19:50
-
-
Save chandeeland/f3a36b1355b6de21d58675ad45f421e9 to your computer and use it in GitHub Desktop.
check number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckNumber | |
ALPHABET = (2..9).to_a + ('a'..'z').to_a - %w[o i] | |
# ALPHABET = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a + %w[ @ & ] | |
def initialize(uuid, size = 4, chunk_size = 4) | |
@uuid = uuid | |
@size = size | |
@chunk_size = chunk_size | |
end | |
def run | |
return chunks if size != chunk_size | |
chars.join | |
end | |
private | |
attr_reader :uuid, :size, :chunk_size | |
def chars | |
num = uuid.delete('-').to_i(16) | |
Array.new(size).map.with_index do |elem, i| | |
curr = ALPHABET.size | |
ALPHABET[num % (curr).to_i].tap do | |
num /= curr | |
end | |
end | |
end | |
def chunks | |
chars | |
.each_slice( chunk_size ) | |
.map(&:join) | |
.join(' ') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment