diff --git a/exercises/1901110099/1001S02E01_helloworld.txt b/exercises/1901110099/1001S02E01_helloworld.txt new file mode 100644 index 000000000..77e4c5c11 --- /dev/null +++ b/exercises/1901110099/1001S02E01_helloworld.txt @@ -0,0 +1 @@ +day 1 assignment by oli9 \ No newline at end of file diff --git a/exercises/1901110099/1001S02E02_hello_python.py b/exercises/1901110099/1001S02E02_hello_python.py new file mode 100644 index 000000000..72d4e69a3 --- /dev/null +++ b/exercises/1901110099/1001S02E02_hello_python.py @@ -0,0 +1,2 @@ +msg="hello world" +print(msg) \ No newline at end of file diff --git a/exercises/1901110099/1001S02E03_calculator.py b/exercises/1901110099/1001S02E03_calculator.py new file mode 100644 index 000000000..d107bcac6 --- /dev/null +++ b/exercises/1901110099/1001S02E03_calculator.py @@ -0,0 +1,32 @@ +def calculation(): + operation=input(''' +Please type in the math type you want to operate: ++ for add +- for abtract +* for multiply +/ for divide +''') + + number_1=int(input('please type the first number:')) + number_2=int(input('please type the second number:')) + + if operation=='+': + print('{}+{}='.format(number_1,number_2)) + print(number_1+number_2) + + elif operation=='-': + print('{}-{}='.format(number_1,number_2)) + print(number_1-number_2) + + elif operation=='*': + print('{}*{}='.format(number_1,number_2)) + print(number_1*number_2) + + elif operation=='/': + print('{}/{}='.format(number_1,number_2)) + print(number_1/number_2) + + else: + print('you have operated wrongly, please try again') + +calculation() \ No newline at end of file diff --git a/exercises/1901110099/1001S02E04_control_flow.py b/exercises/1901110099/1001S02E04_control_flow.py new file mode 100644 index 000000000..3c275afe9 --- /dev/null +++ b/exercises/1901110099/1001S02E04_control_flow.py @@ -0,0 +1,14 @@ +print('打印九九乘法表') +for n in range(1,10): + for x in range(1,n+1): + print(n,'*',x,'=',n*x,end='\t') + print() + +print('打印跳过偶数行') +n=1 +while n<10: + if n%2>0: + for x in range(1,n+1): + print(n,'*',x,'=',n*x,end='\t') + print() + n+=1 diff --git a/exercises/1901110099/1001S02E05_array.py b/exercises/1901110099/1001S02E05_array.py new file mode 100644 index 000000000..817403b7f --- /dev/null +++ b/exercises/1901110099/1001S02E05_array.py @@ -0,0 +1,16 @@ +list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +list.reverse() #将数组 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 翻转 +print(list) +list_str=''.join([str(x) for x in list]) #翻转后的数组拼接成字符串 +print(list_str) +list_str[3:9] #用字符串切⽚的⽅式取出第三到第八个字符(包含第三和第八个字符) +print(list_str[3:9]) +str1=list_str[3:9] +print(str1[::-1]) #将获得的字符串进行翻转 +str2=str1[::-1] +int(str2) #将结果转换为 int 类型 +print(int(str2)) +int1=int(str2) +print(bin(int1)) #转换成二进制并输出结果 +print(oct(int1)) #转换成八进制并输出结果 +print(hex(int1)) #转换成十六进制并输出结果 \ No newline at end of file diff --git a/exercises/1901110099/1001S02E05_stats_text.py b/exercises/1901110099/1001S02E05_stats_text.py new file mode 100644 index 000000000..72c2538d2 --- /dev/null +++ b/exercises/1901110099/1001S02E05_stats_text.py @@ -0,0 +1,53 @@ +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 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! +''' + +text1=text.replace(',','').replace('.','').replace('--','').replace('*','').replace('!','') +text2=text1.split() +dict1={} +for letter in text2: + if letter in dict1: + dict1[letter]=dict1[letter]+1 + else: + dict1[letter]=1 +print(dict1) + +print() +print('按照出现次数从大到小输出所有的单词及出现的次数') +print(sorted(dict1.items(),key = lambda x:x[1],reverse = True)) + + + + + + + + + + + + + + + diff --git a/exercises/1901110099/1001S02E05_string.py b/exercises/1901110099/1001S02E05_string.py new file mode 100644 index 000000000..1a953d238 --- /dev/null +++ b/exercises/1901110099/1001S02E05_string.py @@ -0,0 +1,52 @@ +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 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! +''' +print('将字符串串样本 text ⾥里里的 better 全部替换成 worse') +print(text.replace('better','worse')) + +print() +print('从第 2 步的结果⾥,将单词中包含 ea 的单词剔除') +text1=text.replace('better','worse') +text2=text1.split() # string 转为 list +text3=[] +for letter in text2: + if 'ea' not in letter: + text3.append(letter) +print(text3) + + +print() +print('将第 3 步的结果⾥的字⺟进⾏⼤⼩写翻转(将⼤写字母转成⼩写,⼩写字母转成大写)') +str=' ' +text4=str.join(text3) # list 转为 string +print(text4.swapcase()) + + +print() +print('将第 4 步的结果⾥所有单词按 a…z 升序排列列,并输出结果') +text5=text4.swapcase() +text6=text5.split() # string 转为 list +text6.sort(key=lambda x:x.lower()) +print(text6) \ No newline at end of file diff --git a/exercises/1901110099/1001S02E06_stats_word.py b/exercises/1901110099/1001S02E06_stats_word.py new file mode 100644 index 000000000..086b53752 --- /dev/null +++ b/exercises/1901110099/1001S02E06_stats_word.py @@ -0,0 +1,63 @@ +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 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! +''' + +def stats_text_en(text): #统计参数中每个英⽂单词出现的次数,最后返回⼀个按词频降序排列的数组 + text1=text.replace(',','').replace('.','').replace('--','').replace('*','').replace('!','') + text2=text1.split() + dict1={} + for letter in text2: + if letter in dict1: + dict1[letter]=dict1[letter]+1 + else: + dict1[letter]=1 + dict2=sorted(dict1.items(),key = lambda x:x[1],reverse = True) + return dict2 + +print("统计参数中每个英⽂单词出现的次数,最后返回⼀个按词频降序排列的数组") +print(stats_text_en(text)) + +print() + +text_cn=''' +在没风的地方找太阳,在你冷的地方做暖阳,人事纷纷,你总太天真 +''' +def stats_text_cn(text_cn): #统计参数中每个中⽂汉字出现的次数,最后返回⼀个按字频降序排列的数组 + text_cn1=text_cn.replace(',','') + + word_list=[] + for character in text_cn1: + if '\u4e00'<='\u9fa5': + word_list.append(character) + + word_dict={} + set1=set(word_list) + for character in set1: + word_dict[character]=word_list.count(character) + + word_dict1=sorted(word_dict.items(), key=lambda x: x[1], reverse=True) + return word_dict1 + +print("统计参数中每个中⽂汉字出现的次数,最后返回一个按字频降序排列的数组") +print(text_cn) +print(stats_text_cn(text_cn)) + diff --git a/exercises/1901110099/README.md b/exercises/1901110099/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/exercises/1901110099/d07/main.py b/exercises/1901110099/d07/main.py new file mode 100644 index 000000000..b1774747d --- /dev/null +++ b/exercises/1901110099/d07/main.py @@ -0,0 +1,46 @@ +from mymodule import stats_word + +new_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(new_text) + +print(result) \ No newline at end of file diff --git a/exercises/1901110099/d07/mymodule/stats_word.py b/exercises/1901110099/d07/mymodule/stats_word.py new file mode 100644 index 000000000..55ac71c01 --- /dev/null +++ b/exercises/1901110099/d07/mymodule/stats_word.py @@ -0,0 +1,70 @@ + + +def stats_text_en(text): #统计参数中每个英⽂单词出现的次数,最后返回⼀个按词频降序排列的数组 + elements=text.split() + words=[] + symbols=',.*-!' + for element in elements: + for symbol in symbols: + element=element.replace(symbol,'') + if len(element) and element.isascii(): + words.append(element) + + counter={} + word_set=set(words) + + for word in word_set: + counter[word]=words.count(word) + + return sorted(counter.items(), key=lambda x: x[1], reverse=True) + + + +def stats_text_cn(text): #统计参数中每个中⽂汉字出现的次数,最后返回⼀个按字频降序排列的数组 + word_list=[] + for character in text: + if '\u4e00'<= character <= '\u9fff': + word_list.append(character) + + word_dict={} + set1=set(word_list) + for wording in set1: + word_dict[wording]=word_list.count(wording) + + word_dict1=sorted(word_dict.items(), key=lambda x: x[1], reverse=True) + return word_dict1 + + + +def stats_text(text): #分别调⽤stats_text_en , stats_text_cn ,输出合并词频统计结果 + return stats_text_cn(text)+stats_text_en(text) + + +if __name__ == "__main__": + 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 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! +在没风的地方找太阳,在你冷的地方做暖阳,人事纷纷,你总太天真 +''' + + + print(stats_text(text)) \ No newline at end of file