TopcoderArchive
Archive
Algorithm problem

PulseDial

Problem statement, definition, constraints, and public examples.

Problem Statement

There are two methods used by phones to dial phone numbers, and one of them is pulse dial. In pulse dial, a phone number is represented as a sequence of pulses and pauses. Each maximal consecutive sequence of pulses represents a single digit. We will use the character '-' to denote a pulse and '*' to denote a pause. Digit '1' is represented as '-', digit '2' is represented as '--', and so on. Digit '0' consists of 10 dashes.

Given a String containing a sequence of pulses and pauses, return the phone number that it represents. If it is a 7-digit number, format the String as "XXX-XX-XX", and if it is an 11-digit number, prefix it with a '+' character: "+XXXXXXXXXXX". The phone number is guaranteed to contain exactly 7 or 11 digits.

Definition

Class:
PulseDial
Method:
toDigits
Parameters:
String
Returns:
String
Method signature:
String toDigits(String raw)
(be sure your method is public)

Notes

  • Maximal consecutive sequence of pulses are separated by one or more pauses.
  • Pauses can start and finish input data.

Constraints

  • raw will contain between 1 and 50 characters, inclusive.
  • raw will contain only '-' and '*' characters.
  • raw will contain 7 or 11 maximal consecutive sequences of '-'.
  • Each maximal consecutive sequence of '-' will contain between 1 and 10 characters, inclusive.

Examples

  1. "---***-*--*-*--*-******--***"
    Returns: "312-12-12"
  2. "-*--*-----*------*---*----*-"
    Returns: "125-63-41"
  3. "--**--**--**--**--**--**--**--**--**--**--******"
    Returns: "+22222222222"
  4. "*-*****-**-*--***--*****--***-***--**-**---**----*"
    Returns: "+11122212134"
Back to all problems