Ctrl K

Folding Digits in Groups

Easy
You are given a string of digits digits and a number size. While the string is longer than size, run a round: 1. Split the string from left to right into groups of size characters (the last group may be shorter). 2. Replace each group with the sum of its digits, written in decimal. For example, the group "479" becomes "20". 3. Join the results in order; that is the new string. Return the string you are left with once no more rounds apply.
Example 1
digits="5823917", size=3 → '710'

Round 1: "582", "391", "7" → 15, 13, 7 → "15137". Round 2: "151", "37" → 7, 10 → "710". Its length 3 does not exceed size, so we stop.

Example 2
digits="4096", size=4 → '4096'

The string is already no longer than size, so no rounds happen.

Example 3
digits="000000", size=2 → '00'
Constraints
  • 1 ≤ len(digits) ≤ 100
  • 2 ≤ size ≤ 100
  • digits contains only digits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyCounting Off by Threes and Fives EasyAdding Long Numbers in Strings EasyDrone Back on the Pad EasySame Text After Erasing Easy
loading Python…
Input
digits="5823917", size=3
Expected
'710'

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