Created
March 19, 2015 11:46
-
-
Save p53ud0k0d3/9d00c2eed4e081a00e95 to your computer and use it in GitHub Desktop.
HackerRank The Love-Letter Mystery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
words = [] | |
def evenPal(word): | |
steps = 0 | |
l = len(word) | |
w1 = word[:l/2] | |
w2 = word[l/2:] | |
w2 = w2[::-1] | |
for i in xrange(0, len(w1)): | |
steps += abs(ord(w1[i]) - ord(w2[i])) | |
print steps | |
def oddPal(word): #Different approach | |
steps = 0 | |
l = len(word) | |
mid = l/2 | |
j = -1 | |
for i in xrange(0, mid): | |
steps += abs(ord(word[i]) - ord(word[j])) | |
j -= 1 | |
print steps | |
for _ in xrange(int(raw_input())): | |
words.append(raw_input().strip()) | |
for word in words: | |
if word == word[::-1]: | |
print 0 | |
elif len(word) % 2 == 0: | |
evenPal(word) | |
else : | |
oddPal(word) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment