Skip to content

Commit 94ae9b8

Browse files
committed
Merge pull request Show-Me-the-Code#121 from Silocean/master
Python练习题
2 parents b5392f5 + 6fe0cd3 commit 94ae9b8

26 files changed

+318
-0
lines changed

Silocean/0000/DejaVuSansMono.ttf

314 KB
Binary file not shown.

Silocean/0000/Test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Jun 5 13:40:53 2015
4+
5+
@author: Tracy
6+
"""
7+
8+
from PIL import Image
9+
from PIL import ImageDraw
10+
from PIL import ImageFont
11+
12+
img = Image.open('icon.jpg')
13+
draw = ImageDraw.Draw(img)
14+
font = ImageFont.truetype('DejaVuSansMono.ttf',100)
15+
16+
draw.text((img.size[0]-100,30),"3",(255,0,0), font)
17+
18+
img.save('result.jpg')

Silocean/0004/Test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
__author__ = 'Tracy'
2+
3+
import io, re
4+
5+
count = 0
6+
7+
with io.open('text.txt', 'r') as file:
8+
for line in file.readlines():
9+
list = re.findall("[a-zA-Z]+'*-*[a-zA-Z]*", line)
10+
count += len(list)
11+
print(count)
12+

Silocean/0004/text.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Dursleys had everything they wanted, but they also had a secret, and their greatest fear was that somebody would discover it. They didn't think they could bear it if anyone found out about the Potters. Mrs. Potter was Mrs. Dursley's sister, but they hadn't met for several years; in fact, Mrs. Dursley pretended she didn't have a sister, because her sister and her good-for-nothing husband were as unDursleyish as it was possible to be. The Dursleys shuddered to think what the neighbors would say if the Potters arrived in the street. The Dursleys knew that the Potters had a small son, too, but they had never even seen him. This boy was another good reason for keeping the Potters away; they didn't want Dudley mixing with a child like that.

Silocean/0007/Test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
__author__ = 'Tracy'
2+
3+
import os, io, re
4+
5+
commentLines = 0
6+
whiteLines = 0
7+
comment = False
8+
9+
path = 'F:\AllKindsOfWorkplace\PyCharmWorkplace\PythonLearning'
10+
11+
count = 0
12+
def tree(path):
13+
filelist = os.listdir(path)
14+
for file in filelist:
15+
if os.path.isdir(os.path.join(path, file)):
16+
tree(os.path.join(path, file))
17+
else:
18+
filename = os.path.basename(os.path.join(path, file))
19+
if filename.endswith(".py"):
20+
# print(filename)
21+
file = io.open(os.path.join(path, file))
22+
parse(file)
23+
file.close()
24+
25+
def parse(file):
26+
global commentLines
27+
global whiteLines
28+
global comment
29+
for line in file.readlines():
30+
# line = line.strip("\n")
31+
if line.startswith("#"):
32+
commentLines += 1
33+
elif re.match("^[\\s&&[^\\n]]*$", line):
34+
whiteLines += 1
35+
36+
tree(path)
37+
38+
print(commentLines)
39+
print(whiteLines)

Silocean/0008/Test.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<title>This is the Title</title>
6+
7+
</head>
8+
<body>
9+
<div id="container">
10+
11+
<div id="nav">
12+
13+
<div style="display:none">
14+
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9394293" charset="UTF-8"></script>
15+
</div>
16+
</body>
17+
</html>

Silocean/0008/Test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# -*- coding=utf-8 -*-
3+
4+
from bs4 import BeautifulSoup
5+
import io
6+
file = io.open('Test.html', 'r')
7+
soup = BeautifulSoup(file)
8+
print(soup.getText().strip("\n"))
9+
10+
11+
file.close()

Silocean/0009/Test.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<link rel="stylesheet" href="http://tb1.bdstatic.com/??/tb/_/thread_recommend_50cc234.css,/tb/_/grade_e31ce1c.css,/tb/_/interest_smiley_90ea01d.css,/tb/_/aside_float_bar_b8d73a2.css" />
9+
<p class="title"><b>The Dormouse's story</b></p>
10+
11+
<p class="story">Once upon a time there were three little sisters; and their names were
12+
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>
13+
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>
14+
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
15+
and they lived at the bottom of a well.</p>
16+
<img pic_type="0" class="BDE_Image" src="http://imgsrc.baidu.com/forum/w%3D580/sign=527796ed1f178a82ce3c7fa8c603737f/d3af6ccb0a46f21f6e583272f7246b600c33ae0c.jpg" bdwater="shanshanyoumei,1280,860" width="560" height="376" changedsize="true">
17+
<p class="story">...</p>
18+
</body>
19+
</html>

Silocean/0009/Test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__author__ = 'Tracy'
2+
3+
from bs4 import BeautifulSoup
4+
import io
5+
6+
with io.open('Test.html') as file:
7+
html = file.read()
8+
soup = BeautifulSoup(html)
9+
listA = soup.find_all('a')
10+
listL = soup.find_all('link')
11+
listI = soup.find_all('img')
12+
# print(listA)
13+
# print(listL)
14+
# print(listI)
15+
16+
for x in listA:
17+
print(x['href'])
18+
for x in listL:
19+
print(x['href'])
20+
for x in listI:
21+
print(x['src'])

Silocean/0010/DejaVuSansMono.ttf

314 KB
Binary file not shown.

Silocean/0010/Result.jpg

3.61 KB
Loading

Silocean/0010/Test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
__author__ = 'Tracy'
2+
import Image,ImageDraw,ImageFont,ImageFilter
3+
import random
4+
5+
image = Image.new('RGB', (50*4, 50), (255,255,255))
6+
7+
font = ImageFont.truetype('DejaVuSansMono.ttf', 24)
8+
9+
draw = ImageDraw.Draw(image)
10+
11+
def randColor():
12+
return (random.randint(64,255), random.randint(64,255), random.randint(64,255))
13+
14+
def randColor2():
15+
return (random.randint(32,127), random.randint(32,127), random.randint(32,127))
16+
17+
def randChar():
18+
return chr(random.randint(65,90))
19+
20+
for x in range(50*4):
21+
for y in range(50):
22+
draw.point((x, y), randColor())
23+
24+
for x in range(4):
25+
draw.text((50*x+10, 10), randChar(), randColor2(), font)
26+
27+
image = image.filter(ImageFilter.BLUR)
28+
image.save('Result.jpg')
29+
image.show()

Silocean/0011/Test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import io
2+
3+
file = io.open('filtered_words.txt', 'r')
4+
list = []
5+
for word in file.readlines():
6+
list.append(word.strip('\n'))
7+
8+
print(list)
9+
10+
def isValid(word):
11+
for x in list:
12+
if word == x:
13+
return False
14+
return True
15+
16+
myword = input("please input a word:")
17+
if isValid(myword):
18+
print('Human Rights')
19+
else:
20+
print('Freedom')
21+
22+
file.close()

Silocean/0011/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
����
2+
����Ա
3+
����Ա
4+
�쵼
5+
ţ��
6+
ţ��
7+
����
8+
����
9+
love
10+
sex
11+
jiangge

Silocean/0012/Test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import io
2+
3+
file = io.open('filtered_words.txt', 'r')
4+
list = []
5+
for word in file.readlines():
6+
list.append(word.strip('\n'))
7+
8+
print(list)
9+
10+
def isValid(mystr):
11+
result = mystr
12+
for x in list:
13+
if result.find(x) != -1:
14+
result = result.replace(x, '**')
15+
return result
16+
17+
mystr = input("please input a string:")
18+
print (isValid(mystr))
19+
20+
file.close()

Silocean/0012/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
����
2+
����Ա
3+
����Ա
4+
�쵼
5+
ţ��
6+
ţ��
7+
����
8+
����
9+
love
10+
sex
11+
jiangge

Silocean/0013/Test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__author__ = 'Tracy'
2+
from urllib import urlopen
3+
from bs4 import BeautifulSoup
4+
5+
f = urlopen('http://tieba.baidu.com/p/2166231880').read()
6+
7+
s = BeautifulSoup(f)
8+
9+
images = s.find_all('img', pic_type='0')
10+
count = 1
11+
def download(src):
12+
global count
13+
file_name = str(count) + '.jpg'
14+
content = urlopen(src).read()
15+
with open(file_name, 'wb') as f:
16+
f.write(content)
17+
count += 1
18+
19+
for image in images:
20+
download(image['src'])
21+

Silocean/0014/Student.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"1":["����",150,120,100],
3+
"2":["����",90,99,95],
4+
"3":["����",60,66,68]
5+
}

Silocean/0014/Test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__author__ = 'Tracy'
2+
import xlwt
3+
4+
with open('Student.txt', 'r') as f:
5+
content = f.read()
6+
7+
dic = eval(content)
8+
9+
file = xlwt.Workbook()
10+
table = file.add_sheet('Test', cell_overwrite_ok=True)
11+
12+
13+
def deal(key, value):
14+
table.write(int(key) - 1, 0, key)
15+
for x in range(len(value)):
16+
table.write(int(key) - 1, x + 1, str(value[x]).decode('gbk'))
17+
18+
for key, value in dic.items():
19+
deal(key, value)
20+
21+
file.save('result.xls')

Silocean/0014/result.xls

5.5 KB
Binary file not shown.

Silocean/0015/Test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
__author__ = 'Tracy'
2+
import xlwt
3+
4+
with open('city.txt') as f:
5+
content = f.read()
6+
dic = eval(content)
7+
8+
file = xlwt.Workbook()
9+
table = file.add_sheet("Test", cell_overwrite_ok=True)
10+
11+
for k, v in dic.items():
12+
table.write(int(k)-1, 0, k)
13+
table.write(int(k)-1, 1, str(v).decode('gbk'))
14+
15+
file.save('result.xls')

Silocean/0015/city.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"1" : "�Ϻ�",
3+
"2" : "����",
4+
"3" : "�ɶ�"
5+
}

Silocean/0015/result.xls

5.5 KB
Binary file not shown.

Silocean/0016/Test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
__author__ = 'Tracy'
2+
import xlwt
3+
4+
with open('numbers.txt') as f:
5+
content = f.read()
6+
lists = eval(content)
7+
8+
file = xlwt.Workbook()
9+
table = file.add_sheet('Test',cell_overwrite_ok=True)
10+
11+
for row in range(len(lists)):
12+
for col in range(len(lists[row])):
13+
table.write(row, col, lists[row][col])
14+
15+
file.save('result.xls')

Silocean/0016/numbers.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
[1, 82, 65535],
3+
[20, 90, 13],
4+
[26, 809, 1024]
5+
]

Silocean/0016/result.xls

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)