本题来自 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:姓名分值的更多相关文章

  1. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

  4. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  5. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  6. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  7. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  8. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  9. Python练习题 044:Project Euler 016:乘方结果各个数值之和

    本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...

随机推荐

  1. Vuex 注入 Vue 生命周期的过程

    首先我们结合 Vue 和 Vuex 的部分源码,来说明 Vuex 注入 Vue 生命周期的过程. 说到源码,其实没有想象的那么难.也和我们平时写业务代码差不多,都是方法的调用.但是源码的调用树会复杂很 ...

  2. 【Android】AndroidStudio(Eclipse)如何使用天天模拟器进行调试apk应用。

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 大家都知道,我们这些Android开 ...

  3. GENYMOTION问题之an error occurred while deploying a file install_failed_no_machine_abis

    GENYMOTION问题之an error occurred while deploying a file install_failed_no_machine_abis 出现上面错误,看网上有一种解决 ...

  4. 小程序开发-block组件的使用

    微信小程序中,block不是一个组件,而是一个包装元素,不会在页面中做任何渲染. 使用情况:条件渲染 wx:if 因为 wx:if 是一个控制属性,需要将它添加到一个标签/组件上,用于控制隐藏与显示. ...

  5. 干货:不同场景容器内获取客户端源IP的方法

    摘要:客户端和容器服务器之间可能存在多种不同形式的代理服务器,那容器中如何获取到客户端真实的源ip呢? k8s已经成为当今容器化的标准,人们在享受容器带来的高效与便利的同时,也遇到一些烦恼:客户端和容 ...

  6. Repeater每行绑定事件代码

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repea ...

  7. IAR错误Error[e16]: Segment ISTACK (size: 0xc0 align: 0) is too long for segment definition. At least 0xd more bytes needed. The problem occurred while processing the segment 的解决办法

    IAR for 8051 IDE version 9.10.1编译阿莫单片机的cc2541程序osal第一节教程出现错误如下 出错的愿意是iar设置错误.所以需修改IAR的设置. 解决办法如下: 即可 ...

  8. 乔悟空-CTF-i春秋-Web-SQL

    2020.09.05 是不是有些题已经不能做了--费了半天,到最后发现做不出来,和网上大神的方法一样也不行,最搞笑的有个站好像是别人运营中的,bug好像被修复了-- 做题 题目 题目地址 做题 尝试简 ...

  9. JVM运行时数据区--本地方法栈

    本地方法栈 1.Java虚拟机栈用于管理Java方法的调用,而本地方法栈用于管理本地方法(一般非Java实现的方法)的调用 2.本地方法栈,也是线程私有的. 3.允许被实现成固定或者是可动态拓展的内存 ...

  10. Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署静态网站(6)

    前言 在之前已经写了关于Git,Gitlab以及Ansible的两篇博客<Git+Gitlab+Ansible剧本实现一键部署Nginx–技术流ken>,<Git+Gitlab+An ...