Skip to content

Commit 6ef2b2e

Browse files
committed
0000-0010
1 parent 2e0560c commit 6ef2b2e

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+
fromPILimportImage, ImageDraw, ImageFont
7+
#PIL https://pillow.readthedocs.org/
8+
defadd_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+
return0
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+
importrandom
7+
importpymysql
8+
defcreat_num(num,long):
9+
str='qwertyuiopasdfghjklzxcvbnm1234567890'
10+
b= []
11+
foriinrange(num):
12+
a=''
13+
forjinrange(long):
14+
a+=random.choice(str)
15+
b.append(a)
16+
returnb
17+
18+
defInsertIntoMysql(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+
fornumincodelist:
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+
importcollections
7+
importos
8+
9+
withopen('test.txt','r') asfp:
10+
str1=fp.read().split(' ')
11+
b=collections.Counter(str1)
12+
withopen('result.txt','w') asresult_file:
13+
forkey,valueinb.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+
fromPILimportImage
5+
importos.path
6+
7+
defSize(dirPath, size_x, size_y):
8+
f_list=os.listdir(dirPath)
9+
foriinf_list:
10+
ifos.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+
importcollections
6+
importos.path
7+
defjudgeit(words):
8+
foriinrange(6):
9+
iflen(words[i]) >2andwords[i] !='the'andwords[i] !='her'andwords[i] !='his'andwords[i] !='and'andwords[i] !='she':
10+
returnwords[i]
11+
returnwords[7]
12+
13+
defmainKeywords(dirPath):
14+
f_list=os.listdir(dirPath)
15+
foriinf_list:
16+
ifos.path.splitext(i)[1] =='.txt':
17+
print('the keywords of'+i+' is:' )
18+
withopen(i, 'r') asfp:
19+
str1=fp.read().split(' ')
20+
b=collections.Counter(str1)
21+
keywords=sorted(b, key=lambdax: 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+
importos.path
5+
importre
6+
defmainKeywords(dirPath):
7+
blank, comments, codelines, totalines, count, temp=0, 0, 0, 0, 0, 0
8+
f_list=os.listdir(dirPath)
9+
foriinf_list:
10+
ifos.path.splitext(i)[1] =='.py':
11+
print(i)
12+
withopen(i, 'r', encoding='utf-8') asfp:
13+
whileTrue:
14+
line=fp.readline()
15+
totalines+=1
16+
ifnotline:
17+
break
18+
elifline.strip().startswith('#'):
19+
comments+=1
20+
elifline.strip().startswith("'''") orline.strip().startswith('"""'):
21+
comments+=1
22+
ifline.count('"""') ==1orline.count("'''") ==1:
23+
whileTrue:
24+
line=fp.readline()
25+
totalines+=1
26+
comments+=1
27+
if ("'''"inline) or ('"""'inline):
28+
break
29+
elifline.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+
frombs4importBeautifulSoup
9+
defsechBodyUrl(path):
10+
withopen(path,encoding='utf-8') asfp:
11+
text=BeautifulSoup(fp, 'lxml')
12+
urls=text.findAll('a')
13+
foruinurls:
14+
print(u['href'])
15+
content=text.get_text().strip('\n')
16+
returncontent
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)