Reputation: 127
inp = input("Enter the expression ")
num = str()
for i in range(len(inp)):
if(inp[i] == "."):
print("0",end="")
elif(inp[i] == "-"):
if(inp[i+1] == "."):
print("1",end="")
elif(inp[i+1] == "-"):
print("2",end="")
i += 1
print(num)
I am attempting to decode the Borze code question from codeforces, here I have started with a for loop that loops through the range of provided . and - in the string inp. In the given part below,
elif(inp[i] == "-"):
if(inp[i+1] == "."):
print("1",end="")
elif(inp[i+1] == "-"):
print("2",end="")
i += 1
I have increased the value of i by one by adding i+=1 as in some cases two values of the string will be evaluated and to avoid repetition of the second value in the next iteration the value of i, in that case, has to be increased. However, the loops still seem to iterate the length of times as the length of the string provided. Is it a logic error? What am I missing?
Upvotes: 0
Views: 37
Reputation: 39354
You may have to manually construct your loop index to get what you want:
i = 0
while i < len(inp):
if(inp[i] == "."):
print("0",end="")
elif(inp[i] == "-"):
if(inp[i+1] == "."):
print("1",end="")
elif(inp[i+1] == "-"):
print("2",end="")
i += 1 # increment under certain conditions
i += 1 # increment every time round
Upvotes: 2