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) { ...
随机推荐
- Expressions-->Member lookup
7.4 Member lookup 在word文档的第140页 A member lookup is the process whereby凭借:通过…:借以:与…一致 the meaning of ...
- 【关键字】volatile
volatile 修饰的关键字,确保编译器不对成员变量进行任何优化: private volatile double d; // No optimization
- openstack 虚拟机导出
- ubuntu 16.04 Hbase 安装
1.解压安装包至路径 /usr/local 1.1.sudo tar -zxf ~/下载/hbase-1.1.2-bin.tar.gz -C /usr/local 2.将解压的文件名hbase-1.1 ...
- PCB genesis方槽加内角槽孔实现方法
一.为什么方槽孔加内角孔 如下图,客户来的方槽或Slot槽有内角尺寸要求,通常直接钻一个Slot槽孔内角是不能满足客户要求的,这时我们做CAM的需采用小钻刀进行处理.加内角孔或内角槽的方式进行处理了. ...
- CSS样式适配杂记
1.问:input框的对齐,制作类似百度搜索框的时候,发现IE下前面输入框和后面按钮总是不能对齐. 解答:给input框增加vertical-align:bottom; 2.问:IE下display: ...
- 51nod 1222 莫比乌斯反演
思路: yhx找的反演题 题解已经烂大街了 #pragma GCC optimize("O3") //By SiriusRen #include <bits/stdc++.h ...
- 334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- Scala-基础-数据类型
import junit.framework.TestCase import org.junit.Test import scala.runtime.RichByte //数据类型 class Dem ...
- Java中 == 和 equals()
记住三句话 1. 语义上:==指的是内存引用一样.equals是指的是逻辑相等.逻辑相等具体的意思由编写者决定(即在引用类型中,"=="是比较两个引用是否指向堆内存里的同一个地址( ...