"Buch, um die Programmierfähigkeit zu trainieren, um in der Welt zu kämpfen" Python-Code-Antwortbeispiel --1.3 URLify
def replaceSpaces(str,trueLength):
spaceCount = 0
for i in range(trueLength):
if str[i] == " ":
spaceCount = spaceCount + 1
index = trueLength + spaceCount * 2
target = [0] * index
for i in range(trueLength-1,-1,-1):
if str[i] == " ":
target[index-1] = "0"
target[index-2] = "2"
target[index-3] = "%"
index = index - 3
else:
target[index-1] = str[i]
index = index - 1
return "".join(target)
input_str = "Mr John Smith"
input_num = len(input_str)
print(replaceSpaces(input_str,input_num))
Recommended Posts