Advertisement
makispaiktis

Love vs Friendship (Codewars)

Oct 31st, 2019 (edited)
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. '''
  2.  
  3. If a = 1, b = 2, c = 3 ... z = 26
  4.  
  5. Then l + o + v + e = 54
  6.  
  7. and f + r + i + e + n + d + s + h + i + p = 108
  8.  
  9. So friendship is twice stronger than love :-)
  10.  
  11. The input will always be in lowercase and never be empty.
  12.  
  13.  
  14.  
  15. '''
  16.  
  17. alphabet = "abcdefghijklmnopqrstuvwxyz"
  18.  
  19. def lexarithm(word):
  20.  
  21.     # Create the map/directory
  22.     directory = {}
  23.     counter = 0
  24.     for i in range(0, len(alphabet)):
  25.         counter += 1
  26.         directory[alphabet[i]] = counter
  27.  
  28.     # print(directory)
  29.     # Directory is ready, time for scanning the parameter "word"
  30.     weights = []
  31.     for i in range(0, len(word)):
  32.         # For every character of the word, "word[i]"
  33.         for key, value in directory.items():
  34.             if word[i] == key:
  35.                 weights.append(value)
  36.                 break
  37.  
  38.     # The list "weights" contains the equivalent numbers matching with the letters of word
  39.     sum = 0
  40.     for i in range(0, len(weights)):
  41.         sum += weights[i]
  42.  
  43.     return sum
  44.  
  45. # MAIN FUNCTION
  46. print(lexarithm("love"))
  47. print(lexarithm("friendship"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement