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..7b00b88fa 100644 --- a/exercises/1901100166/1001s02e05_string.py +++ b/exercises/1901100166/1001s02e05_string.py @@ -25,26 +25,25 @@ # 调用 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)) - 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/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 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