Beat the previous

Beat the previous

Tags: Data Structures Algorithms Sequences Optimization

Credit: Ilkka Kokkarinen

Difficulty: Easy

Type: Text

Category: Practice

Time Allowed: 15 s

Description:

Given a string of digits guaranteed to only contain ordinary integer digit characters 0 to 9, create and return the list of increasing integers acquired from reading these digits in order from left to right. The first integer in the result list is made up from the first digit of the string. After that, each element is an integer that consists of as many following consecutive digits as are needed to make that integer strictly larger than the previous integer. Any leftover digits at the end of the digit string that do not form a sufficiently large integer are ignored.

This problem can be solved with a for-loop through the digits that looks at each digit exactly once regardless of the position of that digit in the beginning, end or middle of the string. Keep track of the current number (initially zero) and the previous number to beat (initially equal to minus one). Each digit d is then processed by pinning it at the end of current number with the assignment current=10*current+int(d), updating the result and previous as needed.

Test Case Input Expected Output
1 '600005' [6]
2 '045349' [0, 4, 5, 34]
3 '77777777777777777777777' [7, 77, 777, 7777, 77777, 777777]
4 '122333444455555666666' [1, 2, 23, 33, 44, 445, 555, 566, 666]
5 '2718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260' [2, 7, 18, 28, 182, 845, 904, 5235, 36028, 74713, 526624, 977572, 4709369, 9959574, 96696762, 772407663, 3535475945, 7138217852, 51664274274, 66391932003, 599218174135, 966290435729]

Please register or login to submit solutions to challenges.