From 55bb8acd8eaab8b434982be24642c3038796105a Mon Sep 17 00:00:00 2001 From: May^_^ Date: Wed, 11 Sep 2019 13:25:53 +0700 Subject: [PATCH 1/2] 1901100169 Day5 Work --- exercises/1901100169/1001S02E05_array.py | 24 +++++++++++ exercises/1901100169/1001S02E05_stats_text.py | 40 +++++++++++++++++++ exercises/1901100169/1001S02E05_string.py | 40 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 exercises/1901100169/1001S02E05_array.py create mode 100644 exercises/1901100169/1001S02E05_stats_text.py create mode 100644 exercises/1901100169/1001S02E05_string.py diff --git a/exercises/1901100169/1001S02E05_array.py b/exercises/1901100169/1001S02E05_array.py new file mode 100644 index 000000000..79ee89b86 --- /dev/null +++ b/exercises/1901100169/1001S02E05_array.py @@ -0,0 +1,24 @@ +#1.将数组 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 翻转 +sample_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +reversed_list = sample_list[1:-1] +print('list reversed',reversed_list) + +#2. 翻转后的数组拼接成字符串 +joined_str = ''.join([str(i) for i in reversed_list]) +print('翻转后的数组拼接成字符串 ==>',joined_str) + +#3. 用字符串切片的方式取出第三到第八个字符(包含第三和第八个字符) +sliced_str = joined_str[2:8] +print('用字符串切片的方式取出第三到第八个字符==>',sliced_str) + +#4. 将获得的字符串进行翻转 +reversed_str = sliced_str[1:-1] +print('字符串翻转',reversed_str) + +#5. 将结果转换为 int 类型 +int_value = int(reversed_str) +print('转换为 int 类型',int_value) +#6. 分别转换成二进制,八进制,十六进制 +print('转换成二进制',bin(int_value)) +print('转换成八进制',oct(int_value)) +print('转换成进十六制',hex(int_value)) \ No newline at end of file diff --git a/exercises/1901100169/1001S02E05_stats_text.py b/exercises/1901100169/1001S02E05_stats_text.py new file mode 100644 index 000000000..fbf7d5eca --- /dev/null +++ b/exercises/1901100169/1001S02E05_stats_text.py @@ -0,0 +1,40 @@ +sample_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! +''' +#1. 统计字符串样本 text 中各个英文单词出现的次数 +elements = sample_text.split() +words = [] +symbols = ',.-*!' +for element in elements: + for symbol in symbols: + element = element.replace(symbol,'') + if len(element): + words.append(element) +print('正常的英文单词',words) +counter = {} +word_set = set(words) +for word in word_set: + counter[word] = words.count(word) +print('英文单词出现的次数',counter) + +#2. 按照出现次数从大到小输出所有的单词及出现的次数 +print('从大到小输出所有的单词及出现的次数',sorted(counter.items(),key=lambda x:x[1],reverse=True)) diff --git a/exercises/1901100169/1001S02E05_string.py b/exercises/1901100169/1001S02E05_string.py new file mode 100644 index 000000000..0b7869336 --- /dev/null +++ b/exercises/1901100169/1001S02E05_string.py @@ -0,0 +1,40 @@ +sample_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! +''' +#1. 将字符串串样本 text 里的 better 全部替换成 worse +text = sample_text.replace('better','worse') +print('将字符串样本里的 better,全部替换成 worse',text) +#2. 将单词中包含 ea 的单词剔除 +words = text.split() +filtered = [] +for word in words: + if word.find('ea')<0: + filtered.append(word) +print('将单词中包含 ea 的单词剔除',filtered) +#3. 进行大小写翻转 +swapcase=[i.swapcase() for i in filtered] +print('进行大小写翻转',swapcase) +#4. 按 a…z 升序排列 +print('单词按 a…z 升序排列',sorted(swapcase)) +#print('单词按 a…z 降序排列',sorted(swapcase,reverse=True)) + + From 3a066644ae1e82a57dee24d8d3a2bc4394e51978 Mon Sep 17 00:00:00 2001 From: May^_^ Date: Fri, 20 Sep 2019 15:17:10 +0800 Subject: [PATCH 2/2] Create 1001S02E06_stats_word.py --- exercises/1901100169/1001S02E06_stats_word.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 exercises/1901100169/1001S02E06_stats_word.py diff --git a/exercises/1901100169/1001S02E06_stats_word.py b/exercises/1901100169/1001S02E06_stats_word.py new file mode 100644 index 000000000..2c711199f --- /dev/null +++ b/exercises/1901100169/1001S02E06_stats_word.py @@ -0,0 +1,70 @@ +#封装统计英文单词词频的函数 +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): + elements = text.split() # 形成单词列表,通过string的split()将字符串切成数组 + words = [] #定义一个列表,列表用[]括起 + symbols = ',.-*!' # 标示出标点符号 + for element in elements: + for symbol in symbols: + element = element.replace(symbol,'') + if len(element): + words.append(element) + #print('正常的英文单词',words) + counter = {} + word_set = set(words) + for word in word_set: + counter[word] = words.count(word) + print('英文单词出现的次数',counter) + print('单词按 a…z 降序排列',sorted(counter.items(), key=lambda x:x[1],reverse=True)) + +print(stats_text_en(text)) + +import re +text1=''' +蜀人杜渭江朝绅令麻城,居官执法,不敢干以私。 +一日宴乡绅,梅西野倡令,要拆字入俗语二句。 +梅云:“单奚也是奚,加点也是溪,除却溪边点,加鸟却为鸡。 +俗语云:得志猫儿雄似虎,败翎鹦鹉不如鸡。” +毛石崖云:“单青也是青,加点也是清。 +除却清边点,加心却为情。 +俗语云:火烧纸马铺,落得做人情。” +杜答云:“单相也是相,加点也是湘。 +除却湘边点,加雨却为霜。 +俗语云:各人自扫门前雪,莫管他家瓦上霜。” +又云:“单其也是其,加点也是淇。 +除却淇边点,加欠却为欺。 +俗语云:龙居浅水遭虾戏,虎落平阳被犬欺。” +(《古今谭概·谈资部·梅、郭二令相同》) +''' +def stats_text_cn(text1): + p=re.compile(r'[\u4e00-\u9fa5]') # 中文的编码范围是\u4e00到\u9fa5] + res=re.findall(p,text1) #Chinese choosen + counter={} + for i in res: + counter[i]=res.count(i) + counter=sorted(counter.items(),key=lambda x:x[1],reverse=True) + print(counter) + return +print(stats_text_cn(text1)) +