Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22
'''
Project Euler: Problem 22: Names scores
Using names.txt (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?
Answer: 871198282
''' lst = [] #姓名列表
for i in open("Files/p022_names.txt").readline().split(','):
lst.append(i.strip('"'))
lst.sort() nameScore = 0
for i in range(len(lst)):
wordSum = 0
for j in range(len(lst[i])):
wordSum += (ord(lst[i][j])-64)
nameScore += (lst.index(lst[i])+1) * wordSum
print(nameScore)
本题中,有个txt文件,内有5000个英文姓名,要求首先对这些姓名进行排序,然后分别计算各个姓名每个字母的序号之和a、以及该姓名在txt文件中的序号b,将a和b相乘,作为该姓名的分值。最后,将这5000个分值相加,得出结果。
本题涉及的知识点:
- data = open("xxxxx.txt"):可打开txt文件
- data.readline():可读取txt文件其中一行。本题只有一行,所以不需要逐行读取
- str.split(','):以逗号为分隔符,将字符串分割成若干单元
- str.strip('"'):将字符串前后的特定字符(此处为双引号)去掉
- ord('A'):返回单个字符A对应的ASCII码
- lst.index('COLIN'):返回COLIN在lst列表中的位置
思路则很简单:读取txt文件,将每个英文姓名剥离出来,放入lst列表。之后逐一将姓名中的每个单词转化为序号,相加得出wordSum,因为字母A对应的序号是1,ASCII码是65,所以用ord()转换为ASCII码之后应该减去64。接着获取姓名在lst列表中的位置,因为lst.index()获得的位置从0开始,而本题单词的序号从1开始,所以需要获取位置值之后再加1。把wordSum和该单词的序号相乘,可得该单词的nameScore。5000个单词遍历一下就可以了。
Python练习题 049:Project Euler 022:姓名分值的更多相关文章
- Python练习题 032:Project Euler 004:最大的回文积
本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 033:Project Euler 005:最小公倍数
本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
- Python练习题 044:Project Euler 016:乘方结果各个数值之和
本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...
随机推荐
- TX-LCN 分布式事务框架
第十章 TX-LCN 分布式事务框架 (Spring Cloud 高级) 一. 什么是分布式事务 分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位 于不同的分布式系统的不同 ...
- seo成功案例的背后秘密
http://www.wocaoseo.com/thread-319-1-1.html 刚刚在seo群内一个企业主告诉我,他在淘宝找了做seo排名的,在交了首付后,对方却跑路了.对方刚刚在淘宝开店,然 ...
- 火狐Firefox 52.90版是最后一个支持WinXP和Vista的版本
Firefox 52.90版是最后一个支持 Windows XP 和 Windows Vista 的升级版.(参考:https://support.mozilla.org/zh-CN/kb/firef ...
- el-dialog“闪动”解决办法
问题描述:el-dialog关闭的时候总是出现两次弹窗 解决思路:既然是el-dialog产生的那就直接杀掉el-dialog 代码实践:在el-dialog上添加上一个v-if,值就是用闭窗的值,促 ...
- 【免费】windows下如何生成tar.gz,一键生成tar.gz
废话 一.实验背景 tar.gz 是Linux和Unix下面比较常用的格式,一条命令就可以把文件压缩打包成tar.gz格式,然而这种格式在windows并不多见. Linxu服务器上,tar.gz 包 ...
- Fragment的跳转
1. 设置主Fragment 其它fragment得到它就可以了. 1 val ft = fragmentManager?.beginTransaction() 2 val maiFrgmt = Ma ...
- Java网络通信 —— 序列化问题
Java序列化的目的主要有两个: 1.网络传输 2.对象持久化 当选行远程跨迸程服务调用时,需要把被传输的Java对象编码为字节数组或者ByteBuffer对象.而当远程服务读取到ByteBuffer ...
- Spring JPA 查询创建
Spring JPA 查询创建 这是JPA内容的核心部分,可以收藏用作参阅文档. 1. 查询转化和关键字 例:一个JPA查询的转化 public interface UserRepository ex ...
- Sympy解方程-求极限-微分-积分-矩阵运算
简介 Sympy是一个Python的科学计算库,用一套强大的符号计算体系完成诸如多项式求值.求极限.解方程.求积分.微分方程.级数展开.矩阵运算等等计算问题.虽然Matlab的类似科学计算能力也很强大 ...
- 认识一下python
python 目录 python 1.python创始人 2.python的设计目标 3.为什么使用python 4.python的特点 5.python的优缺点 1.python创始人 1.1989 ...