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的更多相关文章

  1. (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 ...

  2. leetcode problem 42 -- Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. 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 ...

  4. EularProject 42:单词解码出来的三角形数

    Coded triangle numbers Problem 42 The nth term of the sequence of triangle numbers is given by, tn = ...

  5. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  6. UVa 106 - Fermat vs Pythagoras(数论题目)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  7. 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 ...

  8. 《DSP using MATLAB》Problem 8.42

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. Problem F: 零起点学算法42——多组测试数据输出II

    #include<stdio.h> int main() { ; while(scanf("%d%d%d",&a,&b,&c)!=EOF) { ...

随机推荐

  1. CoffeeScript的缩进

    CoffeeScript用缩进代替了花括符"{}",作用范围的划分只靠缩进.这带来代码精简.简洁的同时,有时候也让人困惑. 比如说: if 3 > 1 alert(" ...

  2. ES6详解八:模块(Module)!--各种导入导出方法

    [-] 基本用法 命名导出named exports 默认导出 命名导出结合默认导出 仅支持静态导入导出 各种导入和导出方式总结   modules是ES6引入的最重要一个特性. 所以以后再写模块,直 ...

  3. putty+Xmanager登陆Linux,实现图形界面操作.

  4. 2-1 创建第一个Vue实例

    https://cn.vuejs.org/ 生产版本是经过压缩后的版本,它里面把一些没用的警告的代码删除掉了所以它会更精悍一些. 直接引入CDN的网址我们就可以使用Vuejs了.使用生产版本或者CDN ...

  5. python - list 列表推导式

    一.如有两个list,分别为: a = [1,2,3,4,5,6]b = ["a","b","c","d"," ...

  6. 【转】Java中特殊的String类型

    Java中String是一个特殊的包装类数据有两种创建形式: String s = "abc"; String s = new String("abc"); 第 ...

  7. Sorting It All Out 拓扑排序+确定点

    这一道题的话  数据有一点问题    ........     例如 不过 还是   能理解一下  试试吧  ......... A<B B<C C<A A<C B<A ...

  8. 在3D中两条射线的相交性检测

    摘自[3D数学基础: 图形与游戏开发] 考虑在3D中两条以参数形式定义的射线: \(\vec{r_1}(t_1)=\vec{p_1}+t_1\vec{d_1}\) \(\vec{r_2}(t_2)=\ ...

  9. C#内容格式刷html 转成txt

    /// <summary> /// 内容格式刷 /// </summary> /// <param name="strHtml">要格式的文本& ...

  10. 属性字符串(NSAttributedString)的简单应用

    属性字符串NSAttributedString 可以对字符串附加格式信息,由于对于对不同文本片段使用不同的格式,属性字符串类特别合适. IOS 6中对样式文本有了大改善,大部分主要的UIKit控件都允 ...