Problem 42
Problem 42
https://projecteuler.net/problem=42
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
三角数可由上述公式得出。
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
讲单词中的字母转换为字母表中的位置(如:A=1),将它们相加。如果得到的数字是一个三教数,则这个单词为三角单词。
Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
找找这两千多个英语常用词中有多少个三角单词?
filename = 'p042_words.txt'
with open(filename) as f:
data = f.read() words = data.split(',')
for i in range(len(words)):
words[i] = words[i][1:-1] triangle_nums = [i * (i + 1) // 2 for i in range(1, 100)]
triangle_words = []
alphabet = [chr(i) for i in range(65, 65 + 26)]
word_value = 0
count = 0
for word in words:
for letter in word:
word_value += alphabet.index(letter) + 1
if word_value in triangle_nums:
triangle_words.append(word)
count += 1
word_value = 0
else:
word_value = 0 print(triangle_words)
print(count)
Problem 42的更多相关文章
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- EularProject 42:单词解码出来的三角形数
Coded triangle numbers Problem 42 The nth term of the sequence of triangle numbers is given by, tn = ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- UVa 106 - Fermat vs Pythagoras(数论题目)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- 《DSP using MATLAB》Problem 8.42
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Problem F: 零起点学算法42——多组测试数据输出II
#include<stdio.h> int main() { ; while(scanf("%d%d%d",&a,&b,&c)!=EOF) { ...
随机推荐
- 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc【转】
本文转载自:http://www.arm9home.net/read.php?tid-80810.html 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc ...
- git的基本操作流程
1.git clone 初始会有默认的master分支,并且master和origin/master自动建立了映射关系 2. git checkout -b local 创建并且切换到local ...
- YTU 2715: 函数---判断某年某月某日是这一年中的第几天
2715: 函数---判断某年某月某日是这一年中的第几天 时间限制: 1 Sec 内存限制: 128 MB 提交: 380 解决: 155 题目描述 在主程序(main)中输入某年某月某日,例如2 ...
- git 命令 —— checkout
git checkout 会重写工作区.check in 常常表示酒店入住,则 check out 就表示结账(检查)离开. 1. 基本用法 Git学习笔记04–git checkout git ch ...
- 洛谷P1341 无序字母对(欧拉回路)
P1341 无序字母对 题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 ...
- js 中的定时器
在js中的定时器分两种:1.setTimeout() 2.setInterval() 1.setTimeOut() 只在指定时间后执行一次 /定时器 异步运行 function hello(){ al ...
- Java系列学习(零)-写在前面的话
1.为什么写这套笔记 理由一:因为需求,所以学习,然后就要记笔记 理由二:同时学几种相似的语言,怕搞混,所以写 2.笔记修改日志
- P1968 美元汇率
题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 ...
- CSS——层级
层级问题:选中的盒子显示的效果并不完整,右边的边框并没有显示红色,原因是其右边的盒子压了它的边框. <!DOCTYPE html> <html lang="en" ...
- Json——一般应用
引用命名空间 using Newtonsoft.Json; 序列化类或者类的集合 string jsonData1 = JsonConvert.SerializeObject(p1);//序列化类 s ...