Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
19e0fdd
Create 1001S02E02_hello_python.py.py
Bear127 Apr 1, 2019
034b8a9
Create 1001S02E03_calculator.py
Bear127 Sep 2, 2019
c867408
Update 1001S02E03_calculator.py
Bear127 Sep 2, 2019
123ecd9
Merge branch 'master' into master
liujiayi0042 Sep 3, 2019
1ee4f59
Create 1001S02E04_control_flow.py
Bear127 Sep 3, 2019
dd3c5e6
Merge branch 'master' of https://github.com/Bear127/selfteaching-pyth…
Bear127 Sep 3, 2019
85fda72
Update 1001S02E04_control_flow.py
Bear127 Sep 6, 2019
1b350aa
Merge branch 'master' into master
liujiayi0042 Sep 8, 2019
96983e5
Create 1001S02E05_array.py
Bear127 Sep 28, 2019
9d7b2c2
Create 1001S02E05_state_text.py
Bear127 Sep 28, 2019
32384e4
Create 1001S02E05_string.py
Bear127 Sep 28, 2019
33b413b
Update 1001S02E05_array.py
Bear127 Oct 1, 2019
99b8417
Update 1001S02E05_state_text.py
Bear127 Oct 1, 2019
f1514e8
Update 1001S02E05_string.py
Bear127 Oct 1, 2019
e18bf27
Create 1001S02E06_stats_word.py
Bear127 Oct 1, 2019
c1eb389
Merge branch 'master' into master
liujiayi0042 Oct 6, 2019
12567d1
Update 1001S02E05_state_text.py
Bear127 Oct 12, 2019
0d2b732
Update 1001S02E06_stats_word.py
Bear127 Oct 12, 2019
21f2f9a
Create main.py.py
Bear127 Oct 12, 2019
df43c88
Create stats_word.py
Bear127 Oct 12, 2019
552a54a
Update main.py.py
Bear127 Oct 12, 2019
07152c6
Update stats_word.py
Bear127 Oct 12, 2019
3e652e2
Update 1001S02E05_array.py
Bear127 Oct 13, 2019
2e478aa
Update 1001S02E05_state_text.py
Bear127 Oct 13, 2019
cf384d4
Update 1001S02E05_string.py
Bear127 Oct 13, 2019
c25c148
Update main.py.py
Bear127 Oct 16, 2019
821e80c
Update stats_word.py
Bear127 Oct 16, 2019
5c997ea
Create main.py.py
Bear127 Oct 17, 2019
67d26eb
Create stats_word.py
Bear127 Oct 17, 2019
c6c5b9d
Create stats_word.py
Bear127 Nov 21, 2019
98b26d2
Create main.py.py
Bear127 Nov 21, 2019
fd911fd
Update main.py.py
Bear127 Nov 21, 2019
c741a4e
Update main.py.py
Bear127 Nov 25, 2019
72fccba
Update stats_word.py
Bear127 Nov 25, 2019
5008da9
Create stats_word.py
Bear127 Nov 25, 2019
afc669d
Create main.py.py
Bear127 Nov 25, 2019
90f6abb
Update main.py.py
Bear127 Dec 2, 2019
5836aac
Create main.py.py
Bear127 Dec 2, 2019
4b706d5
Update stats_word.py
Bear127 Dec 2, 2019
21933f8
Create stats_word.py
Bear127 Dec 2, 2019
23aec56
Update stats_word.py
Bear127 Dec 2, 2019
1754820
Update main.py.py
Bear127 Dec 4, 2019
a74e80a
Create stats_word.py
Bear127 Dec 4, 2019
73d5238
Create main.py.py
Bear127 Dec 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion 1901100103/1001S02E01_helloworld.txt

This file was deleted.

1 change: 0 additions & 1 deletion 1901100103/1001S02E02_hello_python.py

This file was deleted.

1 change: 0 additions & 1 deletion 1901100103/README.md

This file was deleted.

1 change: 0 additions & 1 deletion 1901100304/1001S02E01_helloword.txt

This file was deleted.

1 change: 0 additions & 1 deletion 1901100304/README.md

This file was deleted.

1 change: 0 additions & 1 deletion 1901100373/1001S02E02_hello_python.py

This file was deleted.

58 changes: 33 additions & 25 deletions exercises/1901010072/1001S02E05_array.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# 将数组 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 翻转
a_list =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(iterable)
for i in a:
print(i,reverse=True)
a_list.reverse()
# 翻转后的数组拼接成字符串
s=''
s.join(a)
# ⽤字符串切片的方式取出第三到第⼋个字符(包含第三和第⼋个字符)
print(a_list[3:9])
# 将获得的字符串进行翻转
b=a_list[3:9].reverse()
print(b)
# 将结果转换为 int 类型
c=int(b)
print(c)
# 分别转换成⼆进制,⼋进制,十六进制
d=bin(int(c, 2))
e=oct(int(c, 2))
f=hex(int(c, 2))
# 最后输出三种进制的结果
print(d)
print(e)
print(f)

# 1.对列表 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 翻转
sample_list =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reverse_list=sample_list[::-1]
print('列表翻转==>',reverse_list)

# 2.翻转后的列表拼接成字符串
#调用''(空字符串)str类型的join方法可以连接列表里的元素,用''(空字符串)表示连接的时候元素间不用任何字符隔开
#因为reverse_list里面都是int类型元素,所以拼接之前要把reverse_list变成一个包含str类型元素的列表
joined_str = ''.join([str(i) for i in reverse_list])
print('翻转后的数组接拼成字符串-->',joined_str)

# 3.⽤字符串切片的方式取出第三到第⼋个字符(包含第三和第⼋个字符)
# 切片的时候包含开始索引,不包含结尾的索引
slice_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)

# 6.分别转换成⼆进制,⼋进制,十六进制

print('转换为二进制==>',bin(int_value))
print('转换为八进制==>',oct(int_value))
print('转换为十六进制==>',hex(int_value))


51 changes: 41 additions & 10 deletions exercises/1901010072/1001S02E05_state_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
text = '''

sample_text = '''

The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Expand All @@ -21,12 +23,41 @@
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!
'''
# 使用字典(dict)统计字符串样本text中各个英文单词出现的次数
# 按照出现次数从⼤到⼩输出所有的单词及出现的次数
# 只统计英文单词,不包括⾮、非英⽂字符的其他任何符号,如连接符号、空白字符等
s=text
s.strip()
for k in s:
v = str.count(k)
dict=dict_items([k,v])
sorted(dict,reverse=true)

# 1.使用字典(dict)统计字符串样本text中各个英文单词出现的次数
# 先将字符串根据 空白字符 分割成list,在调用str类型
elements = sample_text.split()
# 定义一个新的list类型变量,存储处理过的单词
words=[]
#先针对样本文本挑选出需要提出的非单词符号
symbols=',.*-!'
for element elements:
#遍历一遍要剔除的符号
for symbol in symbols:
#逐个替换字符号,用''是为了同时剔除符号所占的位置
element=elements.replace(symbol,'')
#剔除了字符后如果element的长度不为0,则算作正常单词
if len(element):
words.append(element)
print('正常的英文单词==>',words)

#初始化一个dict(字典)类型的数量,用来存放单词出现的次数
count={}

#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.items())

print('从大到小输出所有的单词及出现的次数==>',sorted(counter.items(),key=lamda x:x[1],reverse=True))


36 changes: 22 additions & 14 deletions exercises/1901010072/1001S02E05_string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
text = '''

sample_text = '''

The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Expand All @@ -22,22 +24,28 @@
Namespaces are one honking great idea -- let's do more of those!
'''
#第一步将字符串样本text里的better全部替换成worse
s=text
s.replace('better', 'worse', )
print(s.replace('better', 'worse', ))

#调用str类型的replace方法进行替换
text=sample_text.replace('better','worse')
print('将字符串样本里的better全部替换成worse ==>', text)

#第二步将单词中包含ea的单词剔除
s.split()
L = list(s)
for C in L:
if 'ea' in 'C',
del L[C]
print(L_list)
else:
print(L_list)
#先将字符串根据 空白字符 分割成 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)

#第三步将字⺟进行⼤小写翻转
s.swapcase()
#利用列表推导式对str类型的元素进行大小写翻转
swapcasd=[i.swapcase()for i in filtered]
print('进行大小写翻转-->',swapcased)

#第四步将单词升序排列
L_list.sort()
print('单词按a...z升序排列-->',sorted(swapcased))
#print('单词按a...z降序排列-->',sorted(swapcased,reverse=true))
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
en_text = '''
# 统计参数中每个英文单词出现的次数
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.
Expand All @@ -12,54 +44,16 @@
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.
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.
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()
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 sorted(counter.items(), key=lambda x: x[1], reverse=True)




def stats_text_cn(text):
cn_characters = []
for character in text:
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)



cn_text = '''
cn_text='''
Python之禅 by Tim Peters

优美胜于丑陋
明了胜于晦涩
简洁胜于复杂
Expand All @@ -73,11 +67,14 @@ def stats_text_cn(text):
而是尽量找一种,最好是唯一一种明显的解决方案
虽然这并不容易,因为你不是 Python 之父
做也许好过不做,但不假思索就动手还不如不做
。。。
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然
命名空间是一种绝妙的理念,我们应当多加利用。。。
'''

if __name__ == '__main__':
en_result = stats_text_en(en_text)
cn_result = stats_text_en(cn_text)
print('统计参数中每个英语单词出现的次数 ==>\n',en_result)
print('统计参数中每个中文汉字出现的次数 ==>\n',cn_result)
#搜索__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)
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
from mymodule import stats_word

new_text = '''
sample_text = '''
愚公移⼭
太行,王屋⼆山的北面,住了一個九十歲的⽼翁,名叫愚公。⼆山佔地廣闊,擋住去路,使他和家⼈往來來極為不便。
⼀天,愚公召集家人說:「讓我們各盡其力,剷平⼆山,開條道路,直通豫州,你們認為怎樣?」
⼤家都異口同聲贊成,只有他的妻子表示懷疑,並說:「你連開鑿一個⼩丘的力量量都沒有,怎可能剷平太行、王屋⼆山呢?況且,鑿出的⼟石⼜丟到哪裏去呢?」
⼤家都熱烈烈地說:「把⼟石丟進渤海裏。」
於是愚公就和兒孫,⼀一起開挖土,把⼟⽯搬運到渤海去。
愚公的鄰居是個寡婦,有個兒子⼋歲也興致勃勃地走來幫忙。
寒來暑往,他們要⼀年才能往返渤海一次。
住⿈河畔的智叟,看見他們這樣辛苦,取笑愚公說:「你不是很愚蠢嗎?你已一把年紀了,就是用盡你的氣力,也不能挖去山的一角呢?」
愚公歎息道:「你有這樣的成見,是不會明⽩的。你比那寡婦的小兒⼦子還不不如呢!就算我死了,還有我的兒⼦,我的孫子,我的曾孫子,他們一直傳下去。⽽這⼆山是不會加大的,總有
一天,我們會把它們剷平。」
智叟聽了,無話可說:
⼆山的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位⼤力神揹⾛二⼭。
太行,王屋⼆山的北面,住了一個九十歲的⽼翁,名叫愚公。⼆山佔地廣闊,擋住去路路,使他
和家⼈往來極為不便。
⼀天,愚公召集家⼈人說:「讓我們各盡其力,剷平⼆山,開條道路,直通豫州,你們認為怎
樣?」
⼤家都異異⼝同聲贊成,只有他的妻子表示懷疑,並說:「你連開鑿一個⼩丘的⼒量都沒有,怎
可能剷平太行、王屋⼆山呢?況且,鑿出的⼟石⼜丟到哪裏去呢?」
⼤家都熱烈地說:「把⼟石丟進渤海海裏。」
於是愚公就和兒孫,⼀起開挖土,把⼟石搬運到渤海去。
愚公的鄰居是個寡婦,有個兒⼦八歲也興致勃地⾛來幫忙。
寒來暑往,他們要一年才能往返渤海一次。
住在⿈河畔的智叟,看見他們這樣辛苦,取笑愚公說:「你不是很愚蠢嗎?你已一把年紀
了,就是⽤盡你的氣力,也不能挖去山的一角呢?」
愚公歎息道:「你有這樣的成見,是不會明白的。你⽐那寡婦的⼩兒子還不如呢!就算我死
了,還有我的兒子,我的孫⼦,我的曾孫⼦,他們一直傳下去。⽽這⼆山是不會加大的,總有
⼀天,我們會把它們剷平。」
智叟聽了,無話可說:
⼆山的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位⼤
力神揹走⼆山。

How The Foolish Old Man Moved Mountains
Yugong was a ninety-year-old man who lived at the north of two high mountains, Mount Taixing and Mount Wangwu.
Stretching over a wide expanse of land, the mountains blocked yugong’s way making it inconvenient for him and his family to get
around.
One day yugong gathered his family together and said,”Let’s do our best to level these two mountains. We shall open a road that leads
to Yuzhou. What do you think?”
Stretching over a wide expanse of land, the mountains blocked yugong’s way making it inconvenient for him and his family to get around.
One day yugong gathered his family together and said,”Let’s do our best to level these two mountains. We shall open a road that leads to Yuzhou. What do you think?”
All but his wife agreed with him.
“You don’t have the strength to cut even a small mound,” muttered his wife. “How on earth do you suppose you can level Mount Taixin
and Mount Wanwu? Moreover, where will all the earth and rubble go?”
“Dump them into the Sea of Bohai!” said everyone.
So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea
of Bohai.
So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea of Bohai.
Now Yugong’s neighbour was a widow who had an only child eight years old. Evening the young boy offered his help eagerly.
Summer went by and winter came. It took Yugong and his crew a full year to travel back and forth once.
On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed
Yugong saying,”Aren’t you foolish, my friend? You are very old now, and with whatever remains of your waning strength, you won’t be able
to remove even a corner of the mountain.”
On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,”Aren’t you foolish, my friend? You are very old now,
and with whatever remains of your waning strength, you won’t be able to remove even a corner of the mountain.”
Yugong uttered a sigh and said,”A biased person like you will never understand. You can’t even compare with the widow’s little boy!”
“Even if I were dead, there will still be my children, my grandchildren, my great grandchildren, my great great grandchildren.
They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!” he declared with
confidence.
They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!” he declared with confidence.
The wise old man was totally silenced.
When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the
incident to the Emperor of Heavens.
When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the incident to the Emperor of Heavens.
Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away.
'''

result = stats_word.stats_text(new_text)
result = stats_word.stats_text(sample_text)

print(result)
print('统计结果==>',result)
Loading