https://www.hackerrank.com/challenges/python-mutations/problem
This is the question.
The answer is
def mutate_string(string, position, character):
return string[:position] + character + string[position + 1:]
This is.
I don't understand why there is a position in the string [position + 1:] here. If I don't put this in, it's printed out like abackbracadabra, but if I put in position, why can't I print out the braca? Is that right to slice from the part where the position is located at 0 and then 1:?
return character.join([string[:position], string[position + 1:]])
And another answer is that in the case of something.join(a, b), there is an interpretation that something corresponds to something between a and b, but did I read it correctly?
algorithm python
The mute_string()
function does this.
Therefore, character
will be entered only for position
, and the rest will be the original contents of string
.
A solution using character.join()
does this.
I don't know if it was answered.
© 2024 OneMinuteCode. All rights reserved.