Project Euler - Problem 22


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. # Project Euler - Problem 22
  4.  
  5. f = open("names.txt",'r')
  6.  
  7. names = []
  8.  
  9. names = sorted(f.read().replace('"','').split(','),key=str)
  10. i = 1
  11. sum = 0
  12. ans = 0
  13. for string in names:
  14. chars = list(string)
  15. for x in chars:
  16. if x == 'A':
  17. t = 1
  18. elif x == 'B':
  19. t = 2
  20. elif x == 'C':
  21. t = 3
  22. elif x == 'D':
  23. t = 4
  24. elif x == 'E':
  25. t = 5
  26. elif x == 'F':
  27. t = 6
  28. elif x == 'G':
  29. t = 7
  30. elif x == 'H':
  31. t = 8
  32. elif x == 'I':
  33. t = 9
  34. elif x == 'J':
  35. t = 10
  36. elif x == 'K':
  37. t = 11
  38. elif x == 'L':
  39. t = 12
  40. elif x == 'M':
  41. t = 13
  42. elif x == 'N':
  43. t = 14
  44. elif x == 'O':
  45. t = 15
  46. elif x == 'P':
  47. t = 16
  48. elif x == 'Q':
  49. t = 17
  50. elif x == 'R':
  51. t = 18
  52. elif x == 'S':
  53. t = 19
  54. elif x == 'T':
  55. t = 20
  56. elif x == 'U':
  57. t = 21
  58. elif x == 'V':
  59. t = 22
  60. elif x == 'W':
  61. t = 23
  62. elif x == 'X':
  63. t = 24
  64. elif x == 'Y':
  65. t = 25
  66. else:
  67. t = 26
  68. sum += t
  69. ans += sum * i
  70. i += 1
  71. sum = 0
  72.  
  73. print ans

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.