diff --git a/exercises/1901100053/1001S02E02_hello_python.py b/exercises/1901100053/1001S02E02_hello_python.py index afcffc0d6..cfa8d7507 100644 --- a/exercises/1901100053/1001S02E02_hello_python.py +++ b/exercises/1901100053/1001S02E02_hello_python.py @@ -1 +1 @@ -print("hello world"£© \ No newline at end of file +print("hello world"!) \ No newline at end of file diff --git a/exercises/1901100053/1001S02E03_ca1cu1ator.py b/exercises/1901100053/1001S02E03_ca1cu1ator.py new file mode 100644 index 000000000..69eece636 --- /dev/null +++ b/exercises/1901100053/1001S02E03_ca1cu1ator.py @@ -0,0 +1,21 @@ +operator = input('请输入è¿ç®—符(+ã€-ã€*ã€/):') +first_number = input('è¯·è¾“å…¥ç¬¬ä¸€ä½æ•°å­—:') +second_number = input('è¯·è¾“å…¥ç¬¬äºŒä½æ•°å­—:') +a = int(first_number) +b = int(second_number) +print('operator:', operator, type(operator)) +print('first_number:',first_number, type(first_number),type(a)) +print('second_number:',second_number,type(second_number),type(b)) +print('测试加法 str 加法:', first_number + second_number) +# print('测试加法 str 凿³•:', first_number - second_number) +if operator == '+': + print(a, '+',b,'=',a+b) +elif operator == '-': + print(a,'-',b,'=',a-b) +elif operator == '*': + print(a,'*',b,'=',a*b) +elif operator == '/': + print(a,'/',b,'=',a/b) +else: + print('无效的è¿ç®—符') + # raise valueError('无效的è¿ç®—符') diff --git a/exercises/1901100053/1001S02E04_control_flow.py b/exercises/1901100053/1001S02E04_control_flow.py new file mode 100644 index 000000000..2ddd83901 --- /dev/null +++ b/exercises/1901100053/1001S02E04_control_flow.py @@ -0,0 +1,19 @@ + +print('打å°ä¹ä¹ä¹˜æ³•表') +for i in range(1,10): + print('第%d行' %i,end='\t') + for j in range(1, i+1): + # print(i,'*',j,'=',i*j,end='\t') + print('{}*{}={}'.format(i,j,i*j),end='\t') + print() + + # print('\n打å°è·³è¿‡å¶æ•°è¡Œçš„ä¹ä¹ä¹˜æ³•表') +i = 1 +while i<10: + if i % 2 == 0: + print() + else: + for j in range(1,i+1): + print(i,'*',j,'=',i*j,end='\t') + # print('{}*{}={}'.format(i,j,i*j),end='\t') + i += 1 \ No newline at end of file diff --git a/exercises/1901100053/day5_array.py b/exercises/1901100053/day5_array.py new file mode 100644 index 000000000..68b5fbc99 --- /dev/null +++ b/exercises/1901100053/day5_array.py @@ -0,0 +1,21 @@ +# 列表也å«åšæ•°ç»„ +# 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] +print('列表翻转==>', 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] +print('字符串翻转==>',reversed_str) +# 5. 转æ¢ä¸º int 类型 +int_value = int(reversed_str) +print('转æ¢ä¸º int 类型==>', int_value) +print('转æ¢ä¸º 二进制==>', bin(int_value)) +print('转æ¢ä¸º 八进制==>', oct(int_value)) +print('转æ¢ä¸º å六进制==>', hex(int_value)) \ No newline at end of file diff --git a/exercises/1901100053/day5_stats_text.py b/exercises/1901100053/day5_stats_text.py new file mode 100644 index 000000000..faedee55e --- /dev/null +++ b/exercises/1901100053/day5_stats_text.py @@ -0,0 +1,64 @@ +sample_text = ''' +The Zen Of Python ,by Tim peetrs + + +Beautiful is beter than ugly. +Explicit is better than complex. +Simple is better than complicated. +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 silenced. +Unless explcitly 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 bsd idea. +If the implementation si easy to explain,it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + +# 1. 使用字典(dict 类型)统计字符串样本 text 中å„个英文å•è¯å‡ºçŽ°çš„æ¬¡æ•° + +# 先进将字符串根æ®ç©ºç™½å­—ç¬¦åˆ†å‰²æˆ list , è¦è°ƒç”¨ str 类型 +elements = sample_text.split() + +# 定义一个新的 list åž‹å˜é‡,存储处ç†è¿‡çš„å•è¯ +words = [] + +# 先针对样本挑选需è¦å‰”除的éžå•è¯ç¬¦å· +symbols = ',.*-!' + +for element in elements: + # é历一éè¦å‰”é™¤çš„ç¬¦å· + for symbol in symbols: + # é€ä¸ªæ›¿æ¢å­—符å·, 用 '' æ˜¯ä¸ºäº†åŒæ—¶å‰”é™¤ç¬¦å·æ‰€å çš„ä½ç½® + element= element.replace(symbol, '') + # å‰”é™¤äº†å­—ç¬¦åŽ å¦‚æžœ element çš„ 长度ä¸ä¸º 0 则算作正常å•è¯ + if len(element): + words.append(element) + +print('正常的英文å•è¯==>',words) + +# åˆå§‹åŒ–一个 dict (å­—å…¸)类型的å˜é‡, 用æ¥å­˜æ”¾å•è¯å‡ºçŽ°çš„æ¬¡æ•° +counter = {} + +# set (集åˆ) 类型 å¯ä»¥åŽ»æŽ‰ 列表里的é‡å¤å…ƒç´ ,å¯ä»¥åœ¨ for...in 里å‡å°‘循环次数 +word_set = set(words) + +for word in word_set: + counter[word]=words.count[word] + +print('英文å•è¯å‡ºçŽ°çš„æ¬¡æ•°==>', counter) + +# 2. 按照出现次数从大到å°è¾“出所有的å•è¯åŠå‡ºçŽ°çš„æ¬¡æ•° + +# 内置函数 sorted çš„å‚æ•° key è¡¨ç¤ºæŒ‰å…ƒç´ çš„é‚£ä¸€é¡¹çš„å€¼è¿›è¡ŒæŽ’åº +# dict 类型 counter çš„ items 方法会返回一个包å«ç›¸åº”项(key .value) 的元组 列表 +# print('counter.items()==>', counter.itemse()) +print('从大到å°è¾“出所有的å•è¯åŠå‡ºçŽ°çš„æ¬¡æ•°==>', sorted(counter.items(), key = lambda x: x[1], reverse = true)) \ No newline at end of file diff --git a/exercises/1901100053/day5_string.py b/exercises/1901100053/day5_string.py new file mode 100644 index 000000000..dafdd7430 --- /dev/null +++ b/exercises/1901100053/day5_string.py @@ -0,0 +1,52 @@ +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.将字符串样本里的 better å…¨éƒ¨æ›¿æ¢æˆ worse + +# 调用 str 类型 çš„ replace æ–¹æ³•è¿›è¡Œæ›¿æ¢ +text = sample_text.replace('better','worse') +print('将字符串样本里的 better å…¨éƒ¨æ›¿æ¢æˆ worse ==>',text) + +# 2. å°†å•è¯ä¸­åŒ…å« ea çš„å•è¯å‰”除 + +# 先将字符串根æ®ç©ºç™½å­—ç¬¦åˆ†å‰²æˆ list ,è¦è°ƒç”¨ str 类型 +words = text.split() +# 定义一个 list 类型的å˜é‡ç”¨æ¥å­˜æ”¾è¿‡æ»¤å®Œçš„å•è¯ +filtered = [] +# 用 for...in 循环é历一é words 里的元素然åŽåˆ¤æ–­å•è¯æ˜¯å¦åŒ…å« ea +for word in words: + # str 类型的 find 方法 如果ä¸åŒ…å« å‚æ•° 字符则返回 -1 ,如果包å«åˆ™è¿”回该字符第一次出现时的索引 + if word.find('ea') < 0: + filtered.append(word) +print('å°†å•è¯ä¸­åŒ…å« ea çš„å•è¯å‰”除==>', filtered) + +# 3. 进行大å°å†™ç¿»è½¬ +# åˆ©ç”¨åˆ—è¡¨æŽ¨å€’å¼ å¯¹ str 类型的元素进行大å°å†™ç¿»è½¬ +swapcased = [i.swapcase() for i in filtered ] +print('进行大å°å†™ç¿»è½¬ ==>', swapcased) + +# 4.å•è¯æŒ‰ a_z å‡åºæŽ’列 +# print('å•è¯æŒ‰ a_z å‡åºæŽ’列==>', sorted(swapcased)) +print('å•è¯æŒ‰ a_z é™åºæŽ’列==>', sorted(swapcased, reverse=True)) \ No newline at end of file diff --git a/exercises/1901100053/day6_stats_word.py b/exercises/1901100053/day6_stats_word.py new file mode 100644 index 000000000..af91c6237 --- /dev/null +++ b/exercises/1901100053/day6_stats_word.py @@ -0,0 +1,93 @@ +# ç»Ÿè®¡å‚æ•°ä¸­æ¯ä¸ªè‹±æ–‡å•è¯å‡ºçŽ°çš„æ¬¡æ•° +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): + words.append(element) + + + counter = {} + word_set = set(words) + + for word in word_set: + counter[word] = words.count(word) + # 函数返回值用 return 进行返回, 如果没有 return 返回值则为 None + return sorted(counter.items(), key=lambda x: x[1], reverse=True) + + + + +# ç»Ÿè®¡å‚æ•°ä¸­æ¯ä¸ªä¸­æ–‡æ±‰å­—出现的次数 +def stats_text_cn(text): + cn_characters = [] + for character in text: + # unicode 中 中文字符的范围 + if'\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + counter = {} + cn_character_set = set(cn_characters) + for character in cn_character_set: + counter[character] = cn_characters.count(character) + return sorted(counter.items(), key = lambda x: x[1], reverse = True) + + +en_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 nesed . +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 explicotly 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 ! +''' + +cn_text=''' +《蟒蛇之禅》(the zen of python),蒂姆·彼得斯(tim peters)è‘— +美丽总比丑陋好。 +æ˜¾å¼æ¯”éšå¼å¥½ã€‚ +ç®€å•æ€»æ¯”夿‚好。 +夿‚æ€»æ¯”å¤æ‚好。 +平的比嵌套的好。 +ç¨€ç–æ€»æ¯”密集好。 +å¯è¯»æ€§ã€‚ +特殊情况还ä¸è¶³ä»¥æ‰“破规则。 +è™½ç„¶å®žç”¨æ€§èƒœè¿‡çº¯æ´æ€§ã€‚ +错误ä¸åº”该悄无声æ¯åœ°è¿‡åŽ»ã€‚ +é™¤éžæ˜Žç¡®ç¦æ­¢ã€‚ +é¢å¯¹æ•é”,拒ç»çŒœæµ‹çš„诱惑。 +应该有一ç§â€”â€”è€Œä¸”æœ€å¥½åªæœ‰ä¸€ç§â€”—显而易è§çš„æ–¹æ³•æ¥åšåˆ°è¿™ä¸€ç‚¹ã€‚ +ä¸è¿‡ï¼Œé™¤éžä½ æ˜¯è·å…°äººï¼Œå¦åˆ™è¿™ç§æ–¹å¼ä¸€å¼€å§‹å¯èƒ½å¹¶ä¸æ˜Žæ˜¾ã€‚ +现在总比ä¸åšå¥½ã€‚ +è™½ç„¶ä»Žæ¥æ²¡æœ‰æ¯”â€œçŽ°åœ¨â€æ›´å¥½ã€‚ +如果实现很难解释,这是一个å主æ„。 +如果实现易于解释,å¯èƒ½æ˜¯ä¸ªå¥½ä¸»æ„。 +å称空间是一个很棒的主æ„â€”â€”è®©æˆ‘ä»¬åšæ›´å¤šè¿™æ ·çš„事情! +''' + +# æœç´¢ _name_==_main_ +# 一般情况下在文件内测试 代ç çš„æ—¶å€™ä»¥ä¸‹é¢çš„å½¢å¼è¿›è¡Œ +if __name__ == '__main__': + en_result = stats_text_en(en_text) + cn_result = stats_text_cn(cn_text) + print('ç»Ÿè®¡å‚æ•°ä¸­æ¯ä¸ªè‹±æ–‡å•è¯å‡ºçŽ°çš„æ¬¡æ•°==>\n', en_result) + print('ç»Ÿè®¡å‚æ•°ä¸­æ¯ä¸ªä¸­æ–‡æ±‰å­—出现的次数==>\n', cn_result)