From 5e41ca6818b75af785cb317c27229379c55645e8 Mon Sep 17 00:00:00 2001 From: oneisonly <532276567@qq.com> Date: Thu, 12 Sep 2019 16:55:20 +0800 Subject: [PATCH 1/9] day2 --- exercises/1901100166/1001S02E02_hello_python.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100166/1001S02E02_hello_python.py diff --git a/exercises/1901100166/1001S02E02_hello_python.py b/exercises/1901100166/1001S02E02_hello_python.py new file mode 100644 index 000000000..00950d9ac --- /dev/null +++ b/exercises/1901100166/1001S02E02_hello_python.py @@ -0,0 +1 @@ +print('hello world') \ No newline at end of file From ec46fe0217ad9305a75fd6b19a4fcb989fa37e59 Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Thu, 31 Oct 2019 14:29:36 +0800 Subject: [PATCH 2/9] day3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 辛苦老师了,第一个是视频里讲的,打印正常,第二个是我做了个简单的,但打印不出来 --- exercises/1901100166/1001s02e03_calculator.py | 23 +++++++++++++++++++ .../1901100166/1001s02e03_calculator1.py | 14 +++++++++++ 2 files changed, 37 insertions(+) create mode 100644 exercises/1901100166/1001s02e03_calculator.py create mode 100644 exercises/1901100166/1001s02e03_calculator1.py diff --git a/exercises/1901100166/1001s02e03_calculator.py b/exercises/1901100166/1001s02e03_calculator.py new file mode 100644 index 000000000..e3f3a1ba1 --- /dev/null +++ b/exercises/1901100166/1001s02e03_calculator.py @@ -0,0 +1,23 @@ +operator = input("请输入运算符(+,-,*,/):") +first_number = input("请输入第一个数字:") +second_number = input("请输入第二个数字:") + +a = int(first_number) +b = int(second_number) + +print('operator:', operator, type(operator)) +print('first_number:',first_number,type(first_number),type(a)) +print('second_number:', second_number,type(second_number),type(b)) + +print('测试加法 str 加法',first_number + second_number) + +if operator == '+': + print(a, '+', b, '=', a + b) +elif operator == '-': + print(a, '-', b, '=', a - b) +elif operator == '*': + print(a, '*', b, '=', a * b) +elif operator == '/': + print(a, '/', b, '=', a / b) +else: + print('无效的运算符') diff --git a/exercises/1901100166/1001s02e03_calculator1.py b/exercises/1901100166/1001s02e03_calculator1.py new file mode 100644 index 000000000..e4dae7eee --- /dev/null +++ b/exercises/1901100166/1001s02e03_calculator1.py @@ -0,0 +1,14 @@ +fuhao = input"请输入运算符号(+,-,*,/):" +a=int(input("请输入第一个数字")) +b=int(input("请输入第二个数字")) + +if fuhao == '+': + print(a + b) +elif fuhao == '-': + print(a - b) +elif fuhao == '*': + print(a * b) +elif fuhao == '/': + print(a / b) +else: + print('无效的运算符') \ No newline at end of file From 0bedf88240c80d45651ea3f6f5d8037c1767c523 Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Thu, 31 Oct 2019 15:11:59 +0800 Subject: [PATCH 3/9] day3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了一下 --- exercises/1901100166/1001s02e03_calculator1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/1901100166/1001s02e03_calculator1.py b/exercises/1901100166/1001s02e03_calculator1.py index e4dae7eee..a0d5bdd53 100644 --- a/exercises/1901100166/1001s02e03_calculator1.py +++ b/exercises/1901100166/1001s02e03_calculator1.py @@ -1,4 +1,4 @@ -fuhao = input"请输入运算符号(+,-,*,/):" +fuhao = input("请输入运算符号(+,-,*,/):") a=int(input("请输入第一个数字")) b=int(input("请输入第二个数字")) From 735dd8be298200de216d091a5e2e217ec9842a0f Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Mon, 11 Nov 2019 16:33:52 +0800 Subject: [PATCH 4/9] d4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两种方法 --- .../1901100166/1001s02e04_control_flow.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 exercises/1901100166/1001s02e04_control_flow.py diff --git a/exercises/1901100166/1001s02e04_control_flow.py b/exercises/1901100166/1001s02e04_control_flow.py new file mode 100644 index 000000000..f343caa0d --- /dev/null +++ b/exercises/1901100166/1001s02e04_control_flow.py @@ -0,0 +1,42 @@ + +print('打印九九乘法表') +for i in range(1,10): + print('第%d行'%i,end='\t') + for j in range(1,i+1): + + print('{}*{}={}'.format(i,j,i*j),end='\t') + print() + +print('\n\n打印奇数行九九乘法表') + +i=1 +while i<10: + if i%2==0: + print('跳过第%d行'%i) + else: + print('第%d行'%i,end='\t') + for j in range(1,i+1): + + print('{}*{}={}'.format(i,j,i*j),end='\t') + print() + i+=1 + +# 用for in 打印九九乘法口诀表 +for a in range(1,10): + for b in range(1,a+1): + print(a,'*',b,'=',a*b,end="\t") + print() + + +print() + +# 用While 打印九九乘法表并用条件判断把偶数⾏行行去除掉 +a=1 +while a<10: + if a%2>0: + b=1 + while b',fanzhuan_list) #此时是int类型元素 + +#2. + +A=''.join([str(j)for j in fanzhuan_list]) #''符号表示元素间不用任何字符隔开 + #转换成str类型 的列表 +print('翻转后的数组拼接成字符串==>',A) + +#3. +b_str=A[2:8] +print('用字符串切片的方式取出第三到八个字符==>',b_str) + +#4. +C=b_str[::-1] +print('切片字符串翻转==>',C) + +#5. +E=int(C) + +print('转为int类型==》',E) + +print('转为 二进制==》',bin(E)) +print('转为 八进制==》',oct(E)) +print('转为 十六进制==》',hex(E)) \ No newline at end of file diff --git a/exercises/1901100166/1001s02e05_stats_text.py b/exercises/1901100166/1001s02e05_stats_text.py new file mode 100644 index 000000000..8b1b4cc70 --- /dev/null +++ b/exercises/1901100166/1001s02e05_stats_text.py @@ -0,0 +1,51 @@ +a = ''' +The Zen of Python, by Tim Peters +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + +#1.使用字典dict类型统计单词出现次数 + +A=a.split() #字符串分割成list +B=[] #定义新的list型变量,储存处理后的单词 +C=',.*-!' #选出样本中要剔除的非单词符号 + +for AA in A: #遍历一遍要剔除的符号 + for CC in C: #逐个替换字符号,用''是为了同时剔除符号所占位置 + AA=AA.replace(CC,'') #剔除字符后如果AA的长度不为0,则算正常单词 + if len(AA): + B.append(AA) + +print('纯单词==>',B) + +counter={} #初始化一个dict类型的变量,来存放单词出现次数 + +B_set=set(B) #set(集合)使重复的元素只出现1次,减少在for in 里的循环次数 + +for BB in B_set: + counter[BB]=B.count(BB) #BB是对单词的项进行赋值,表明单词出现多少次 + +print('单词次数==>',counter) + +#2. +print('从大到小==>',sorted(counter.items(),key=lambda x:x[1],reverse=True)) + +#E.items() 是一个字典的方法,会返回一个列表即含(key,value)的 元组,我们是在对此列表排序, +# key指单词,value指单词出现的次数,lambda函数取第一项 \ No newline at end of file diff --git a/exercises/1901100166/1001s02e05_string.py b/exercises/1901100166/1001s02e05_string.py new file mode 100644 index 000000000..8b15ab597 --- /dev/null +++ b/exercises/1901100166/1001s02e05_string.py @@ -0,0 +1,50 @@ +a = ''' +The Zen of Python, by Tim Peters +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + +#1.将字符串样本里的 better 全部替换成 worse + +# 调用 str 类型 的 replace 方法进行替换 +b= a.replace('better','worse') +#print('将字符串样本里的 better 全部替换成 worse ==>', b) + +#2. +c=a.split() #调用str类型,将字符串 根据 空白字符 分割成list +guolv=[] #定义list类型的变量存放过滤后的单词 +for d in c: #句型判断是否含ea + if d.find('ea')<2: # str类型的find方法 不含参数则返回-1,含则返回第一次出现的索引 + guolv.append(d) +print('剔除ea',guolv) + +#3.大小写翻转 +E=[e.swapcase() for e in guolv] +#print('333333',E) + +#4.单词升序排列 +#print('升序==>',sorted(E)) #sorted默认升序 + +#print('降序==>',sorted(E, reverse=True)) + + + + + From cceebb34a387150388b5d830bd38520e95a54c6f Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Mon, 25 Nov 2019 15:49:55 +0800 Subject: [PATCH 6/9] d6 --- exercises/1901100166/1001s02e06_stats_word.py | 67 +++++++++++++++++++ exercises/1901100166/111.py/111.py | 11 +++ 2 files changed, 78 insertions(+) create mode 100644 exercises/1901100166/1001s02e06_stats_word.py create mode 100644 exercises/1901100166/111.py/111.py diff --git a/exercises/1901100166/1001s02e06_stats_word.py b/exercises/1901100166/1001s02e06_stats_word.py new file mode 100644 index 000000000..0d6206599 --- /dev/null +++ b/exercises/1901100166/1001s02e06_stats_word.py @@ -0,0 +1,67 @@ + #统计英文单词出现次数 +def stats_text_en(a): + A=a.split() + B=[] + C=',.*-!' + + for AA in A: + for CC in C: + AA=AA.replace(CC,'') + if len(AA): + B.append(AA) + counter={} + B_set=set(B) + + for BB in B_set: + counter[BB]=B.count(BB) + #函数返回值用return进行返回,如果没有return,则返回值为none + return sorted(counter.items(),key=lambda x: x[1],reverse=True) + #在一个完整的文件里是不能用return值,在函数里可以用? + + + #统计中文汉字次数 + +def cn(b): + cn_character=[] + for character in b: + if'\u4e00'<=character<='\u9fff': #汉字在unicode中都有对应固定的值 + cn_character.append(character) + counter={} + cn=set(cn_character) + for character in cn: + counter[character]=cn_character.count(character) + return sorted(counter.items(),key=lambda x:x[1],reverse=True) + + + +a= ''' +The Zen of Python, by Tim Peters +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + + + +b='我爱北京天安门!' + +#if __name__ == "__main__": #这个公式作用:防止在引用这个文件时不运行此代码 + +print('英文次数==>\n',stats_text_en(a)) +print('中文次数==>\n',cn(b)) \ No newline at end of file diff --git a/exercises/1901100166/111.py/111.py b/exercises/1901100166/111.py/111.py new file mode 100644 index 000000000..dc001202f --- /dev/null +++ b/exercises/1901100166/111.py/111.py @@ -0,0 +1,11 @@ +a='123456789e' + +b=a.split() + +print(b) + +c=a.replace('1','9') +print(c) + +d=a.swapcase() +print(d) \ No newline at end of file From 60efa2d9e1735a4679ea15300122a876a0c84a56 Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Mon, 25 Nov 2019 15:52:52 +0800 Subject: [PATCH 7/9] day6 --- exercises/1901100166/1001s02e05_array.py | 2 +- exercises/1901100166/1001s02e05_string.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/1901100166/1001s02e05_array.py b/exercises/1901100166/1001s02e05_array.py index 1cb898318..290114059 100644 --- a/exercises/1901100166/1001s02e05_array.py +++ b/exercises/1901100166/1001s02e05_array.py @@ -12,7 +12,7 @@ #3. b_str=A[2:8] -print('用字符串切片的方式取出第三到八个字符==>',b_str) +print('用字符串切片的方式取出第三到八个字符==>',b_str) #从0 开始算第一个字符 #4. C=b_str[::-1] diff --git a/exercises/1901100166/1001s02e05_string.py b/exercises/1901100166/1001s02e05_string.py index 8b15ab597..3637d5574 100644 --- a/exercises/1901100166/1001s02e05_string.py +++ b/exercises/1901100166/1001s02e05_string.py @@ -25,22 +25,22 @@ # 调用 str 类型 的 replace 方法进行替换 b= a.replace('better','worse') -#print('将字符串样本里的 better 全部替换成 worse ==>', b) +print('将字符串样本里的 better 全部替换成 worse ==>', b) #2. c=a.split() #调用str类型,将字符串 根据 空白字符 分割成list guolv=[] #定义list类型的变量存放过滤后的单词 for d in c: #句型判断是否含ea - if d.find('ea')<2: # str类型的find方法 不含参数则返回-1,含则返回第一次出现的索引 + if d.find('ea')<0: # str类型的find方法 不含参数则返回-1,含则返回第一次出现的索引 guolv.append(d) -print('剔除ea',guolv) +print('剔除ea==>',guolv) #3.大小写翻转 E=[e.swapcase() for e in guolv] -#print('333333',E) +print('333333',E) #4.单词升序排列 -#print('升序==>',sorted(E)) #sorted默认升序 +print('升序==>',sorted(E)) #sorted默认升序 #print('降序==>',sorted(E, reverse=True)) From bda8c79632921d38e4f4a3d1cdbd80c6c9615ea9 Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Tue, 26 Nov 2019 15:06:49 +0800 Subject: [PATCH 8/9] d7 --- exercises/1901100166/111.py/111.py | 11 --- exercises/1901100166/d07/main.py | 71 +++++++++++++++++++ .../1901100166/d07/mymodule/stats_word.py | 70 ++++++++++++++++++ 3 files changed, 141 insertions(+), 11 deletions(-) delete mode 100644 exercises/1901100166/111.py/111.py create mode 100644 exercises/1901100166/d07/main.py create mode 100644 exercises/1901100166/d07/mymodule/stats_word.py diff --git a/exercises/1901100166/111.py/111.py b/exercises/1901100166/111.py/111.py deleted file mode 100644 index dc001202f..000000000 --- a/exercises/1901100166/111.py/111.py +++ /dev/null @@ -1,11 +0,0 @@ -a='123456789e' - -b=a.split() - -print(b) - -c=a.replace('1','9') -print(c) - -d=a.swapcase() -print(d) \ No newline at end of file diff --git a/exercises/1901100166/d07/main.py b/exercises/1901100166/d07/main.py new file mode 100644 index 000000000..a9a4f6eee --- /dev/null +++ b/exercises/1901100166/d07/main.py @@ -0,0 +1,71 @@ +from mymodule import stats_word #从m文件中导入st模块 + +text = ''' +愚公移⼭ +太⾏,王屋⼆⼭的北⾯,住了⼀個九⼗歲的⽼翁,名叫愚公。⼆⼭佔地廣闊,擋住去路,使他 +和家⼈往來極為不便。 +⼀天,愚公召集家⼈說:「讓我們各盡其⼒,剷平⼆⼭,開條道路,直通豫州,你們認為怎 +樣?」 +⼤家都異⼝同聲贊成,只有他的妻⼦表示懷疑,並說:「你連開鑿⼀個⼩丘的⼒量都沒有,怎 +可能剷平太⾏、王屋⼆⼭呢?況且,鑿出的⼟⽯⼜丟到哪裏去呢?」 +⼤家都熱烈地說:「把⼟⽯丟進渤海裏。」 +於是愚公就和兒孫,⼀起開挖⼟,把⼟⽯搬運到渤海去。 +愚公的鄰居是個寡婦,有個兒⼦⼋歲也興致勃勃地⾛來幫忙。 +寒來暑往,他們要⼀年才能往返渤海⼀次。 +住在⿈河河畔的智叟,看⾒他們這樣⾟苦,取笑愚公說:「你不是很愚蠢嗎?你已⼀把年紀 +了,就是⽤盡你的氣⼒,也不能挖去⼭的⼀⻆呢?」 +愚公歎息道:「你有這樣的成⾒,是不會明⽩的。你⽐那寡婦的⼩兒⼦還不如呢!就算我死 +了,還有我的兒⼦,我的孫⼦,我的曾孫⼦,他們⼀直傳下去。⽽這⼆⼭是不會加⼤的,總有 +⼀天,我們會把它們剷平。」 +智叟聽了,無話可說: +⼆⼭的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位⼤ +⼒神揹⾛⼆⼭。 +How The Foolish Old Man Moved Mountains +Yugong was a ninety-year-old man who lived at the north of two high +mountains, Mount Taixing and Mount Wangwu. +Stretching over a wide expanse of land, the mountains blocked +yugong’s way making it inconvenient for him and his family to get +around. +One day yugong gathered his family together and said,”Let’s do our +best to level these two mountains. We shall open a road that leads +to Yuzhou. What do you think?” +All but his wife agreed with him. +“You don’t have the strength to cut even a small mound,” muttered +his wife. “How on earth do you suppose you can level Mount Taixin +and Mount Wanwu? Moreover, where will all the earth and rubble go?” +“Dump them into the Sea of Bohai!” said everyone. +So Yugong, his sons, and his grandsons started to break up rocks and +remove the earth. They transported the earth and rubble to the Sea +of Bohai. +Now Yugong’s neighbour was a widow who had an only child eight years +old. Evening the young boy offered his help eagerly. +Summer went by and winter came. It took Yugong and his crew a full +year to travel back and forth once. +On the bank of the Yellow River dwelled an old man much respected +for his wisdom. When he saw their back-breaking labour, he ridiculed +Yugong saying,”Aren’t you foolish, my friend? You are very old now, +and with whatever remains of your waning strength, you won’t be able +to remove even a corner of the mountain.” +Yugong uttered a sigh and said,”A biased person like you will never +understand. You can’t even compare with the widow’s little boy!” +“Even if I were dead, there will still be my children, my +grandchildren, my great grandchildren, my great great grandchildren. +They descendants will go on forever. But these mountains will not +grow any taler. We shall level them one day!” he declared with +confidence. +The wise old man was totally silenced. +When the guardian gods of the mountains saw how determined Yugong +and his crew were, they were struck with fear and reported the +incident to the Emperor of Heavens. +Filled with admiration for Yugong, the Emperor of Heavens ordered +two mighty gods to carry the mountains away. +''' + + +result=stats_word.stats_text(text) + +print('结果==>',result) + + + + diff --git a/exercises/1901100166/d07/mymodule/stats_word.py b/exercises/1901100166/d07/mymodule/stats_word.py new file mode 100644 index 000000000..d79ba210e --- /dev/null +++ b/exercises/1901100166/d07/mymodule/stats_word.py @@ -0,0 +1,70 @@ + +def stats_text_en(a): + A=a.split() + B=[] + C=',.*-!' + + for AA in A: + for CC in C: + AA=AA.replace(CC,'') + if len(AA) and AA.isascii(): #用str型的isascii方法判断是否是英文单词 + B.append(AA) + counter={} + B_set=set(B) + for BB in B_set: + counter[BB]=B.count(BB) + return sorted(counter.items(),key=lambda x: x[1],reverse=True) + + + +def cn(b): + cn_character=[] + for character in b: + if'\u4e00'<=character<='\u9fff': + cn_character.append(character) + counter={} + cn=set(cn_character) + for character in cn: + counter[character]=cn_character.count(character) + return sorted(counter.items(),key=lambda x:x[1],reverse=True) + +def stats_text(d): + ''' + 合并 英文词频和 中文字频的结果 + ''' + return stats_text_en(a)+cn(b) + + + + +a= ''' +The Zen of Python, by Tim Peters +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + + + +b='我爱北京天安门!' + +#if __name__ == "__main__": #这个公式作用:防止在引用这个文件时不运行此代码 + +print('英文次数==>\n',stats_text_en(a)) +print('中文次数==>\n',cn(b)) \ No newline at end of file From 6a53b0d32e1a2c5e4db3b970834c704319def6de Mon Sep 17 00:00:00 2001 From: oneisonly <53245383+oneisonly@users.noreply.github.com> Date: Wed, 27 Nov 2019 17:04:54 +0800 Subject: [PATCH 9/9] d8 --- exercises/1901100166/d08/main.py | 26 +++++++ .../1901100166/d08/mymodule/stats_word.py | 77 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 exercises/1901100166/d08/main.py create mode 100644 exercises/1901100166/d08/mymodule/stats_word.py diff --git a/exercises/1901100166/d08/main.py b/exercises/1901100166/d08/main.py new file mode 100644 index 000000000..1d8bd835b --- /dev/null +++ b/exercises/1901100166/d08/main.py @@ -0,0 +1,26 @@ +from mymodule import stats_word #从m文件中导入st模块 + +import traceback +import logging #这是两种方法 + +logger=logging.getLogger(__name__) + +def T(): + try: + stats_word.stats_text(1) + except Exception as e: + print('T=>',e) + print(traceback.format_exc()) + +def L(): + try: + stats_word.stats_text(1) + except Exception as e: + # print('L=>',e) + logger.exception(e) + +if __name__ == "__main__": + stats_word.stats_text(1) + T() + L() + \ No newline at end of file diff --git a/exercises/1901100166/d08/mymodule/stats_word.py b/exercises/1901100166/d08/mymodule/stats_word.py new file mode 100644 index 000000000..79a24e8d6 --- /dev/null +++ b/exercises/1901100166/d08/mymodule/stats_word.py @@ -0,0 +1,77 @@ + +def stats_text_en(text): + if not isinstance(text,str): #加入的类型判断,判断a是否是str类型 + raise ValueError('参数必须是str类型,输入类型%s' % type(text)) + + A=text.split() + B=[] + C=',.*-!' + + for AA in A: + for CC in C: + AA=AA.replace(CC,'') + if len(AA) and AA.isascii(): #用str型的isascii方法判断是否是英文单词 + B.append(AA) + counter={} + B_set=set(B) + for BB in B_set: + counter[BB]=B.count(BB) + return sorted(counter.items(),key=lambda x: x[1],reverse=True) + + + +def cn(text): + if not isinstance(text,str): + raise ValueError('参数必须是str类型,输入类型%s' % type(text)) + cn_character=[] + for character in text: + if'\u4e00'<=character<='\u9fff': + cn_character.append(character) + counter={} + cn=set(cn_character) + for character in cn: + counter[character]=cn_character.count(character) + return sorted(counter.items(),key=lambda x:x[1],reverse=True) + +def stats_text(text): + if not isinstance(text,str): + raise ValueError('参数必须是str类型,输入类型%s' % type(text)) + ''' + 合并 英文词频和 中文字频的结果 + ''' + return stats_text_en(text)+cn(text) + + + + +a= ''' +The Zen of Python, by Tim Peters +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + + + +b='我爱北京天安门!' + +#if __name__ == "__main__": #这个公式作用:防止在引用这个文件时不运行此代码 + +print('英文次数==>\n',stats_text_en(a)) +print('中文次数==>\n',cn(b)) \ No newline at end of file