Python - variable not updating in loop -
i trying solve 'love-letter' mystery problem of hackerrank using python, stuck @ place in loop variable not getting updated.
s = input() first_char = s[0] last_char = s[-1] ascii_first_char = ord(first_char) ascii_last_char = ord(last_char) count = 0 = 1 while ascii_first_char < ascii_last_char: count += abs((ascii_last_char-ascii_first_char)) ascii_first_char = ord(s[i]) ascii_last_char = ord(s[-i]) += 1 print(count)
if seek run that, see alc not changing it's value according ord(s[i])
keeps incrementing. why happening?
you first letter s[0] , lastly s[-1]. in loop take next letters same index i.
i don't understand status in while loop. instead of "ascii_first_char < ascii_last_char" should test if have looked @ every element of string. have loop len(s)/2 times. like:
while < len(s) - i:
or equivalent
while 2*i < len(s):
and conditions work length. prefer for-loops when know how many times loop
current_line = input() # if length even, don't care letter in middle # abcde <-- need first , lastly 2 values # 5 // 2 == 2 half_length = len(current_line) // 2 changes = 0 in range(index): changes += abs( ord(current_line[i]) - ord(current_line[-(i+1)]) ) print (changes)
python python-3.x
No comments:
Post a Comment