Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Created May 23, 2022 20:00
Show Gist options
  • Save joalbertg/7d95c133d9cc5d2442c4db71da46b847 to your computer and use it in GitHub Desktop.
Save joalbertg/7d95c133d9cc5d2442c4db71da46b847 to your computer and use it in GitHub Desktop.
challenge: phone number
# Task executed successfully.
# ---------------------------------
# Input parameters: ("0151-319723")
# Result: No
# OK
# ---------------------------------
# Input parameters: ("(123) 456-7890")
# Result: 1234567890
# OK
# ---------------------------------
# Input parameters: ("(137) 811-0877")
# Result: 1378110877
# OK
# ---------------------------------
# Input parameters: ("(66) 030-2617")
# Result: No
# OK
No
1234567890
1378110877
No
# frozen_string_literal: true
# You are for a customer support center and have been asked to help update the company's extensive phone directory.
# In order to check which phone numbers are still valid and which need updating, you need to write a short program that:
# - Checks wether a phone number (given as string str) in the database is in the format (XXX) XXX-XXXX. And then
# - If the number is in the correct format, returns it as a continues 10-digit string (called convertNumber)
# - If the number is not in the correct format, returns "No"
# Take the following into account:
# 1 <= |str| <= 100
# Example 1:
# Input: (123) 456-7890
# Output: 1234567890
# Explanation
# Since the input is in the correct format, your code will convert it into a continuous 10-digit string. The outcome,
# therefore, is 1234567890.
# Example 2:
# Input: 0151-319723
# Output: No
# Explanation:
# Since the input is not in the correct format, your code will return the output No.
def convert_number(str)
regex = str.match(/\((\d{3})\) (\d{3})-(\d{4})/)
return 'No' unless regex
regex.captures.join
end
puts convert_number('0151-319723')
puts convert_number('(123) 456-7890')
puts convert_number('(137) 811-0877')
puts convert_number('(66) 030-2617')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment