Skip to content

Instantly share code, notes, and snippets.

@stephancom
Last active January 16, 2019 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephancom/41b9e0bf9c0a82bfbb9b33b42d5da9ea to your computer and use it in GitHub Desktop.
Save stephancom/41b9e0bf9c0a82bfbb9b33b42d5da9ea to your computer and use it in GitHub Desktop.
Phone Format test
#! /usr/bin/ruby
# _ _
# ___| |_ ___ _ __ | |__ __ _ _ __ ___ ___ _ __ ___
# / __| __/ _ \ '_ \| '_ \ / _` | '_ \ / __/ _ \| '_ ` _ \
# \__ \ || __/ |_) | | | | (_| | | | || (_| (_) | | | | | |
# |___/\__\___| .__/|_| |_|\__,_|_| |_(_)___\___/|_| |_| |_|
# |_|
# coding challenge for Simplero
# https://engineering.simplero.com/coding-challenge
# phone_format formats a phone number, discarding all non-digit characters
# and formatting it in groups of 3 digits, separated by hyphens, such that
# the last group will never have a single digit.
# phone_format("555 123 1234") => "555-123-12-34"
# phone_format("(+1) 888 33x19") => "188-833-19"
# I tried initially solving this with just a regexp.
# I was quickly reminded of this quote:
# > Some people, when confronted with a problem, think "I know,
# > I'll use regular expressions." Now they have two problems.
# - Jamie Zawinski
# https://en.wikiquote.org/wiki/Jamie_Zawinski#Attributed
# monkey patch string
# cribbed from https://apidock.com/rails/String/last
class String
def first(limit = 1)
return '' if limit.zero?
return dup if limit >= size
self[0..(limit - 1)]
end
def last(limit = 1)
return '' if limit.zero?
return dup if limit >= size
self[-limit..-1]
end
end
# gratuitous use of lambdas
def maketest(method)
lambda do |input, result|
if (actual = send(method, input)) == result
"OK: success with #{input} == #{result}"
else
"#{input} failed with #{actual}"
end
end
end
# why, yes, you should be able to generalize group size and minimum.
# but I did not.
def phone_format(string)
str = string.tr('^0-9', '')
return str if str.length < 4
if str.length % 3 == 1
str.first(-4).scan(/.{3}/) + str.last(4).scan(/.{2}/)
else
str.scan(/.{2,3}/)
# to put the smaller group at the beginning
# str.reverse.scan(/.{2,3}/).map(&:reverse).reverse
end.join('-')
end
pftest = maketest(:phone_format)
puts pftest.call('555 123 1234', '555-123-12-34')
puts pftest.call('(+1) 888 33x19', '188-833-19')
puts pftest.call('310.384.3771', '310-384-37-71') # my cell
puts pftest.call('12!345...6, 7, 8.', '123-456-78') # abomination
puts pftest.call('88888888', '888-888-88') # I prefer 88-888-888
puts pftest.call('7777777', '777-77-77')
puts pftest.call('666665', '666-665') # the beast, and his neighbor
puts pftest.call('55555', '555-55') # but I prefer 55-555
puts pftest.call('4444', '44-44')
puts pftest.call('333', '333')
puts pftest.call('22', '22')
puts pftest.call('1', '1') # mind, blown.
puts pftest.call('', '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment