Extract increasing integers from digit string def extract_increasing(digits): 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. The 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 one 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 minus one). Each digit d then updates current = 10*current+d to tack that digit to the end of current. digits Expected result '045349' [0, 4, 5, 34] '77777777777777777777777' [7, 77, 777, 7777, 77777, 777777] '122333444455555666666' [1, 2, 23, 33, 44, 445, 555, 566, 666] '1234567890987654321' [1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 765, 4321]
Extract increasing integers from digit string
def extract_increasing(digits):
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. The 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 one 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 minus one). Each digit d then updates current = 10*current+d to tack that digit to the end of current.
digits | Expected result |
'045349' | [0, 4, 5, 34] |
'77777777777777777777777' | [7, 77, 777, 7777, 77777, 777777] |
'122333444455555666666' | [1, 2, 23, 33, 44, 445, 555, 566, 666] |
'1234567890987654321' | [1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 765, 4321] |
'3141592653589793238462643 383279502884' |
[3, 14, 15, 92, 653, 5897, 9323, 84626, 433832, 795028] |
'2718281828459045235360287 47135266249775724709369995 95749669676277240766303535 47594571382178525166427427 46639193200305992181741359 6629043572900334295260' |
[2, 7, 18, 28, 182, 845, 904, 5235, 36028, 74713, 526624, 977572, 4709369, 9959574, 96696762, 772407663, 3535475945, 7138217852, 51664274274, 66391932003, 599218174135, 966290435729] |
'123456789' * 100 | A list with 75 elements, the last one equal to 34567891234567891234567891 |
Trending now
This is a popular solution!
Step by step
Solved in 2 steps