Ctrl K

Regroup a Serial Code

Easy
A serial code code is made of English letters, digits and dashes, with the dashes placed arbitrarily. You are also given a number size. Rewrite the code as follows: remove every dash, turn every letter uppercase, then split the characters into groups joined by - so that each group has exactly size characters. Only the first group may be shorter, but it must not be empty. If the code has nothing but dashes, return an empty string.
Example 1
code="x7-k2p-9q", size=3 → 'X-7K2-P9Q'

Without dashes we have X7K2P9Q, 7 characters. Cutting groups of 3 from the end gives P9Q and 7K2, leaving X at the front.

Example 2
code="ab-cd", size=4 → 'ABCD'

All four characters fit into a single group.

Example 3
code="---", size=2 → ''

There is nothing but dashes, so the answer is an empty string.

Constraints
  • 0 ≤ len(code) ≤ 10⁵
  • code consists of English letters, digits and -
  • 1 ≤ size ≤ 10⁴
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
code="x7-k2p-9q", size=3
Expected
'X-7K2-P9Q'

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