diff --git a/exercises/1901100362/1001S02E02_hello_python.py b/exercises/1901100362/1001S02E02_hello_python.py index 7ddfc8f85..e47483a9d 100644 --- a/exercises/1901100362/1001S02E02_hello_python.py +++ b/exercises/1901100362/1001S02E02_hello_python.py @@ -1 +1,3 @@ + + print('hello world!') \ No newline at end of file diff --git a/exercises/1901100362/1001S02E05_array.py b/exercises/1901100362/1001S02E05_array.py new file mode 100644 index 000000000..5aaf467b2 --- /dev/null +++ b/exercises/1901100362/1001S02E05_array.py @@ -0,0 +1,23 @@ +arr = [0,1,2,3,4,5,6,7,8,9] +#翻转arr数组 +arr.reverse() + +#将数组元素拼接成字符串 +l1 = [str(a) for a in arr] +s1 = ''.join(l1) +print(s1) + +#对字符串切片,取出第三到第八个字符 +s2 = s1[2:9] +print(s2) +#对字符串进行翻转 +s3 = s2[::-1] +print(s3) +#转换成十进制 +in1 = int(s3) +#转换成8禁制 +oct1 = oct(in1) +#转换成16进制 +hex1 = hex(in1) +print(f'{oct1} \n{hex1}') + diff --git a/exercises/1901100362/1001S02E05_stats_text.py b/exercises/1901100362/1001S02E05_stats_text.py new file mode 100644 index 000000000..50c71c147 --- /dev/null +++ b/exercises/1901100362/1001S02E05_stats_text.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. 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!''' + + +text = text.replace('-','').replace('.','').replace('*','').replace('!','').replace(',','') + +word = text.split() +wordDic = {} + + +for key in word: + if key in wordDic: + wordDic[key] +=1 + else: + wordDic[key] = 1 + +#print(wordDic) +print('----------------------------------------------') + +#返回元素魏元组的列表 +wordSort = sorted(wordDic.items(),key = lambda x:x[1],reverse=True) +#print(wordSort) +print(dict(wordSort)) + + + + + + + + + + + + + + + + + + + + + diff --git a/exercises/1901100362/1001S02E05_string.py b/exercises/1901100362/1001S02E05_string.py new file mode 100644 index 000000000..731308d38 --- /dev/null +++ b/exercises/1901100362/1001S02E05_string.py @@ -0,0 +1,68 @@ +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!''' + +#把better替换成worse +t1 = text.replace('better','worse') +print(t1) +print("-------------------------------------------------------------------------------") + +#删除字符串中的带ea的单词 +l1 = text.strip().split() +l2 = [] +for i in range(len(l1)): + if l1[i].find('ea') == -1: + l2.append(l1[i]) + +s1 = ' '.join(l2) + +#大小写转换 +s2 = s1.swapcase() +print(s2) +print('---------------------------------------------------------------------------------') + +#按单词排序 +s3 = s2.replace('*','').replace('.','').replace('-','').replace('!','').replace(',','') +l3 = s3.split() +l4 = sorted(l3) +print(sorted(l3)) + +print('----------------------------------') +s4 = ' '.join(l4) +print(s4) + +#print(s3) +#l3 = [] +#for a in s2.split(','): +# for b in a.split(): + +# l3.append(b.strip('*').strip('.').strip('-').strip('9').strip('!')) + +#去掉列表中的空字符元素 +#l4 = list(filter(None,l3)) + +#可以用 sort和sorted两种方法对列表元素排序 +#方法1:sort函数 +#l4.sort() +#s3 = ','.join(l4) +#print(s3) +#方法二:sorted函数 +#print(sorted(l4))