Last active
February 26, 2017 02:53
-
-
Save sshaw/29d6f7379771e3b4596e228b626bcf9a to your computer and use it in GitHub Desktop.
Convert a Vanity Phone Number With Letters to One With Numbers Only
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
# https://gist.github.com/sshaw/29d6f7379771e3b4596e228b626bcf9a | |
def convert(chr) | |
chr = chr.upcase | |
# subtract "A" | |
n = (chr.ord - 65) / 3 | |
# account for #7 & #9 which have 4 chars | |
n -= 1 if chr == "S".freeze || chr == "V".freeze || chr >= "Y".freeze | |
(n + 2).to_s | |
end | |
puts "180044STERN".gsub(/[a-z]/i) { |chr| convert(chr) } | |
puts "555AXYZ".gsub(/[a-z]/i) { |chr| convert(chr) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment