Last active
April 13, 2020 21:47
-
-
Save ch1ago/8a3e0638203dd9e774313dd84c073ca5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module CpfJames | |
module_function | |
def protect(string) | |
string.split(' ').map { |s| valid?(s) ? mask(s) : s }.join(' ') | |
end | |
def valid?(s) | |
!!s.match(/\d{3}.\d{3}.\d{3}-\d{2}/) | |
end | |
def mask(s) | |
md = s.match(/(\d\d)\d.(\d{3}).(\d{3})-(\d{2})/) | |
"#{md[1]}X.#{md[2]}.#{md[3]}-#{md[4]}" | |
end | |
end | |
module CpfAndre | |
module_function | |
def protect(string) | |
string.split('').inject('') { |acc, char| mask_protected_chars(char, acc) } | |
end | |
def mask_protected_chars(char, acc) | |
should_mask?(char, acc.size) ? acc + 'X' : acc + char | |
end | |
def should_mask?(char, char_index) | |
is_a_digit = char.match?(/\d/) | |
in_the_first_6_digits = char_index <= 7 | |
value_lesser_than_index = char.to_i <= char_index | |
is_a_digit && in_the_first_6_digits && value_lesser_than_index | |
end | |
end | |
module CpfAndreProc | |
module_function | |
def protect(text) | |
tmp_string = '' | |
text.split('').each do |i| | |
if i.match?(/\d/) && tmp_string.size <= 7 && i.to_i <= tmp_string.size | |
tmp_string += 'X' | |
else | |
tmp_string += i | |
end | |
end | |
tmp_string | |
end | |
end | |
USE_CASE = "120.979.700-71 BRASIL 254" | |
EXPECTED = "12X.979.700-71 BRASIL 254" | |
puts "CpfJames: #{CpfJames.protect(USE_CASE)}" | |
puts "CpfAndre: #{CpfAndre.protect(USE_CASE)}" | |
puts "CpfAndreProc: #{CpfAndreProc.protect(USE_CASE)}" | |
raise 'James' if CpfJames.protect(USE_CASE) != EXPECTED | |
raise 'Andre' if CpfAndre.protect(USE_CASE) != EXPECTED | |
raise 'AndreProc' if CpfAndreProc.protect(USE_CASE) != EXPECTED | |
require 'benchmark' | |
n = 1_000_000 #_000 | |
Benchmark.bm do |x| | |
x.report("#{n} runs, James:") { n.times { CpfJames.protect USE_CASE } } | |
x.report("#{n} runs, Andre:") { n.times { CpfAndre.protect USE_CASE } } | |
x.report("#{n} runs, CpfAndreProc:") { n.times { CpfAndreProc.protect USE_CASE } } | |
end |
This file contains hidden or 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
ruby cpf_andre_vs_james.rb | |
CpfJames: 12X.979.700-71 BRASIL 254 | |
CpfAndre: 12X.979.700-71 BRASIL 254 | |
CpfAndreProc: 12X.979.700-71 BRASIL 254 | |
user system total real | |
1000000 runs, James: 9.072773 0.074202 9.146975 ( 9.393672) | |
1000000 runs, Andre: 17.585979 0.075285 17.661264 ( 17.816493) | |
1000000 runs, CpfAndreProc: 12.549021 0.045396 12.594417 ( 12.672436) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment