0

I'm trying to create a programm in which a user inputs a string e.g 'roller' and the program converts the alphabet to numbers such as a=1, b=2, c=3 etc, and the calculate the sum of these values. But, if the program finds two same letters in a row then it doubles the sum. So far I have done this:

input = raw_input('Write Text: ')
input = input.lower()
output = []
sum=0
for character in input:
    number = ord(character) - 96
    sum=sum+number
    output.append(number)
print sum

which calculates the sum of the characters and also appends the converted characters to a new array. So can anyone help me to double the sum if two letters appear in a row?

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
thr
  • 35
  • 1
  • 9

1 Answers1

0

Store the previous character and compare it to the current character. If they're the same, double the value.

word = 'hello'
out = []
c_prev = None

for c in word:
    value = ord(c) - ord('a')

    if c == c_prev:  # double if repeated character
        value = value * 2

    out.append(value)
    c_prev = c  # store for next comparison

print(sum(out))
davidism
  • 121,510
  • 29
  • 395
  • 339