diff --git a/exercises/1901100240/1001S02E04_control_flow.py b/exercises/1901100240/1001S02E04_control_flow.py index bd8c6079e..caf9e54a2 100644 --- a/exercises/1901100240/1001S02E04_control_flow.py +++ b/exercises/1901100240/1001S02E04_control_flow.py @@ -5,20 +5,8 @@ for j in range(1,i+1): # The column start from 1 to i print(i,'*',j,'=',i*j,end='\t') # \t to make double blank print('') # Start a new line for each row i - -for i in range(1,10): - for j in range(1,i+1): - - print(i,'*',j,'=',i*j,end='\t') - - print(i,'*',j,'=',i*j,end=' ') - - print('') - - print('') - # Print multiplication table without even numbers by using the while loop print('The Multiplication Table Without Even Number') @@ -30,19 +18,4 @@ print(i,'*',j,'=',i*j,end='\t') # \t to make double blank j += 1 print("") # Start a new line for each odd row - -i=1; -while i<10 : - if i%2!=0: - j=1; - while j<=i: - if j%2!=0: - - print(i,'*',j,'=',i*j,end='\t') - - print(i,'*',j,'=',i*j,end=' ') - - j+=1 - print("") - - i+=1 \ No newline at end of file + i += 1 \ No newline at end of file diff --git a/exercises/1901100240/d09/mymodule/main.py b/exercises/1901100240/d09/mymodule/main.py index 449c17965..4a18407f0 100644 --- a/exercises/1901100240/d09/mymodule/main.py +++ b/exercises/1901100240/d09/mymodule/main.py @@ -3,8 +3,15 @@ import stats_word import json +import os + +# Imput the tang300.json documents and transfer into form of [{dict},{dict},{dict}] +with open('Documents/GitHub/selfteaching-python-camp/exercises/1901100240/d09/mymodule/tang300.json','r+',encoding = "utf-8") as f: + + # Imput the tang300.json documents and transfer into form of [{dict},{dict},{dict}] with open('/Users/yanning/Documents/GitHub/selfteaching-python-camp/exercises/1901100240/d09/mymodule/tang300.json','r+',encoding = "utf-8") as f: + tang_load = json.load(f) # Create a empty string and use it as storage. @@ -13,8 +20,16 @@ for unit in tang_load: # Unit will be each dictionary in the list for char in unit.values(): # Char will be each values of the dictionary in form ['values','values','values','values'] for content in str(char): # content will be each element in the list Char + + if ord(content) > 256 and content not in "!“”#$%&‘’()*+,-。/:;、……<=>?@[][]《》^_`{|}~\n": # Only count the Chinese characters without any symbol + tang_text += str(content) # Store the string into the storage string tang_text + +# Doing the statistic of frequncy of each Chinese characters and make it a list +print(stats_word.stats_text_cn(tang_text,100)) + if ord(content) > 256 and content not in "!“”#$%&‘’()*+,-。/:;、……<=>?@[]《》^_`{|}~\n": # Only count the Chinese characters without any symbol tang_text += str(content) # Store the string into the storage string tang_text # Doing the statistic of frequncy of each Chinese characters and make it a dictionary print(stats_word.stats_text(tang_text,100)) + diff --git a/exercises/1901100240/d10/mymodule/main.py b/exercises/1901100240/d10/mymodule/main.py new file mode 100644 index 000000000..3964d3018 --- /dev/null +++ b/exercises/1901100240/d10/mymodule/main.py @@ -0,0 +1,20 @@ +# This algorithm is to do the statistic of frequency of Chinese characters in tang300 +# Using the user-defined algrorithm 'stats_word' and package 'json' +import stats_word +import json + +# Imput the tang300.json documents and transfer into form of [{dict},{dict},{dict}] +with open('/Users/yanning/Documents/GitHub/selfteaching-python-camp/exercises/1901100240/d10/mymodule/tang300.json','r+',encoding = "utf-8") as f: + tang_load = json.load(f) + +# Create a empty string and use it as storage. +tang_text="" +# Using loop to combine all dict.values() into the storage string +for unit in tang_load: # Unit will be each dictionary in the list + for char in unit.values(): # Char will be each values of the dictionary in form ['values','values','values','values'] + for content in str(char): # content will be each element in the list Char + if ord(content) > 256 and content not in "!“”#$%&‘’()*+,-。/:;、……<=>?@[][]《》^_`{|}~\n": # Only count the Chinese characters without any symbol + tang_text += str(content) # Store the string into the storage string tang_text + +# Doing the statistic of frequncy of each Chinese characters and make it a list +print(stats_word.stats_text_cn(tang_text,20)) \ No newline at end of file diff --git a/exercises/1901100240/d10/mymodule/stats_word.py b/exercises/1901100240/d10/mymodule/stats_word.py new file mode 100644 index 000000000..dd30d6657 --- /dev/null +++ b/exercises/1901100240/d10/mymodule/stats_word.py @@ -0,0 +1,48 @@ +# This algorithm return the statistic result of the text +# It provide the frequency of every single words +from collections import Counter +import jieba + +# Define the function +def stats_text_en(en_text,count): + try: + en_text = "".join(i for i in en_text if 65 <= ord(i) <= 90 or 97 <= ord(i) <= 122).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + # Count the word use Counter + try: + en_count = Counter(en_text).most_common(count) # Create an counter + return en_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_en should be the form of integer. The input now is in ", type(en_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_en should be the form of string. The input now is in ", type(en_text)," type. Please retry and enter a string") + +# This algorithm return the statistic result of the text in Chinese +# It provide the frequency of every Chinese word using algrothm jieba +def stats_text_cn(cn_text,count): + try: + cn_text = "".join(j for j in cn_text if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + # Seprate the words in cn_text + cn_list=jieba.cut(cn_text,cut_all=False) + # Selected the words which length greater than two + cn_two_list=[] + for unit in cn_list: + if len(unit) >= 2: + cn_two_list.append(unit) + try: + cn_count = Counter(cn_two_list).most_common(count) # Create an counter + return cn_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_cn should be the form of integer. The input now is in ", type(cn_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_cn should be the form of string. The input now is in ", type(cn_text)," type. Please retry and enter a string") + +# This function uses the following encoding: utf-8 +# Return a list of strings with English and Chinese words seperatly +def stats_text(string,count): + # Seperate the Chinese and English words into two strings by checking ASCII value + try: + en_text = "".join(i for i in string if ord(i) < 256).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + cn_text = "".join(j for j in string if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + return [stats_text_en(en_text,count),stats_text_cn(cn_text,count)] + except TypeError: + print("Error! The first input of function stats_text should be the form of string. The input now is in ", type(string)," type. Please retry and enter a string") diff --git a/exercises/1901100240/d11/mymodule/main.py b/exercises/1901100240/d11/mymodule/main.py new file mode 100644 index 000000000..36cb7d506 --- /dev/null +++ b/exercises/1901100240/d11/mymodule/main.py @@ -0,0 +1,28 @@ +# Import the 微信文章 from the webside +import requests +response=requests.get('https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA') + +# Transer the 微信文章 to string type +from pyquery import PyQuery +document = PyQuery(response.text) +content = document('#js_content').text() + +# Make the satistic of the frequency of the word and output as a string +import stats_word +stats = stats_word.stats_text_cn(content,100) +string=str('') # Create a string to store the result +for unit in stats: + # Control the spacing by adding two more space it the word is less than two + if len(unit[0]) <= 2: + string += "'" + unit[0] + "'的频率是: " + str(unit[1]) + "\n" + elif len(unit[0]) == 3: + string += "'" + unit[0] + "'的频率是: " + str(unit[1]) + "\n" + +# Send the result to the email +import yagmail +import getpass +sender = input('输⼊入发件⼈人邮箱:') +password = getpass.getpass('输⼊入发件⼈人邮箱密码(可复制粘贴):') # The password typed will be invisible but will still work +recipients = input('输⼊入收件⼈人邮箱:') +yag=yagmail.SMTP(sender,password,host='smtp.sina.com') +yag.send(to=recipients,subject='自学训练营学习19群+Ningziyun',contents=string) \ No newline at end of file diff --git a/exercises/1901100240/d11/mymodule/stats_word.py b/exercises/1901100240/d11/mymodule/stats_word.py new file mode 100644 index 000000000..dd30d6657 --- /dev/null +++ b/exercises/1901100240/d11/mymodule/stats_word.py @@ -0,0 +1,48 @@ +# This algorithm return the statistic result of the text +# It provide the frequency of every single words +from collections import Counter +import jieba + +# Define the function +def stats_text_en(en_text,count): + try: + en_text = "".join(i for i in en_text if 65 <= ord(i) <= 90 or 97 <= ord(i) <= 122).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + # Count the word use Counter + try: + en_count = Counter(en_text).most_common(count) # Create an counter + return en_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_en should be the form of integer. The input now is in ", type(en_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_en should be the form of string. The input now is in ", type(en_text)," type. Please retry and enter a string") + +# This algorithm return the statistic result of the text in Chinese +# It provide the frequency of every Chinese word using algrothm jieba +def stats_text_cn(cn_text,count): + try: + cn_text = "".join(j for j in cn_text if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + # Seprate the words in cn_text + cn_list=jieba.cut(cn_text,cut_all=False) + # Selected the words which length greater than two + cn_two_list=[] + for unit in cn_list: + if len(unit) >= 2: + cn_two_list.append(unit) + try: + cn_count = Counter(cn_two_list).most_common(count) # Create an counter + return cn_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_cn should be the form of integer. The input now is in ", type(cn_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_cn should be the form of string. The input now is in ", type(cn_text)," type. Please retry and enter a string") + +# This function uses the following encoding: utf-8 +# Return a list of strings with English and Chinese words seperatly +def stats_text(string,count): + # Seperate the Chinese and English words into two strings by checking ASCII value + try: + en_text = "".join(i for i in string if ord(i) < 256).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + cn_text = "".join(j for j in string if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + return [stats_text_en(en_text,count),stats_text_cn(cn_text,count)] + except TypeError: + print("Error! The first input of function stats_text should be the form of string. The input now is in ", type(string)," type. Please retry and enter a string") diff --git a/exercises/1901100240/d12/mymodule/main.py b/exercises/1901100240/d12/mymodule/main.py new file mode 100644 index 000000000..88c0d34cc --- /dev/null +++ b/exercises/1901100240/d12/mymodule/main.py @@ -0,0 +1,26 @@ +# Using wxpy to do the automatic reaply for sharing type +from wxpy import * +bot = Bot() +Chats=bot.friends() +# Doing the statistic for SHARING type +@bot.register(Chats,SHARING) +def print_statis(msg): + # Import the 微信文章 from the chat + import requests + response=requests.get(msg.url) + # Transer the 微信文章 to string type + from pyquery import PyQuery + document = PyQuery(response.text) + content = document('#js_content').text() + # Make the satistic of the frequency of the word and output as a string + import stats_word + stats = stats_word.stats_text_cn(content,100) + string=str('') # Create a string to store the result + for unit in stats: + # Control the spacing by adding two more space it the word is less than two + if len(unit[0]) <= 2: + string += "'" + unit[0] + "'的频率是: " + str(unit[1]) + "\n" + elif len(unit[0]) == 3: + string += "'" + unit[0] + "'的频率是: " + str(unit[1]) + "\n" + msg.reply(string) +embed() \ No newline at end of file diff --git a/exercises/1901100240/d12/mymodule/stats_word.py b/exercises/1901100240/d12/mymodule/stats_word.py new file mode 100644 index 000000000..dd30d6657 --- /dev/null +++ b/exercises/1901100240/d12/mymodule/stats_word.py @@ -0,0 +1,48 @@ +# This algorithm return the statistic result of the text +# It provide the frequency of every single words +from collections import Counter +import jieba + +# Define the function +def stats_text_en(en_text,count): + try: + en_text = "".join(i for i in en_text if 65 <= ord(i) <= 90 or 97 <= ord(i) <= 122).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + # Count the word use Counter + try: + en_count = Counter(en_text).most_common(count) # Create an counter + return en_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_en should be the form of integer. The input now is in ", type(en_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_en should be the form of string. The input now is in ", type(en_text)," type. Please retry and enter a string") + +# This algorithm return the statistic result of the text in Chinese +# It provide the frequency of every Chinese word using algrothm jieba +def stats_text_cn(cn_text,count): + try: + cn_text = "".join(j for j in cn_text if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + # Seprate the words in cn_text + cn_list=jieba.cut(cn_text,cut_all=False) + # Selected the words which length greater than two + cn_two_list=[] + for unit in cn_list: + if len(unit) >= 2: + cn_two_list.append(unit) + try: + cn_count = Counter(cn_two_list).most_common(count) # Create an counter + return cn_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_cn should be the form of integer. The input now is in ", type(cn_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_cn should be the form of string. The input now is in ", type(cn_text)," type. Please retry and enter a string") + +# This function uses the following encoding: utf-8 +# Return a list of strings with English and Chinese words seperatly +def stats_text(string,count): + # Seperate the Chinese and English words into two strings by checking ASCII value + try: + en_text = "".join(i for i in string if ord(i) < 256).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + cn_text = "".join(j for j in string if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + return [stats_text_en(en_text,count),stats_text_cn(cn_text,count)] + except TypeError: + print("Error! The first input of function stats_text should be the form of string. The input now is in ", type(string)," type. Please retry and enter a string") diff --git a/exercises/1901100240/d13/mymodule/main.py b/exercises/1901100240/d13/mymodule/main.py new file mode 100644 index 000000000..49cb9ddeb --- /dev/null +++ b/exercises/1901100240/d13/mymodule/main.py @@ -0,0 +1,37 @@ +# Using wxpy to do the automatic reaply for sharing type +from wxpy import * +bot = Bot() +Chats=bot.friends() +# Doing the statistic for SHARING type +@bot.register(Chats,SHARING) +def print_statis(msg): + # Import the 微信文章 from the chat + import requests + response=requests.get(msg.url) + # Transer the 微信文章 to string type + from pyquery import PyQuery + document = PyQuery(response.text) + content = document('#js_content').text() + # Make the satistic of the frequency of the word and output as a string + import stats_word + stats = stats_word.stats_text_cn(content,10) + # Value is used to store frequency, index is used to store words + value = [] + index = [] + for unit in stats: + # Control the spacing by adding two more space it the word is less than two + value.append(unit[1]) + index.append(unit[0]) + # Create the plot + import matplotlib.pyplot as plt + scale = range(10) + plt.rcParams['font.family'] = ['sans-serif'] + plt.rcParams['font.sans-serif'] = ['SimHei'] + plt.title("微信公众号文章词频统计") + plt.xlabel("词汇") + plt.ylabel("词频") + plt.bar(scale,value) + plt.xticks(scale,index) + plt.savefig("stats.png") # Save the image + msg.reply_image("stats.png") # Send the image as a result +embed() \ No newline at end of file diff --git a/exercises/1901100240/d13/mymodule/stats_word.py b/exercises/1901100240/d13/mymodule/stats_word.py new file mode 100644 index 000000000..dd30d6657 --- /dev/null +++ b/exercises/1901100240/d13/mymodule/stats_word.py @@ -0,0 +1,48 @@ +# This algorithm return the statistic result of the text +# It provide the frequency of every single words +from collections import Counter +import jieba + +# Define the function +def stats_text_en(en_text,count): + try: + en_text = "".join(i for i in en_text if 65 <= ord(i) <= 90 or 97 <= ord(i) <= 122).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + # Count the word use Counter + try: + en_count = Counter(en_text).most_common(count) # Create an counter + return en_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_en should be the form of integer. The input now is in ", type(en_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_en should be the form of string. The input now is in ", type(en_text)," type. Please retry and enter a string") + +# This algorithm return the statistic result of the text in Chinese +# It provide the frequency of every Chinese word using algrothm jieba +def stats_text_cn(cn_text,count): + try: + cn_text = "".join(j for j in cn_text if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + # Seprate the words in cn_text + cn_list=jieba.cut(cn_text,cut_all=False) + # Selected the words which length greater than two + cn_two_list=[] + for unit in cn_list: + if len(unit) >= 2: + cn_two_list.append(unit) + try: + cn_count = Counter(cn_two_list).most_common(count) # Create an counter + return cn_count # Return the counter as result + except TypeError: + print("Error! The second input of function stats_text_cn should be the form of integer. The input now is in ", type(cn_text)," type. Please retry and enter a integer") + except TypeError: + print("Error! The first input of function stats_text_cn should be the form of string. The input now is in ", type(cn_text)," type. Please retry and enter a string") + +# This function uses the following encoding: utf-8 +# Return a list of strings with English and Chinese words seperatly +def stats_text(string,count): + # Seperate the Chinese and English words into two strings by checking ASCII value + try: + en_text = "".join(i for i in string if ord(i) < 256).replace("\n", " ") # ord() returns ASCII or Unicode value, less than 256 will provide the English words + cn_text = "".join(j for j in string if ord(j) > 256) # ord() returns ASCII or Unicode value, greater than 256 will provide the Chinese Character + return [stats_text_en(en_text,count),stats_text_cn(cn_text,count)] + except TypeError: + print("Error! The first input of function stats_text should be the form of string. The input now is in ", type(string)," type. Please retry and enter a string")