星球实战的一道题(数字转英文)

菜鸟玩python的题目:

如果数字1到5用英语写出:one,two,three,four,five,则总共使用3 + 3 + 5 + 4 + 4 = 19个字母。如果所有1到1001(包含一千零一)的数字都用英语写出来,会用多少个字母?

我的解题思路如下:


image.png

在解题的过程中,先利用自己所学进行尝试,先假设自己能转换成为英语单词
比如说136转换成为one hundred and thirty-six,words="one hundred and thirty-six"
先算出words 的长度26
这里面包含空格及连接符"-"
再移去空格,查到正则表达式re.sub(表达式,替换成"",要处理的字符串)

  pttn=r'\s'
  result=re.sub(pttn,'',words)

字符串变为onehundredandthirty-six
再去移去"-"
字符串变为onehundredandthirtysix
并计算单词的长度为22

至此完成了第三步,将这个过程加入到循环中,就可以算出总的字母的个数21141, 在这个过程中,会出现很多错误, 每次调试都会让自己了解的自己理解的不足之处,然后更新迭代

解题的代码下:

from  num2words import num2words
 import re
 words_number=0
for num in range(1,1002):
   pttn=r'[\s\-]'
   word=num2words(num)
   words=re.sub(pttn,'',word)
   words_number=words_number+len(words)
print(words_number)

现在再回到第1步,将数字转英文,开始以为用excel简单下拉就行,后来证实做不到,然后上网查,number to words python,就找到相关的代码
链接如下:
https://codereview.stackexchange.com/questions/39183/python-converter-number-to-english-project-euler-17

借用他人编好的程序为自己所用,同时去研究学习他人是怎样写代码的,这个过程,可以让自己更一步提升,先多读他人的代码,再自己编写代码解决问题。

数字转英文分两部份:
1.单词表

   NUMBER_WORDS = {
1 : "one",
2 : "two",
3 : "three",
4 : "four",
5 : "five",
6 : "six",
7 : "seven",
8 : "eight",
9 : "nine",
10 : "ten",
11 : "eleven",
12 : "twelve",
13 : "thirteen",
14 : "fourteen",
15 : "fifteen",
16 : "sixteen",
17 : "seventeen",
18 : "eighteen",
19 : "nineteen",
20 : "twenty",
30 : "thirty",
40 : "forty",
50 : "fifty",
60 : "sixty",
70 : "seventy",
80 : "eighty",
90 : "ninety"   }

2.函数部份:

   def convert_number_to_words(num):
#Works up to 99,999
num = str(num)
analyze = 0
postfix = remainder = None
string = ""
if len(num) > 4:
    analyze = int(num[0:2])
    remainder = num[2:]
    postfix = " thousand "
elif len(num) > 3:
    analyze = int(num[0:1])
    remainder = num[1:]
    postfix = " thousand "
elif len(num) > 2:
    analyze = int(num[0:1])
    remainder = num[1:]
    postfix = " hundred "
    if int(remainder) > 0:
        postfix += "and "
elif int(num) in NUMBER_WORDS:
    analyze = int(num)
else:
    analyze = int(num[0:1] + "0")
    remainder = num[1:]
    postfix = "-"
string = NUMBER_WORDS[analyze]
if postfix is not None:
    string += postfix
if remainder is not None and int(remainder) > 0:
    return string + convert_number_to_words(remainder)
else:
    return string

3.调用函数

   “用于测试此函数是否可行”
      num=10
while int(num) >0:
    num=input('input your number,if quit please input q:')
    if num =='q':
        break
    else:
    result=convert_number_to_words(num)
    print(result)

上面链接中的代码,也有错误,比如说当数字为1001时,它就会报错
后来,我去分析其代码,画逻辑图,才知道为什么要这么做?以及在哪里可以修改代码,逻辑图如下:


image.png

所以重新修改后的完整代码为:

 def convert_number_to_words(num):
#Works up to 99,999
num = str(num)
analyze = 0
postfix = remainder = None
string = ""
if len(num) > 4:
    analyze = int(num[0:2])
    remainder = num[2:]
    postfix = " thousand "
elif len(num) > 3:
    analyze = int(num[0:1])
    remainder = num[1:]
    postfix = " thousand "
    if int(remainder[0])== 0 and int(remainder[1:]) > 0:
        remainder= num[2:]
        postfix +=" and "
        if int(remainder[1]) == 0:
            remainder = num[3]
    else:
        remainder=num[3]
elif len(num) > 2:
    analyze = int(num[0:1])
    remainder = num[1:]
    postfix = " hundred "
    if int(remainder) > 0:
        postfix += "and "
elif int(num) in NUMBER_WORDS:
    analyze = int(num)
else:
    analyze = int(num[0:1] + "0")
    remainder = num[1:]
    postfix = "-"
string = NUMBER_WORDS[analyze]
if postfix is not None:
    string += postfix
if remainder is not None and int(remainder) > 0:
    return string + convert_number_to_words(remainder)
else:
    return string

 word_number=0
for number in range(1,1002):
   result=convert_number_to_words(number)
    move_blank=re.sub(r'\s|-','',result)
    word_number=word_number+len(move_blank)
print(word_number)

计算的结果为21141

体会:

1、学习编程可以锻炼我们的思维能力,将思考的过程可视化
2、编程可以获得即时反馈,这一点对于学习新的技能很重要,如果没有反馈,要想持续地投入时间学习,有点难,而编程可以,边尝试,边获得结果
3、对问题的拆解能力,将一个目标拆解成自己可以解决的小问题,再组合,在编程过程中得到完美的体现
4、编程是一种集体协作的活动,你可以利用他人的作品,用在自己的程序内从而更快捷地解决问题,而你的作品也可以成为他人解决问题的桥梁