"Funny String" problem from Hackerrank
Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$\$\left|S[i] - S[i-1]\right| = \left|R[i] - R[i-1]\right|\$ is true for all \$i \in [1, N-1]\$.
I code mainly in Java. I'm very new to pythonPython, so I would appreciate feedback on how I can make this code more pythonicPythonic, cleaner and more efficient in general. Thanks!
Solution
import math
def isFunny(word):
i = 0
length = len(word)
arr = []
revarr = []
for i in range(0,math.ceil(length/2)):
sdiff = abs(ord(word[i])-ord(word[i+1]))
rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
if sdiff == rdiff:
continue
else:
return False
return True
if __name__ == "__main__":
n = int(input())
for _ in range(n):
word = input();
if isFunny(word):
print("Funny")
else:
print("Not Funny")
Any feedback would be appreciated!