We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I am trying to solve the problem using python.When I submit the code it passes only the first 10 test cases, and for the other 30 it says that Terminated due to timeout. It runs perfectly on my editor. What am I doing wrong here ? Thank you.
Hamming Distance
from timeit import default_timer as timer
def R(string,i,j):
prev = []
next = []
rev = []
for x in range(i-1):
prev.append(string[x])
for x in range(j,len(string)):
next.append(string[x])
for x in range(i-1,j):
rev.append(string[x])
rev.reverse()
reversed_string = "".join(str(x) for x in prev) + "".join(str(x) for x in rev) + "".join(str(x) for x in next)
return reversed_string
def W(string,i,j):
for i in range(i-1,j):
print(string[i],end="")
print()
def S(string,i1,j1,i2,j2):
prev = []
middle = []
next = []
string1 = []
string2 = []
for x in range(i1-1):
prev.append(string[x])
for x in range(i1-1,j1):
string1.append(string[x])
for x in range(j1,i2-1):
middle.append(string[x])
for x in range(i2-1,j2):
string2.append(string[x])
for x in range(j2,len(string)):
next.append(string[x])
swapped_string = "".join(str(x) for x in prev) + "".join(str(x) for x in string2) + "".join(str(x) for x in middle) + "".join(str(x) for x in string1) + "".join(str(x) for x in next)
return swapped_string
def C(string,i,j,ch):
prev = []
next = []
equal = []
for x in range(i-1):
prev.append(string[x])
for x in range(j,len(string)):
next.append(string[x])
for x in range(i-1,j):
equal.append(ch)
changed_string = "".join(str(x) for x in prev) + "".join(str(x) for x in equal) + "".join(str(x) for x in next)
return changed_string
def H(string,i,j,length):
distance = 0
first = string[i-1:(i-1)+length]
second = string[j-1:(j-1)+length]
for i in range(len(first)):
if first[i] != second[i]:
distance += 1
return distance
string_size = int(input())
string = input()
command_size = int(input())
commands = []
for i in range(command_size):
command = input()
command_list = command.split(" ")
commands.append(command_list)
if string_size == len(string):
if command_size == len(commands):
for i in commands:
if i[0] == "R":
string = R(string,int(i[1]),int(i[2]))
if i[0] == "W":
W(string,int(i[1]),int(i[2]))
if i[0] == "S":
string = S(string,int(i[1]),int(i[2]),int(i[3]),int(i[4]))
if i[0] == "C":
string = C(string,int(i[1]),int(i[2]),i[3])
if i[0] == "H":
distance = H(string,int(i[1]),int(i[2]),int(i[3]))
print(distance)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hamming Distance
You are viewing a single comment's thread. Return to all comments →
I am trying to solve the problem using python.When I submit the code it passes only the first 10 test cases, and for the other 30 it says that Terminated due to timeout. It runs perfectly on my editor. What am I doing wrong here ? Thank you.
Hamming Distance
from timeit import default_timer as timer
def R(string,i,j): prev = [] next = [] rev = [] for x in range(i-1): prev.append(string[x]) for x in range(j,len(string)): next.append(string[x]) for x in range(i-1,j): rev.append(string[x]) rev.reverse() reversed_string = "".join(str(x) for x in prev) + "".join(str(x) for x in rev) + "".join(str(x) for x in next) return reversed_string
def W(string,i,j): for i in range(i-1,j): print(string[i],end="") print()
def S(string,i1,j1,i2,j2): prev = [] middle = [] next = [] string1 = [] string2 = [] for x in range(i1-1): prev.append(string[x]) for x in range(i1-1,j1): string1.append(string[x]) for x in range(j1,i2-1): middle.append(string[x]) for x in range(i2-1,j2): string2.append(string[x]) for x in range(j2,len(string)): next.append(string[x])
def C(string,i,j,ch): prev = [] next = [] equal = [] for x in range(i-1): prev.append(string[x]) for x in range(j,len(string)): next.append(string[x]) for x in range(i-1,j): equal.append(ch) changed_string = "".join(str(x) for x in prev) + "".join(str(x) for x in equal) + "".join(str(x) for x in next) return changed_string
def H(string,i,j,length): distance = 0 first = string[i-1:(i-1)+length] second = string[j-1:(j-1)+length] for i in range(len(first)): if first[i] != second[i]: distance += 1 return distance
string_size = int(input()) string = input() command_size = int(input()) commands = [] for i in range(command_size): command = input() command_list = command.split(" ") commands.append(command_list)
if string_size == len(string): if command_size == len(commands): for i in commands: if i[0] == "R": string = R(string,int(i[1]),int(i[2])) if i[0] == "W": W(string,int(i[1]),int(i[2])) if i[0] == "S": string = S(string,int(i[1]),int(i[2]),int(i[3]),int(i[4])) if i[0] == "C": string = C(string,int(i[1]),int(i[2]),i[3]) if i[0] == "H": distance = H(string,int(i[1]),int(i[2]),int(i[3])) print(distance)