Skip to content

Instantly share code, notes, and snippets.

@sshaw
Last active February 26, 2017 02:53
Show Gist options
  • Save sshaw/29d6f7379771e3b4596e228b626bcf9a to your computer and use it in GitHub Desktop.
Save sshaw/29d6f7379771e3b4596e228b626bcf9a to your computer and use it in GitHub Desktop.
Convert a Vanity Phone Number With Letters to One With Numbers Only
# 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