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* ...
随机推荐
- 微信小程序自动化
解析微信小程序 注意:若上面方法不行就使用下面的 小程序对应的chrome驱动版本包,2.4版本的
- node-sass安装失败解决方法
node-sass安装失败,提示如下: gyp verb check python checking for Python executable "python" in the P ...
- RabbitMQ配置文件(rabbitmq.conf)
rabbitmq.conf配置文件示例: #====================================== #RabbitMQ经纪人部分 #======================= ...
- 在C++/CLI环境下,千万不要把普通全局函数当标准C/C++的函数指针传递给native的库使用
先上一个简单代码: #include <cstdlib> #include <cstdio> // native apis extern "C" { typ ...
- Oracle RAC与DG
RAC RAC: real application clustersrac RAC: real application clustersrac 单节点数据库:数据文件和示例文件一一对应 实例损坏时数据 ...
- Java中的IO操作和缓冲区
目录 Java中的IO操作和缓冲区 一.简述 二.IO流的介绍 什么是流 输入输出流的作用范围 三.Java中的字节流和字符流 字节流 字符流 二者的联系 1.InputStreamReader 2. ...
- hdu6075 2019CCPC网络选拔赛1004 path
题意:给定一个带权有向图,有q组询问,每次询问在有向图的所有路径中,第k小的路径权值 解题思路:因为k最大只有5e4,考虑暴力搜索出前maxk小的路径并用数组记录权值,然后就可以O(1)查询. 具体实 ...
- Beware of the encrypted VM
A friend of mine Megan told me that she got an error message as below screenshot when trying to open ...
- [Java数据结构]使用Stack检查表达式中左右括号是否匹配
Stack是一种先进后出的数据结构后,这个特点决定了它在递归向下的场景中有独到的功效. 以下程序展示了它在检查表达式中括号匹配的有效性: 程序: package com.heyang.util; im ...
- linux下查找文件中的某个关键字
1.方法一:grep '关键字' filename 2.方法二:vim filename进入文件里面,不要进入insert编辑模式,直接在normal模式下输入/关键字进行搜索 按n进行查找下一个