Skip to content

Commit f62e9ad

Browse files
authored
Merge pull request Show-Me-the-Code#240 from doubiiot/doubiiot
Doubiiot
2 parents 2be7775 + 6ef2b2e commit f62e9ad

File tree

11 files changed

+205
-0
lines changed

11 files changed

+205
-0
lines changed

doubi_sdust/0000.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
3+
第 0000 题: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果
4+
5+
'''
6+
from PIL import Image, ImageDraw, ImageFont
7+
#PIL https://pillow.readthedocs.org/
8+
def add_num(img):
9+
draw = ImageDraw.Draw(img)
10+
#加载TrueType或OpenType字体文件,并创建一个字体对象。
11+
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=20)
12+
fillcolor = "#ff0000"
13+
width, height = img.size
14+
draw.text((width-40, 0), '2', font=myfont, fill=fillcolor)
15+
img.save('result.jpg','jpeg')
16+
return 0
17+
18+
image = Image.open('image.jpg')
19+
print(image.format,image.size,image.mode)
20+
add_num(image)

doubi_sdust/0001.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
第 0001 题: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
3+
4+
将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
5+
'''
6+
import random
7+
import pymysql
8+
def creat_num(num,long):
9+
str = 'qwertyuiopasdfghjklzxcvbnm1234567890'
10+
b = []
11+
for i in range(num):
12+
a = ''
13+
for j in range(long):
14+
a += random.choice(str)
15+
b.append(a)
16+
return b
17+
18+
def InsertIntoMysql(codelist):
19+
# 打开数据库连接
20+
db = pymysql.connect(host='127.0.0.1',user='root',passwd='919824467',db='mysql')
21+
# 使用 cursor() 方法创建一个游标对象 cursor
22+
cur = db.cursor()
23+
#数据库语句
24+
cur.execute('CREATE DATABASE IF NOT EXISTS code')
25+
cur.execute('USE code')
26+
cur.execute('''CREATE TABLE IF NOT EXISTS num(
27+
id INT NOT NULL AUTO_INCREMENT,
28+
code VARCHAR(32) NOT NULL,
29+
PRIMARY KEY(id) )''')
30+
for num in codelist:
31+
cur.execute('INSERT INTO num(code) VALUES(%s)',(num))
32+
cur.connection.commit()
33+
db.close()
34+
35+
InsertIntoMysql(creat_num(200,10))

doubi_sdust/0002.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#refer to 0001.py

doubi_sdust/0003.py

Whitespace-only changes.

doubi_sdust/0004.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。
3+
'''
4+
5+
# encoding: utf-8
6+
import collections
7+
import os
8+
9+
with open('test.txt','r') as fp:
10+
str1=fp.read().split(' ')
11+
b = collections.Counter(str1)
12+
with open('result.txt','w') as result_file:
13+
for key,value in b.items():
14+
result_file.write(key+':'+str(value)+'\n')

doubi_sdust/0005.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
第 0005 题: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
3+
'''
4+
from PIL import Image
5+
import os.path
6+
7+
def Size(dirPath, size_x, size_y):
8+
f_list = os.listdir(dirPath)
9+
for i in f_list:
10+
if os.path.splitext(i)[1] == '.jpg':
11+
img = Image.open(i)
12+
img.thumbnail((size_x,size_y))
13+
img.save(i)
14+
print(i)
15+
Size('D:\PyCharm 2017.1.3\projects', 1136, 640)

doubi_sdust/0006.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
第 0006 题: 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
3+
'''
4+
# encoding: utf-8
5+
import collections
6+
import os.path
7+
def judgeit(words):
8+
for i in range(6):
9+
if len(words[i]) > 2 and words[i] != 'the' and words[i] != 'her' and words[i] != 'his' and words[i] != 'and' and words[i] != 'she':
10+
return words[i]
11+
return words[7]
12+
13+
def mainKeywords(dirPath):
14+
f_list = os.listdir(dirPath)
15+
for i in f_list:
16+
if os.path.splitext(i)[1] == '.txt':
17+
print('the keywords of' + i + ' is:' )
18+
with open(i, 'r') as fp:
19+
str1 = fp.read().split(' ')
20+
b = collections.Counter(str1)
21+
keywords = sorted(b, key=lambda x: b[x],reverse = True)
22+
print(judgeit(keywords))
23+
24+
mainKeywords('D:\PyCharm 2017.1.3\projects')

doubi_sdust/0007.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
3+
'''
4+
import os.path
5+
import re
6+
def mainKeywords(dirPath):
7+
blank, comments, codelines, totalines, count, temp = 0, 0, 0, 0, 0, 0
8+
f_list = os.listdir(dirPath)
9+
for i in f_list:
10+
if os.path.splitext(i)[1] == '.py':
11+
print(i)
12+
with open(i, 'r', encoding='utf-8') as fp:
13+
while True:
14+
line = fp.readline()
15+
totalines += 1
16+
if not line:
17+
break
18+
elif line.strip().startswith('#'):
19+
comments += 1
20+
elif line.strip().startswith("'''") or line.strip().startswith('"""'):
21+
comments += 1
22+
if line.count('"""') == 1 or line.count("'''") == 1:
23+
while True:
24+
line = fp.readline()
25+
totalines += 1
26+
comments += 1
27+
if ("'''" in line) or ('"""' in line):
28+
break
29+
elif line.strip():
30+
codelines += 1
31+
else:
32+
blank += 1
33+
print('the nuber of totalines is : ' + str(totalines-1))
34+
print('the nuber of comments is : ' + str(comments))
35+
print('the nuber of codelines is : ' + str(codelines))
36+
print('the nuber of blanklines is : ' + str(blank))
37+
blank, comments, codelines, totalines = 0, 0, 0, 0
38+
39+
mainKeywords('D:\PyCharm 2017.1.3\projects')

doubi_sdust/0008.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
第 0008 题: 一个HTML文件,找出里面的正文。
3+
4+
第 0009 题: 一个HTML文件,找出里面的链接。
5+
'''
6+
7+
# coding=utf-8
8+
from bs4 import BeautifulSoup
9+
def sechBodyUrl(path):
10+
with open(path,encoding='utf-8') as fp:
11+
text = BeautifulSoup(fp, 'lxml')
12+
urls = text.findAll('a')
13+
for u in urls:
14+
print(u['href'])
15+
content = text.get_text().strip('\n')
16+
return content
17+
18+
sechBodyUrl('0007.html')
19+
#print(searchBody('0007.html'))

doubi_sdust/0009.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#refer to 0008.py

0 commit comments

Comments
 (0)