Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions exercises/1901100240/1001S02E04_control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
i += 1
93 changes: 91 additions & 2 deletions exercises/1901100240/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,91 @@
# hello-world
The project that usually used as the first repository
# 课程总结

# Github
除编译器之外的重要协作软件Github,其功能类似于编程版本的Google Doc,可以允许多人同时编程。
对每段更改保有历史记录,可回溯任意更改,避免代码被覆盖,且可回溯任意版本。
Github建立在分支架构之上,主分支负责程序主体,dev分支则可供个人修改使用。个人修改的程序可通过pull request请求并入主分支。此功能保证多人同时协作的同时,亦可以避免程序被个人错误更改。
通过folk加入其他repository,通过clone下载到本地,通过commit讲更改提交到主分支,通过pull request将修改提交。

# Python 编译器
Anaconda-Navigator 综合了数个python编译器。
Visual Studio Code适合快速编译python shell内容,界面视觉效果较好。Spyder适合做短代码测试,界面一般。
Visual Studio Cose 和 Spyder 保存的代码可以自动同步,方便转换。
在python shell中需要print将数值显示,否则默认隐藏。

# Hello Word
约定俗称的语言入门所编写的第一个程序。
Print作为python输出,在python shell内不print的内容默认隐藏。
print时以'/t'控制同行空格打印。
print时以''换新的一行。

# 结构类型
有字典(dictionary),元组(tuple),列表(list)等储存单位。
元组和列表是以位置储存元素,位置0对应第一元素,位置1对应第二元素,构成元素和位置一一对应的关系。
字典是以key和item进行一一对应。
元组内部数据无法更改。
字典用于统计数字较为方便。

# 循环
for loop 和 while loop是最常用的两种循环模式。
for loop不仅可以进行数字循环,也可以进行列表中元素的循环。
while loop可以通过 while True 不断循环直到达成条件break。
break会跳出最内层的循环。
当过多循环减慢速度时,可采用numpy替代。

# try...except
阻止程序进入error,可保证代码继续运行。
可暂时忽略无效代码。
配合try...except然后print,可进行debuging。

# Debug
bug可以双轴分为四象限:横轴为显性,隐性;纵轴为持续性,间歇性。
隐性bug比显性bug危险,间歇性bug比持续性危险。
显性bug程序会直接报错,易于发现;隐性bug程序不会报错,但输出结果与预期不符,更难发现。
持续性bug每次运行都会出现,间歇性只会在特殊情况下出现。
千年虫既为典型隐性间歇性bug。
Debug过程,需要讲代码分为不同模块,检测不同模块结果,发现模块结果错误既修改之。
修改单一模块后,仍需整体运行以保证输出正确。

# 排序
计算机编程的重要组成部分。
不同的排序方式会产生巨大的速度差别。

# 模块
将已经编写好的代码作为模块保存,可以在其他程序中进行调用。
调用所需时间较长。
可使得程序简洁明了。

# 画图
画图逻辑与matlab相似。
需要导入matplotlib包函数。
处理速度相对较慢。
matplotlib包本身不支持中文,故需要通过一下代码导入中文:
lt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

# yagmail
可进行收发邮件,配合try..except使用,可保证在程序出错时迅速收到提醒。
邮件发送前邮箱需要授权Pop/SMTP。
授权后以授权码代替密码。
getpass默认隐藏密码,输入不可见。
yagmail.SMTP()中host需要输入,否则出现超时错误。

# getpass
可用于获取密码,其特点为密码会呈现不可见状态,以保护密码安全。

# wxpy
微信外链程序,可编写自动回复脚本。
基于webweixin进行,有可能被微信web封号。

# jieba
中文分词包,其共嫩建立在内含的词典之上。
可通过jieba进行词频统计。
jieba输出为不可见的list形式。

# 自学相关
凡是知道方向,大部分问题都可通过搜索引擎解决。
寻找方向是自学的关键。
自定一个Project,以解决问题的形式进行学习,倒逼学习相关知识。
自学可能产生回音室效应,产生自循环,也就是倾向于用已知知识解决新问题,因此需要时时与他人对比。
部分问题已经有人做出更好解答,不可迷恋自身能力。
相对于系统化学习,可能漏过部分基础,使得无知于某些高效解决方案。
15 changes: 15 additions & 0 deletions exercises/1901100240/d09/mymodule/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))

20 changes: 20 additions & 0 deletions exercises/1901100240/d10/mymodule/main.py
Original file line number Diff line number Diff line change
@@ -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))
48 changes: 48 additions & 0 deletions exercises/1901100240/d10/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -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")
28 changes: 28 additions & 0 deletions exercises/1901100240/d11/mymodule/main.py
Original file line number Diff line number Diff line change
@@ -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)
48 changes: 48 additions & 0 deletions exercises/1901100240/d11/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -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")
26 changes: 26 additions & 0 deletions exercises/1901100240/d12/mymodule/main.py
Original file line number Diff line number Diff line change
@@ -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()
48 changes: 48 additions & 0 deletions exercises/1901100240/d12/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -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")
Loading