Problem 22

Using names.txt (https://projecteuler.net/problem=22)(right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

整理文本文件,排序,将每个名字中每个字符在字母表中的位置总和乘以这个名字在排序后的列表中的位置,最后得到的所有名字总和为?

names = []
with open('p022_names.txt') as f:
for line in f:
for i in line.split(','):
names.append(i[1:-1])
names.sort()
alphabet = [chr(i) for i in range(65, 65+26)]
total = 0
for name in names:
count = 0
for char in name:
count += alphabet.index(char) + 1
index = names.index(name) + 1
total += (count * index)
print(total)

最新文章

  1. win7搭建双系统ubuntu
  2. (Python)异常处理try...except、raise
  3. NGUIJoysticK
  4. linux下六大IPC机制【转】
  5. 编译android源码官方教程(5)编译完之后刷机、编译fastboot
  6. Data Flow ->> Import Column & Export Column
  7. Shell脚本[运算表达式,条件控制语句]
  8. C# 线程--第三线程池
  9. Filezilla FTP Server 设置帐号主目录文件夹的方法和多个帐号共享一个文件夹的方法
  10. thinkphp多表查询
  11. C#面向对象基础类与方法
  12. tyvj/joyoi 2018 小猫爬山
  13. 关于MySQL checkpoint
  14. TFS 报错解决方案:tf400324
  15. android状态栏和NavigationBar的动态控制显示
  16. 一本通1625【例 1】反素数 Antiprime
  17. TypeScript 小记
  18. 如何进行 Python性能分析,你才能如鱼得水?
  19. centos7源码安装vim8.0
  20. [置顶] CSS语言精粹

热门文章

  1. 怎么修改tomcat端口
  2. Fragment进阶(四)----->參数传递3种写法
  3. "Hello World " —— 深入理解程序从编译到执行
  4. Instagram的Material Design概念设计文章分享
  5. phoenixframe自己主动化測试平台对div弹出框(如弹出的div登陆框)的处理
  6. ⭐linux主次设备号介绍
  7. Google android source code build 问题总结【转】
  8. android NDK 神经网络API——是给tensorflow lite调用的底层API,应用开发者使用tensorflow lite即可
  9. scrollTop,scrollHeight,clientTop,clientHeight,offsetTop,offsetHeight实际意义 及 计算方式 附实例说明
  10. 机器学习——Day 1 数据预处理