Statistics

Problem Statement for "PhoneNum"

Problem Statement

PROBLEM STATEMENT

Jimmy loves to order things over the phone.  However, sometimes he sees phone
numbers with letters in them, and his old phone only has numbers printed on all
of the buttons.  In order to convert from the on-screen telephone number to the
real telephone number, he must substitute each letter with a number, according
the following table:

Letter      Number
======      ======
A B C       2
D E F       3
G H I       4
J K L       5
M N O       6
P R S       7
T U V       8
W X Y       9
Q Z         0

Create a class PhoneNum that contains the method getNum, which takes a phone
number containing numbers, upper-case letters, and dashes, and returns a
neatly-formatted telephone number for Jimmy to dial.

DEFINITION
Class: PhoneNum
Method: getNum
Parameters: String
Returns: String
Method signature (be sure your method is public): String getNum(String telnum);

NOTES
- Note that in real-life, some telephones place the letter 'Q' on the number 7
and the letter 'Z' on the number 9, and some telephones leave the two letters
out altogether.  For this problem, strictly follow the above table, paying no
attention to the standard for real telephones.
- There are only two types of telephone numbers: 7-digit numbers for local
calls, and 10-digit numbers for long-distance calls (do NOT add a "1" to
long-distance numbers).  So, if the processed telephone number contains 10 or
more digits, return the first 10 digits in the format (without quotes):
"(<first 3 digits>)<next 3 digits>-<next 4 digits>".  Likewise, if the number
contains between 7 and 9 digits, inclusive, return the first 7 digits in the
format (without quotes): "<first 3 digits>-<next 4 digits>".  If the number
contains less than 7 digits, return "INVALID".

TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
- telnum contains only digits ('0'-'9' inclusive), upper-case letters
('A'-'Z'), and dashes ('-').
- telnum does not begin or end with a dash.
- telnum does not contain 2 or more consecutive dashes.
- telnum contains between 1 and 20 characters, inclusive.

EXAMPLES

=========
Example 1
=========
telnum = "800-CALL-NOW"
The method returns "(800)225-5669"

=========
Example 2
=========
telnum = "800-PHONE-NUMBER"
The method returns "(800)746-6368"

=========
Example 3
=========
telnum = "TELEPHONE"
The method returns "835-3746"

=========
Example 4
=========
telnum = "Q-U-E-S-T-I-O-N-S"
The method returns "083-7846"

=========
Example 5
=========
telnum = "DIAL-IT"
The method returns "INVALID"

=========
Example 6
=========
telnum = "12-34-56-789-1011"
The method returns "(123)456-7891"

Definition

Class:
PhoneNum
Method:
getNum
Parameters:
String
Returns:
String
Method signature:
String getNum(String param0)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: