Problem 17

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
如果1到5写成英语,然后再把英语单词的字母数量加起来,我们会得到19。
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
如果所有的从1到1000(包括1000)的数字都写成英语单词,那需要多少个字母呢?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen)
contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
注意:不要计算空白符以及连字符,需要计入‘and’单词。
def number_to_word(num: int) -> int:
determine_thousand = lambda num: int(str(num)[-4]) if len(str(num)) >= 4 else 0
thousand = determine_thousand(num)
determine_hundred = lambda num: int(str(num)[-3]) if len(str(num)) >= 3 else 0
hundred = determine_hundred(num)
determine_ten = lambda num: int(str(num)[-2]) if len(str(num)) >= 2 else 0
ten = determine_ten(num)
one = int(str(num)[-1]) word = 0
if ten == 1:
word += ten_to_twenty(int(str(num)[-2:]))
else:
word += one_digit(one)
word += ten_digit(ten)
word += hundred_digit(hundred)
if hundred:
if ten or one:
word += 3 # and
word += thousand_digit(thousand)
return word def one_digit(num: int) -> int:
if num == 0:
return 0
word = 0
if num in [1, 2, 6]: # one, two, six, ten
word = 3
elif num in [3, 7, 8]: # three, seven, eight
word = 5
else: # 4, 5, 9 four, five, nine
word = 4
return word def ten_digit(num: int) -> int:
if num == 0:
return 0
word = 0
if num in [2, 3, 8, 9]: # twenty, thirty, eighty, ninety
word = 6
elif num in [4, 5, 6]: # forty, fifty, sixty
word = 5
elif num == 7: # seventy
word = 7
return word def hundred_digit(num: int) -> int:
if num == 0:
return 0
word = 0
word = one_digit(num)
word += 7 # hundred
return word def thousand_digit(num: int) -> int:
if num == 0:
return 0
word = 0
word = one_digit(num)
word += 8 # thousand
return word def ten_to_twenty(num: int) -> int:
if num == 0:
return 0
word = 0
if num == 10: # ten
word = 3
elif num in [11, 12]: # eleven, twelve
word = 6
elif num in [13, 14, 18, 19]: # thirteen, fourteen, eighteen, nineteen
word = 8
elif num in [15, 16]: # fifteen, sixteen
word = 7
elif num == 17: # seventeen
word = 9
return word if __name__ == '__main__':
tot = 0
for i in range(1001):
word = number_to_word(i)
print(i, word)
tot += word
print(tot)

Problem 17的更多相关文章

  1. (Problem 17)Number letter counts

    If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...

  2. 【UOJ #17】【NOIP 2014】飞扬的小鸟

    http://uoj.ac/problem/17 dp,注意细节. #include<cstdio> #include<cstring> #include<algorit ...

  3. UOJ #17. 【NOIP2014】飞扬的小鸟 背包DP

    #17. [NOIP2014]飞扬的小鸟 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4902  Solved: 1879 题目连接 http:// ...

  4. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  5. 江西理工大学南昌校区cool code竞赛

    这次比赛原本就是来打酱油的,想做个签到题就走!一开始不知道1002是签到题,一直死磕1001,WA了四发过了,回头一看Rank,三十名,我靠!看了1001的AC率,在我AC之前只有一个人AC了,当时我 ...

  6. CF17E:Palisection——题解

    https://vjudge.net/problem/CodeForces-17E http://codeforces.com/problemset/problem/17/E 题目大意:给一个长度为n ...

  7. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  8. B. Hierarchy

    http://codeforces.com/problemset/problem/17/B 用邻接矩阵建图后, 设cost[v]表示去到顶点v的最小值. 很多个人去顶点v的话,就选最小的那个就OK 然 ...

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

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

随机推荐

  1. Android开发之PullToRefresh的Click点击事件的监听实现长按删除Item

    本文为原创博客.出自http://blog.csdn.net/minimicall 到今天为止,搜芽的卖家版本号应该来说已经基本完毕.攻坚克难的一路过来.速度也控制的比較好. 项目过程进度 从任务分配 ...

  2. erlang Unicode 处理

    最近在使用erlang做游戏服务器,而字符串在服务器编程中的地位是十分重要的,于是便想仔细研究下字符编码,以及erlang下的字符串处理.先从Unicode开始吧.... [Unicode] Unic ...

  3. apache ant解压zip。支持多级文件夹解压

    package cn.liuc.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcepti ...

  4. 美团网 KVM虚拟化公开课学习笔记

    KVM优化技术,美团开放平台--邱剑 基于KVM现有选项做一些优化.视频地址:http://www.osforce.cn/course/77/learn#lesson/80 CPU调优: 1.Cont ...

  5. ios 视频播放代码Demo

    方法一: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. / ...

  6. HashMap源代码剖析

    大部分思路都是一样的 .仅仅是一些细节不一样.源代码中都标了出来.jdk容器源代码还是挺简单的. public class HashMap<K,V> extends AbstractMap ...

  7. Middle-help 终极实现元素水平垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. JNDI 笔记(一) 概述

    很多地方都会用到JNDI,一大堆的缩写加上一大堆不清不楚的概念描述,使得在看到的时候都不认识,更不要说使用了.   JNDI,Java Naming Directory Interface,J2EE的 ...

  9. DNS隐蔽通道 是可以通过dig 子域名来追踪其真实IP的

    比如a.friendskaka.com 是我的外发子域名,那么可以按照下面两个命令来追踪IP: bonelee@bonelee-VirtualBox:~/桌面$ dig auth.a.friendsk ...

  10. 地图使用-----MapKit介绍

    一.MapKit介绍 1.苹果自带地图功能(高德地图),可以提供地图展示,查询,定位,导航等功能.使用MapKit框架实现地图功能,MapKit框架中所有数据类型的前缀都是MK 2.MapKit有一个 ...