From c90578a6751d4156799ec397481432e0369be207 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Mon, 9 Sep 2019 22:47:07 +0800 Subject: [PATCH 01/11] Day 1 task --- exercises/1901100375/1001S02E01_helloworld.txt | 1 + exercises/1901100375/README.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 exercises/1901100375/1001S02E01_helloworld.txt create mode 100644 exercises/1901100375/README.md diff --git a/exercises/1901100375/1001S02E01_helloworld.txt b/exercises/1901100375/1001S02E01_helloworld.txt new file mode 100644 index 000000000..8d68c0b23 --- /dev/null +++ b/exercises/1901100375/1001S02E01_helloworld.txt @@ -0,0 +1 @@ +day 1 assignment by Sally-luo diff --git a/exercises/1901100375/README.md b/exercises/1901100375/README.md new file mode 100644 index 000000000..e0abb0077 --- /dev/null +++ b/exercises/1901100375/README.md @@ -0,0 +1 @@ +# 学习心得 From e0675b42b5187c03cf70a357eccc78db2f620af7 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Sun, 15 Sep 2019 16:26:40 +0800 Subject: [PATCH 02/11] Create 1001S02E02_hello_python.py Day two task. --- exercises/1901100375/1001S02E02_hello_python.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100375/1001S02E02_hello_python.py diff --git a/exercises/1901100375/1001S02E02_hello_python.py b/exercises/1901100375/1001S02E02_hello_python.py new file mode 100644 index 000000000..41d837c79 --- /dev/null +++ b/exercises/1901100375/1001S02E02_hello_python.py @@ -0,0 +1 @@ +print("hello,world!") \ No newline at end of file From a233b517346133f4e9ea1ea45fd0551905edb8d1 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Mon, 7 Oct 2019 21:15:42 +0800 Subject: [PATCH 03/11] Create 1001S02E03_calculator.py design a calculator --- exercises/1901100375/1001S02E03_calculator.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 exercises/1901100375/1001S02E03_calculator.py diff --git a/exercises/1901100375/1001S02E03_calculator.py b/exercises/1901100375/1001S02E03_calculator.py new file mode 100644 index 000000000..d6b3fe5a5 --- /dev/null +++ b/exercises/1901100375/1001S02E03_calculator.py @@ -0,0 +1,66 @@ +#用vscode打开,并使用Python编写⼀一个支持加、减、乘、除功能的计算器,支持输⼊参数,支持输出结果。 +def findAndCheckOperator(stringToCalculate: str): + operator = "" + if stringToCalculate.find("+") != -1: + operator = "+" + elif stringToCalculate.find("-") != -1: + operator = "-" + elif stringToCalculate.find("*") != -1: + operator = "*" + elif stringToCalculate.find("/") != -1: + operator = "/" + else: + print(operator, " is illegal.") + return operator + +def getOperands(stringToCalculate: str, operator: str): + numberStrList = stringToCalculate.split(operator) + numbers = [] + for numberStr in numberStrList: + # 转换为整数 + numbers.append(int(numberStr)) + return numbers + +def doCalculate(operator: str, operands): + result = 0 + if operator == "+": + # do add. + for number in operands: + result += number + elif operator == "-": + # do minus. + result = operands[0] + subOperands = operands[1:len(operands)] + for number in subOperands: + result -= number + elif operator == "*": + # do times. + result = 1 + for number in operands: + result *= number + elif operator == "/": + # do div. + result = operands[0] + subOperands = operands[1:len(operands)] + for number in subOperands: + result /= number + else: + print(operator, " is illegal") + return result + +# 主入口 +if __name__ == "__main__": + print("Usage: Input a string to calculate.") + print(" e.g. 1+2+3") + inputStr = input("Input: ") + # print("Input is ", str) + # check str + operator = findAndCheckOperator(inputStr) + print("Operator is ", operator) + # get all operands + operands = getOperands(inputStr, operator) + print("operands: ", operands) + # calculate + result = doCalculate(operator, operands) + # print result + print(inputStr, "=", result) \ No newline at end of file From d67d197cc7f82ed8ea36be03d186e3b5edf58a95 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Sun, 13 Oct 2019 17:16:06 +0800 Subject: [PATCH 04/11] Day04 --- .../1901100375/1001S02E04_control_flow.py | 30 +++++++++++++++++++ exercises/1901110003/1001S02E03_calculator.py | 4 +-- .../1901110003/1001S02E04_control_flow.py | 9 ++---- 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 exercises/1901100375/1001S02E04_control_flow.py diff --git a/exercises/1901100375/1001S02E04_control_flow.py b/exercises/1901100375/1001S02E04_control_flow.py new file mode 100644 index 000000000..195b0e420 --- /dev/null +++ b/exercises/1901100375/1001S02E04_control_flow.py @@ -0,0 +1,30 @@ +# DAY4 控制流程 2019-10-13 +#Task1:使⽤用for...in循环打印九九乘法表 +#逻辑:1·一个数乘以另一个数; +# 2·这两个数范围是(1,10); +# 3·一个数字从1开始做乘法, +# 4·当遇到另一个数字等于一个数字时,就停止继续运算了;下一个数要换行 +# 5·打印一个数乘以另一个数等于结果 + +for i in range (1,10): + for j in range (1,10): + print(i,"X",j,"=",i*j,"\t",end="") + if i == j: + print("") + break +# prin()是默认换行的,写end=“”是为了不换行;在最后写一个print(“”)就是为了换行。 +# "\t" 是制表符,让每一个输出的间距都相等。 + +#Task2:使用while循环打印九九乘法表并⽤条件判断把偶数⾏去除掉 +i = 1 +while i<=9: + j=1 + while j<=9: + if i>=j: + if i % 2 !=0: + print(i,"X",j,"=",i*j,"\t",end="") + j +=1 + print() + i +=1 +# while与for不同是for给定了范围后自动循环,while是设定初始值,还要设定下个值的新赋值公式,不然永远是初始值。 +#一个数x,x%2!=0 就是奇数;x%2==0就是偶数 \ No newline at end of file diff --git a/exercises/1901110003/1001S02E03_calculator.py b/exercises/1901110003/1001S02E03_calculator.py index aac91ad1c..87e46c05b 100644 --- a/exercises/1901110003/1001S02E03_calculator.py +++ b/exercises/1901110003/1001S02E03_calculator.py @@ -1,4 +1,4 @@ -def add(string): + def add(string): total = 0 @@ -108,4 +108,4 @@ def division(string): else: - print("The string you input is error.") \ No newline at end of file + print("The string you input is error.") diff --git a/exercises/1901110003/1001S02E04_control_flow.py b/exercises/1901110003/1001S02E04_control_flow.py index 6e9c9f7e1..5d67895ae 100644 --- a/exercises/1901110003/1001S02E04_control_flow.py +++ b/exercises/1901110003/1001S02E04_control_flow.py @@ -1,11 +1,6 @@ #DAY4 控制流程 2019-08-04 #Task1:使⽤用for...in循环打印九九乘法表 -for i in range (1,10): - for j in range (1,10): - print(i,"X",j,"=",i*j,"\t",end="") - if i == j: - print("") - break + #Task2:使用while循环打印九九乘法表并⽤条件判断把偶数⾏去除掉 i = 1 while i < 10: @@ -14,4 +9,4 @@ else: for j in range(1,i+1): print('{} * {} = {}'.format(i,j,i * j),end = '\t') - i += 1 \ No newline at end of file + i += 1 From 8bd845de24c740338973b16af63ea571950a0b82 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Sun, 3 Nov 2019 18:00:28 +0800 Subject: [PATCH 05/11] Day05 --- exercises/1901100375/1001S02E05_array.py | 26 +++++++++ exercises/1901100375/1001S02E05_stats_text.py | 45 ++++++++++++++++ exercises/1901100375/1001S02E05_string.py | 53 +++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 exercises/1901100375/1001S02E05_array.py create mode 100644 exercises/1901100375/1001S02E05_stats_text.py create mode 100644 exercises/1901100375/1001S02E05_string.py diff --git a/exercises/1901100375/1001S02E05_array.py b/exercises/1901100375/1001S02E05_array.py new file mode 100644 index 000000000..63661116e --- /dev/null +++ b/exercises/1901100375/1001S02E05_array.py @@ -0,0 +1,26 @@ +# DAY5 掌握基本数据类型 2019-11-03 +#Task3:数组操作,进制转换 + +list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +list. reverse() +print("原列表被翻转:",list) + +# 翻转后的数组拼接成字符串 +list_str = "".join((str(i) for i in list)) +print("翻转后的数组拼接成字符串:",list_str) + +# 用字符串切片的方式取出第二个到第八个 字符 (包含第三到第八个字符) +jiequ_str = list_str[2:8] +print("取出第二个到第八个 字符 (包含第三到第八个字符)",jiequ_str) + +# 将获得的字符串进行翻转 +fz_str = jiequ_str[::-1] +print("将获得的字符串进行翻转 :",fz_str) + +# 将结果转换为 int 类型 +m = int(fz_str) +print("将结果转换为 int 类型:",m) + +print("转换为二进制:",bin(m)) +print("转换为八进制:",oct(m)) +print("转换为十六进制:",hex(m)) diff --git a/exercises/1901100375/1001S02E05_stats_text.py b/exercises/1901100375/1001S02E05_stats_text.py new file mode 100644 index 000000000..d0c9d550d --- /dev/null +++ b/exercises/1901100375/1001S02E05_stats_text.py @@ -0,0 +1,45 @@ +# DAY5 掌握基本数据类型 2019-11-03 +#Task2:统计字符串串样本中英⽂文单词出现的次数 + +text = ''' +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 ambiguity, 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! +''' + +aa = text.replace(","," ").replace("."," ").replace("--"," ").replace("!"," ").replace("*"," ").replace("\n","") +new = aa.split(" ") #分割字符串,变为列表,以空格进行分割。 + + +print("统计单词出现次数:") +d ={} #这里要用大括号 +for i in new : + j= new.count(i) # 一个元素出现的: 次数 ,此处的i 就表示一个单词,上面已经分割好;另外用法 也可以是一个字符串中的 某一个或者几个字母,比如 'ea' + d[i] = j # 这里就是把这个单词 和 出现的次数 添加到 字典中,当然也可以用 update 函数 + # d={i:j} #这里要用大括号 +d.pop("") +print(d) + + +print("按出现频次数从大到小输出:") +# sorted 函数 排序:https://docs.python.org/zh-cn/3/howto/sorting.html#sortinghowto +d1=sorted(d.items(),key=lambda x:x[1],reverse=True) +print(d1) diff --git a/exercises/1901100375/1001S02E05_string.py b/exercises/1901100375/1001S02E05_string.py new file mode 100644 index 000000000..50393f1a0 --- /dev/null +++ b/exercises/1901100375/1001S02E05_string.py @@ -0,0 +1,53 @@ +# DAY5 掌握基本数据类型 2019-11-03 + +text=''' +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. 9 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! +''' +#Task1-1:字符串的基本处理-替换 +textnew = text.replace('better','worse') +print(textnew) + +#Task1-2:字符串的基本处理-去除 +#首先是要找到含有ea的单词,要找到单词首先要用分割规律分割出每个单词(现在是每个单词后面有个空格) +#step1 分割 +listnew = textnew.split( ) +print(listnew) +#step2 把没有ea的增加到新的数列里面去 +listnew2 = [] +for i in listnew: + if "ea" in i: + pass + else: + listnew2.append(i) +#step3 转化成字符串 +result = "" +for i in listnew2: + result += i + " " +print(result) + +#Task1-3:字符串的基本处理-进行⼤小写翻转 +textnew2 = result.swapcase() +print(textnew2) + +#Task1-4:#所有单词按a...z升序排列列,并输出结果 +listnew3 = textnew2.split( ) +listnew3.sort() +print('\n单词首字母按a-z升序后新列表text5如下:\n',listnew3) From 8b78d61a7c32564fc2fe3a1f2056ad2fe78c00a4 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Thu, 7 Nov 2019 08:41:06 +0800 Subject: [PATCH 06/11] Create 1001S02E06_stats_word.py --- .../1901100375/ 1001S02E06_stats_word.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 exercises/1901100375/ 1001S02E06_stats_word.py diff --git a/exercises/1901100375/ 1001S02E06_stats_word.py b/exercises/1901100375/ 1001S02E06_stats_word.py new file mode 100644 index 000000000..3a577f0fc --- /dev/null +++ b/exercises/1901100375/ 1001S02E06_stats_word.py @@ -0,0 +1,38 @@ +text = input('请输入英文文本:') + +# 封装统计英文单词词频的函数 +def stats_text_en( text ): + # 统计输入英⽂单词出现的次数,最后返回⼀个按词频降序排列列的数组 + import re #引入库 + text_pure = re.sub(r'[^\w\s]','',text) #正则表达式进行剔除 + words = text_pure.split() # 拆分所有单词 + + frequency= {} # 定义频率字典 + for word in words: + if word not in frequency: + frequency [word] = 1 + else: + frequency [word] += 1 + frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) + print('您此次输入的文本词频降序排列如下:', frequency_order) +stats_text_en( text ) # 执行封装函数 + + +text = input('请输入中文文本:') +# 封装统计中文汉字次数的函数 +def stats_text_cn( text ): + cn_characters = [] + for character in text: + # 以unicode中中文字符存储的范围 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + frequency= {} # 定义频率字典 + for cn_characters in text: + if cn_characters not in frequency: + frequency [cn_characters] = 1 + else: + frequency [cn_characters] += 1 + frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) + print('您此次输入的文本词频降序排列如下:', frequency_order) + +stats_text_cn( text ) # 执行封装函数 From 3e77f8fade1b2e0804a4b9547d2c127b1e3005d1 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Sun, 17 Nov 2019 18:27:35 +0800 Subject: [PATCH 07/11] day07 --- exercises/1901100375/d07/mymodule/main.py | 57 ++++++++++++ .../1901100375/d07/mymodule/stats_word.py | 89 +++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 exercises/1901100375/d07/mymodule/main.py create mode 100644 exercises/1901100375/d07/mymodule/stats_word.py diff --git a/exercises/1901100375/d07/mymodule/main.py b/exercises/1901100375/d07/mymodule/main.py new file mode 100644 index 000000000..312746142 --- /dev/null +++ b/exercises/1901100375/d07/mymodule/main.py @@ -0,0 +1,57 @@ +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.''' + +import stats_word + +stats_word.stats_word(text) \ No newline at end of file diff --git a/exercises/1901100375/d07/mymodule/stats_word.py b/exercises/1901100375/d07/mymodule/stats_word.py new file mode 100644 index 000000000..a7c091e1c --- /dev/null +++ b/exercises/1901100375/d07/mymodule/stats_word.py @@ -0,0 +1,89 @@ +#1.封装统计英文单词词频的函数 +text1=''' +Remember the day I borrowed your brand new car and dented it? +I thought you'd kill me +But you didn't. +And remember the time I dragged you to the beach +And you said it would rain, and it did? +I thought you'd say, "I told you so." +But you didn't. + +Do you remember the time I flirted with all the guys? +To make you jealous, and you were? +I thought you'd leave, +But you didn't. +
Do you remember the time I spilled strawberry pie all over your car rug? +I thought you'd hit me. +But you didn't. + +And remember the time I forgot to tell you the dance was formal +And you showed up in jeans? +I thought you'd drop me. +But you didn't. + +Yes, there were lots of things you didn't do. +But you put up with me, and loved me, and protected me. +There were lots of things I wanted to make up to you when you returned from Vietnam. +But you didn't. +''' + +def stats_text_en(text): + for x in text: + for x in ',','.','?','"','!',',','。','?','!',':','「','」': + text=text.replace(x,"") + text2=text1.split() #以空格拆分为独立单词 + + for i in text2: + if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文 + text2.remove(i) #将列表中中文删除 + + dic={} + for i in text2: #将字符串转换为字典 + count=text2.count(i) + r1={i:count} + dic.update(r1) + dic1=sorted(dic.items(),key=lambda x:x[1],reverse=True) #按照单词出现次数,从大到小排序 + return(dic1) +print(stats_text_en(text1)) #调用函数并打印结果 + + + +#2.封装统计中文汉字字频的函数 +text_1 = ''' +The Zen of Python, by Tim Peters +美丽 is better than 丑陋. +清楚 is better than 含糊. +简单 is better than 复杂. +复杂 is better than 难懂. +单一 is better than 嵌套. +稀疏 is better than 稠密. +可读性计数. +Special cases aren't special enough to 打破规则. +即使练习会使得不再纯粹. +但错误不应该用沉默来掩盖. +Unless explicitly silenced. +面对起义,拒绝猜的诱惑. +有且只有一个办法. +Although that way may not be obvious at first unless you're Dutch. +尝试总比从未试过要强. +Although never is often better than *right* now. +如果执行很难被解释,那将是一个很糟的想法. +如果执行很容易解释,这会是一个好点子. +Namespaces are one honking great idea -- 让我们继续为之努力! +''' + +def stats_text_cn(text): #定义检索中文函数 + cndic={} + for i in text: + if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文 + count = text.count(i) + cndic2 = {i:count} + cndic.update(cndic2) + cndic=sorted(cndic.items(),key=lambda item:item[1],reverse = True) + return cndic +print(stats_text_cn(text_1)) #调用函数并打印结果 + +def stats_word(text): #定义函数,实现统计汉字和英文单词出现次数 + print(stats_text_en(text)) + print(stats_text_cn(text)) +stats_word(text_1) \ No newline at end of file From cb98f3f23861d6f52ca0bc97057a0a20d51c16f2 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Mon, 18 Nov 2019 17:09:59 +0800 Subject: [PATCH 08/11] day06 --- exercises/1901100375/1001S02E05_stats_text.py | 2 +- exercises/1901100375/1001S02E06_stats_word.py | 0 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 exercises/1901100375/1001S02E06_stats_word.py diff --git a/exercises/1901100375/1001S02E05_stats_text.py b/exercises/1901100375/1001S02E05_stats_text.py index d0c9d550d..5a791154a 100644 --- a/exercises/1901100375/1001S02E05_stats_text.py +++ b/exercises/1901100375/1001S02E05_stats_text.py @@ -40,6 +40,6 @@ print("按出现频次数从大到小输出:") -# sorted 函数 排序:https://docs.python.org/zh-cn/3/howto/sorting.html#sortinghowto + d1=sorted(d.items(),key=lambda x:x[1],reverse=True) print(d1) diff --git a/exercises/1901100375/1001S02E06_stats_word.py b/exercises/1901100375/1001S02E06_stats_word.py new file mode 100644 index 000000000..e69de29bb From 01f233ad2f4595bb20ef0fbc8c17ed2d58a7024a Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Mon, 18 Nov 2019 17:13:30 +0800 Subject: [PATCH 09/11] day06 --- .../1901100375/ 1001S02E06_stats_word.py | 38 ------------------- exercises/1901100375/1001S02E06_stats_word.py | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 38 deletions(-) delete mode 100644 exercises/1901100375/ 1001S02E06_stats_word.py diff --git a/exercises/1901100375/ 1001S02E06_stats_word.py b/exercises/1901100375/ 1001S02E06_stats_word.py deleted file mode 100644 index 3a577f0fc..000000000 --- a/exercises/1901100375/ 1001S02E06_stats_word.py +++ /dev/null @@ -1,38 +0,0 @@ -text = input('请输入英文文本:') - -# 封装统计英文单词词频的函数 -def stats_text_en( text ): - # 统计输入英⽂单词出现的次数,最后返回⼀个按词频降序排列列的数组 - import re #引入库 - text_pure = re.sub(r'[^\w\s]','',text) #正则表达式进行剔除 - words = text_pure.split() # 拆分所有单词 - - frequency= {} # 定义频率字典 - for word in words: - if word not in frequency: - frequency [word] = 1 - else: - frequency [word] += 1 - frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) - print('您此次输入的文本词频降序排列如下:', frequency_order) -stats_text_en( text ) # 执行封装函数 - - -text = input('请输入中文文本:') -# 封装统计中文汉字次数的函数 -def stats_text_cn( text ): - cn_characters = [] - for character in text: - # 以unicode中中文字符存储的范围 - if '\u4e00' <= character <= '\u9fff': - cn_characters.append(character) - frequency= {} # 定义频率字典 - for cn_characters in text: - if cn_characters not in frequency: - frequency [cn_characters] = 1 - else: - frequency [cn_characters] += 1 - frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) - print('您此次输入的文本词频降序排列如下:', frequency_order) - -stats_text_cn( text ) # 执行封装函数 diff --git a/exercises/1901100375/1001S02E06_stats_word.py b/exercises/1901100375/1001S02E06_stats_word.py index e69de29bb..3a577f0fc 100644 --- a/exercises/1901100375/1001S02E06_stats_word.py +++ b/exercises/1901100375/1001S02E06_stats_word.py @@ -0,0 +1,38 @@ +text = input('请输入英文文本:') + +# 封装统计英文单词词频的函数 +def stats_text_en( text ): + # 统计输入英⽂单词出现的次数,最后返回⼀个按词频降序排列列的数组 + import re #引入库 + text_pure = re.sub(r'[^\w\s]','',text) #正则表达式进行剔除 + words = text_pure.split() # 拆分所有单词 + + frequency= {} # 定义频率字典 + for word in words: + if word not in frequency: + frequency [word] = 1 + else: + frequency [word] += 1 + frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) + print('您此次输入的文本词频降序排列如下:', frequency_order) +stats_text_en( text ) # 执行封装函数 + + +text = input('请输入中文文本:') +# 封装统计中文汉字次数的函数 +def stats_text_cn( text ): + cn_characters = [] + for character in text: + # 以unicode中中文字符存储的范围 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + frequency= {} # 定义频率字典 + for cn_characters in text: + if cn_characters not in frequency: + frequency [cn_characters] = 1 + else: + frequency [cn_characters] += 1 + frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) + print('您此次输入的文本词频降序排列如下:', frequency_order) + +stats_text_cn( text ) # 执行封装函数 From 47a56e9f0219f07bc744f2176a948d6965520cc7 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Mon, 18 Nov 2019 17:55:50 +0800 Subject: [PATCH 10/11] day08 --- exercises/1901100375/d08/mymodule/main.py | 63 +++++++++++++ .../1901100375/d08/mymodule/stats_word.py | 92 +++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 exercises/1901100375/d08/mymodule/main.py create mode 100644 exercises/1901100375/d08/mymodule/stats_word.py diff --git a/exercises/1901100375/d08/mymodule/main.py b/exercises/1901100375/d08/mymodule/main.py new file mode 100644 index 000000000..e03808405 --- /dev/null +++ b/exercises/1901100375/d08/mymodule/main.py @@ -0,0 +1,63 @@ +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.''' + +import stats_word + +try: + stats_word.stats_word(text) + print("do stats_word.") +except ValueError: + print("Stats word has error.") +finally: + print("done.") diff --git a/exercises/1901100375/d08/mymodule/stats_word.py b/exercises/1901100375/d08/mymodule/stats_word.py new file mode 100644 index 000000000..200d2e100 --- /dev/null +++ b/exercises/1901100375/d08/mymodule/stats_word.py @@ -0,0 +1,92 @@ +#1.封装统计英文单词词频的函数 +text1=''' +Remember the day I borrowed your brand new car and dented it? +I thought you'd kill me +But you didn't. +And remember the time I dragged you to the beach +And you said it would rain, and it did? +I thought you'd say, "I told you so." +But you didn't. + +Do you remember the time I flirted with all the guys? +To make you jealous, and you were? +I thought you'd leave, +But you didn't. +
Do you remember the time I spilled strawberry pie all over your car rug? +I thought you'd hit me. +But you didn't. + +And remember the time I forgot to tell you the dance was formal +And you showed up in jeans? +I thought you'd drop me. +But you didn't. + +Yes, there were lots of things you didn't do. +But you put up with me, and loved me, and protected me. +There were lots of things I wanted to make up to you when you returned from Vietnam. +But you didn't. +''' + +def stats_text_en(text): + for x in text: + for x in ',','.','?','"','!',',','。','?','!',':','「','」': + text=text.replace(x,"") + text2=text1.split() #以空格拆分为独立单词 + + for i in text2: + if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文 + text2.remove(i) #将列表中中文删除 + + dic={} + for i in text2: #将字符串转换为字典 + count=text2.count(i) + r1={i:count} + dic.update(r1) + dic1=sorted(dic.items(),key=lambda x:x[1],reverse=True) #按照单词出现次数,从大到小排序 + return(dic1) +# print(stats_text_en(text_1)) #调用函数并打印结果 + + + +#2.封装统计中文汉字字频的函数 +text_1 = ''' +The Zen of Python, by Tim Peters +美丽 is better than 丑陋. +清楚 is better than 含糊. +简单 is better than 复杂. +复杂 is better than 难懂. +单一 is better than 嵌套. +稀疏 is better than 稠密. +可读性计数. +Special cases aren't special enough to 打破规则. +即使练习会使得不再纯粹. +但错误不应该用沉默来掩盖. +Unless explicitly silenced. +面对起义,拒绝猜的诱惑. +有且只有一个办法. +Although that way may not be obvious at first unless you're Dutch. +尝试总比从未试过要强. +Although never is often better than *right* now. +如果执行很难被解释,那将是一个很糟的想法. +如果执行很容易解释,这会是一个好点子. +Namespaces are one honking great idea -- 让我们继续为之努力! +''' + +def stats_text_cn(text): #定义检索中文函数 + cndic={} + for i in text: + if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文 + count = text.count(i) + cndic2 = {i:count} + cndic.update(cndic2) + cndic=sorted(cndic.items(),key=lambda item:item[1],reverse = True) + return cndic +#print(stats_text_cn(text_1)) #调用函数并打印结果 + +def stats_word(text): #定义函数,实现统计汉字和英文单词出现次数 + if not isinstance(text, str): + exception = ValueError("input is not string.") + raise exception + print(stats_text_en(text)) + print(stats_text_cn(text)) +# stats_word(text_1) \ No newline at end of file From b878734b24211b51d2b435c6532883b77d6b5673 Mon Sep 17 00:00:00 2001 From: Sally-luo <395308304@qq.com> Date: Wed, 27 Nov 2019 21:36:59 +0800 Subject: [PATCH 11/11] day9 --- exercises/1901100375/d09/main.py | 38 ++++++++++++++ .../1901100375/d09/mymodule/stats_word.py | 49 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 exercises/1901100375/d09/main.py create mode 100644 exercises/1901100375/d09/mymodule/stats_word.py diff --git a/exercises/1901100375/d09/main.py b/exercises/1901100375/d09/main.py new file mode 100644 index 000000000..9671c2aa9 --- /dev/null +++ b/exercises/1901100375/d09/main.py @@ -0,0 +1,38 @@ +text1 = ''' +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! +红酥手,黄藤酒,满城春色宫墙柳。等闲识得东风面,万紫千红总是春。 +''' + +from mymodule.stats_word import stats_text + + +stats_text() + + + + + + + + + + diff --git a/exercises/1901100375/d09/mymodule/stats_word.py b/exercises/1901100375/d09/mymodule/stats_word.py new file mode 100644 index 000000000..e3e54ff43 --- /dev/null +++ b/exercises/1901100375/d09/mymodule/stats_word.py @@ -0,0 +1,49 @@ +from inspect import signature +from functools import wraps +import re,collections + + + + +import re +import os +from collections import Counter + + +def get_words_en(file): + with open (file,'r', encoding='UTF-8') as f: + words_box=[] + for line in f: + if re.match(r'[a-zA-Z0-9]*',line):#避免中文影响 + words_box.extend(line.strip().split()) + user_counter = collections.Counter(words_box) + print(user_counter.most_common(100)) + + +def get_words_cn(file): + with open (file,'r', encoding='UTF-8') as f: + words_box=[] + for line in f: + if re.match(u'[\u4e00-\u9fff]+',line):#避免英文影响 + words_box.extend(line.strip().split()) + user_counter = collections.Counter(words_box) + print(user_counter.most_common(100)) + + +def stats_text(): + #假设要读取文件名为aa,位于当前路径 + filename = 'd09/tang300.json' + dirname = os.getcwd() + fname = os.path.join(dirname, filename) + + get_words_en(fname) + #get_words_cn(fname) + + + + + + + + +