+ 1
Question from coding bat practice
My question: why is the result += str[:i+1] instead of result += str[:i] from my understanding #when i = 0 result = str[0:0] = C #when i = 1 result = C + str[0:1] = C + CO #when i =2 result = C + CO + str [0:2] = C + CO + COD #when i = 3 result = C + CO + COD + str[0:3] result= CCOCODCODE https://code.sololearn.com/c3Djr94rOEk3/?ref=app
2 odpowiedzi
+ 5
range(x) generates numbers from 0 up to x-1 (x is not included)
When you slice the string, str[:i] takes the indexes from 0 up to i-1 (i is not included and remember index starts with 0)
+ 1
C: 0
O: 1
D: 2
E: 3
range(x) = 0, 1, 2, 3
result = result + str(:i) means i is not included
in order to include i , str(:i+1)