1

i need sum in string letters value ex. a = 1 b = 2 c = 3 d = 4

alphabet = 'abcdefghijklmnopqrstuvwxyz'

v1

string = "abcd"

# #result = sum(string) so 
if string[0] and string[1] and string[2] and string[3] in alphabet:
   if string[0] is alphabet[0] and string[1] is alphabet[1] and string[2] is alphabet[2] and string[3] is alphabet[3]:
      print(a+b+c+d)

v2

string = ("ab","aa","dc",)

if string[0][0] and string[0][1] and string[1][0] and string[1][1] and string[2][0] and string[2][1] in alphabet:
   if string[0] is alphabet[0] and string[1] is alphabet[1] and string[2] is alphabet[2] and string[3] is alphabet[3]:
      print(a+b+c+d)   

what is the solution? can you help me

Droid
  • 1,410
  • 8
  • 23
  • 37
  • 2
    Your boolean logic is entirely off-base, see [How do I test one variable against multiple values?](https://stackoverflow.com/q/15112125) – Martijn Pieters Nov 30 '15 at 10:32

2 Answers2

2

Use the sum() function and a generator expression; a dictionary built from string.ascii_lowercase can serve as a means to getting an integer value per letter:

from string import ascii_lowercase

letter_value = {c: i for i, c in enumerate(ascii_lowercase, 1)}
wordsum = sum(letter_value.get(c, 0) for c in word if c)

The enumerate(ascii_lowercase, 1) produces (index, letter) pairs when iterated over, starting at 1. That gives you (1, 'a'), (2, 'b'), etc. That can be converted to c: i letter pairs in a dictionary, mapping letter to integer number.

Next, using the dict.get() method lets you pick a default value; for any character in the input string, you get to look up the numeric value and map it to an integer, but if the character is not a lowercase letter, 0 is returned instead. The sum(...) part with the loop then simply adds those values up.

If you need to support sequences with words, just use sum() again. Put the above sum() call in a function, and apply that function to each word in a sequence:

from string import ascii_lowercase

letter_value = {c: i for i, c in enumerate(ascii_lowercase, 1)}

def sum_word(word):
    return sum(letter_value.get(c, 0) for c in word if c)

def sum_words(words):
    return sum(sum_word(word) for word in words)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I'd use `{chr(i+96):i for i in range(1, 27)}` instead of importing `ascii_lowercase` – Steven Summers Nov 30 '15 at 10:47
  • @StevenSummers: that's a matter of taste; I think it is self-documenting this way. – Martijn Pieters Nov 30 '15 at 11:07
  • @MartijnPieters you are my hero – Droid Nov 30 '15 at 11:43
  • @MartijnPieters how can i do sum string combine products, i tried sum(letter_value.get(c,0) * letter_value.values() for c in zip(m) if c) but i get error unsupported operand type(s) for *: 'int' and 'dict_values' – Droid Nov 30 '15 at 12:40
  • @PyDroid: what did you expect to multiply with? – Martijn Pieters Nov 30 '15 at 13:56
  • @MartijnPieters i need to compare string sum and produc, if sum_word != product_word continue until the condition is not checked, can you help me, how to get product_word? – Droid Nov 30 '15 at 17:38
  • @PyDroid: not without more information what `product_word` is supposed to be. Are you talking about multiplying all the scores together? So `abc` would be `1 * 2 * 3`, producing `6`? – Martijn Pieters Nov 30 '15 at 17:42
  • @MartijnPieters yes i need it, if a+b+c+d != a * b * c * d than continued condition while the results will not be – Droid Nov 30 '15 at 17:47
  • 1
    @PyDroid: use `import operator`, then `reduce(operator.mul, (letter_value.get(c, 0) for c in word if c), 1)` to produce a word product. – Martijn Pieters Nov 30 '15 at 17:53
2

The old-fashioned way is to take advantage of the fact that lowercase letters are contiguous, so that ord(b) - ord(a) == 1:

data = "abcd"
print("Sum:", sum(ord(c)-ord("a")+1 for c in data))

Of course you could "optimize" it to reduce the number of computations, though it seems silly in this case:

ord_a = ord("a")
print("Sum:", sum(ord(c)-ord_a for c in data)+len(data))
alexis
  • 48,685
  • 16
  • 101
  • 161
  • thanks alex, can you help me, how to get produc string? ex. if i have sum a+b+c+d than i need comapre sum and product if a+b+c+d != a * b * c * d – Droid Nov 30 '15 at 17:41
  • I googled "python product" and [boom!](http://stackoverflow.com/a/7948307/699305). Second hit in the google results shows you how to do it. (Define it as shown, then use `prod()` instead of `sum()`.) – alexis Nov 30 '15 at 19:25