I was recently given this problem and tasked with coding a solution. My efforts so far have come close but not yet solved it.
Essentially, the problem involves creating a function which is passed a string of numbers (no validation required), for example '12345'.
I have to code the function (using Python preferably) that adds all the consecutive combination of digits. For the above example, this would be:
12345 + 1 + 2 + 3 + 4 + 5
+ 12 + 23 + 34 + 45
+ 123 + 234 + 345
+ 1234 + 2345
I have got some things working, for example:
#code to add the individual numbers
indivInts = [int(d) for d in stringNumber]
for i in indivInts:
total += i
#code to add 12 + 123 + 12345, etc
for i in range(len(stringNumber)-2):
s = ''.join([str(x) for x in indivInts[:i+2]])
print('Adding loop 2: ' + s)
total += int(s)
The issue I seem to be having is with the 'middle' numbers, ie 234, 34, etc.
The function should be able to take any string of an integer and still work.
123456or12345?