A parcel-tracking terminal can only display a numeric tracking code once the code's length has shrunk to a fixed number of characters or fewer. Given a long digit-only tracking code and the terminal's display width, repeatedly compress the code as follows: split the current code into consecutive chunks of exactly the display-width many characters (the final chunk may be shorter if the length does not divide evenly), replace each chunk with the decimal digit string of the sum of that chunk's individual digits (so a chunk like "329" becomes "14", since 3 + 2 + 9 = 14), and concatenate the replacement chunks, in their original order, to form the next code. Keep repeating this folding step until the code's length is at most the display width, then report that final code exactly as it appears (leading zeros, if any remain, are kept).
A single line containing the final folded code (a string of digits).
Example 1
Input
493207 3
Expected
169
Explanation
s has length 6 > k = 3, so it splits into chunks "493" and "207". Their digit sums are 4+9+3=16 and 2+0+7=9, giving the next code "169". Its length 3 is now at most k = 3, so folding stops and "169" is the answer.
Example 2
Input
7891234 4
Expected
259
Explanation
s has length 7 > k = 4, so it splits into chunks "7891" and "234" (the last chunk is shorter). Their digit sums are 7+8+9+1=25 and 2+3+4=9, giving the next code "259". Its length 3 is now at most k = 4, so folding stops and "259" is the answer.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →