Ctrl K

Phone Number in Groups

Easy
People type phone numbers any way they like: with spaces, with dashes, or all run together. The string raw consists of digits, spaces and dashes. Bring the number to one standard look. 1. Remove every space and dash so only the digits remain. 2. Going left to right, cut off blocks of three digits while more than four digits are left. 3. Group the rest as follows: 2 digits make one block of two, 3 digits make one block of three, 4 digits make two blocks of two. Join the blocks with dashes and return the result. Note that a block of a single digit never appears.
Example 1
raw="8 800-55 53" → '880-055-53'

The digits are 88005553, eight of them. First 880, then 055, and the two remaining digits 53 form one block.

Example 2
raw="4-0-7 1" → '40-71'

There are exactly four digits, so they split into two blocks of two: 40-71.

Example 3
raw="12 34567" → '123-45-67'
Constraints
  • 2 ≤ len(raw) ≤ 100
  • raw consists of digits, spaces and - characters
  • raw contains at least two digits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
raw="8 800-55 53"
Expected
'880-055-53'

Run checks the first 3 tests in your browser. Submit checks all 162 tests on the server, hidden ones included.