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* ...
随机推荐
- session 机制和 httpsession 详解 (转载)
https://www.cnblogs.com/bjanzhuo/archive/2013/02/27/3575884.html 一.术语session 在我的经验里,session这个词被滥用的程度 ...
- 使用python制作趣味小游戏—投骰子
1.模拟真实环境掷骰子 从Python标准库中调用模块:random——random中包含以各种方式生成随机数的函数 从random中引用randint这一函数——骰子都是有固定面数 from ran ...
- [PyTorch 学习笔记] 4.2 损失函数
本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson4/loss_function_1.py https:// ...
- Android开发之 当前日期String类型转date类型 java代码中实现方法
/** * 获取当前时间 * * @return */ public Date getDate(String str) { try { java.text.SimpleDateFormat forma ...
- Fitness - 05.04
倒计时241天 运动38分钟,共计9组.拉伸10分钟. 每组跑步2分钟(6.3KM/h),走路2分钟(6KM/h). 上午下了课,直奔健身房. 手机坏了,没有听音乐. 没有吃午饭,但是上午喝的咖啡还是 ...
- 【HttpRunner v3.x】笔记 ——4. 测试用例-结构解析
一.官方首推pytest格式 上篇文章我们知道了,httprunner可以支持三种格式的用例,分别是pytest.yaml和json.yaml和json是以前的版本所使用的用例格式,但是在3.x版本上 ...
- 利用阿里云服务器免费体验word press博客、个人网站
本文首发于我的个人博客:https://chens.life/create-wordpress-blog.html 前言 目前市面上有许许多多的虚拟云服务器ECS,例如阿里云.华为云.又拍云等等,他们 ...
- TinkPHP5.1开发注意事项
1.新下载的框架文件,开发前先开启调试配置 config目录下app.php文件 // 应用调试模式 'app_debug' => true, 2.每新建一个方法,都要 ...
- Python 利用三个简易模块熟悉前后端交互流程
准备工作 在学习Django之前,先动手撸一个简单的WEB框架来熟悉一下前后端交互的整体流程 本次用到的模块: 1.wsgiref,这是一个Python自带的模块,用于构建路由与视图 2.pymysq ...
- 04async await
async async 函数返回值是一个promise对象,promise对象的状态由async函数的返回值决定 //函数的三种定义 async function hello() { return ...