Skip to content

Commit f4b5938

Browse files
author
Lyndon.lin
committed
complete 0000-0024
1 parent dc8fd61 commit f4b5938

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+730
-0
lines changed

Lyndon1994/0000.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
# 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。
3+
from PIL import Image, ImageFont, ImageDraw
4+
5+
6+
def add_num(img):
7+
im = Image.open(img)
8+
w, h = im.size
9+
font = ImageFont.truetype('/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf', 30)
10+
fillcolor = "#ff0000"
11+
draw = ImageDraw.Draw(im)
12+
draw.text((w - 20, 0), '1', font=font, fill=fillcolor)
13+
im.save('r.jpg', 'jpeg')
14+
15+
16+
if __name__ == '__main__':
17+
add_num('1.jpg')

Lyndon1994/0001.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# 做为 Apple Store App 独立开发者,你要搞限时促销,
3+
# 为你的应用生成激活码(或者优惠券),
4+
# 使用 Python 如何生成 200 个激活码(或者优惠券)
5+
import random
6+
import string
7+
8+
forSelect = string.ascii_letters + string.digits
9+
10+
11+
def generate_code(count, length):
12+
for x in range(count):
13+
Re = ""
14+
for y in range(length):
15+
Re += random.choice(forSelect)
16+
print(Re)
17+
18+
19+
if __name__ == '__main__':
20+
generate_code(200, 20)

Lyndon1994/0002.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
3+
import mysql.connector
4+
5+
import random
6+
import string
7+
8+
import mysql.connector
9+
10+
forSelect = string.ascii_letters + string.digits
11+
12+
13+
def generate_code(count, length):
14+
for x in range(count):
15+
Re = ""
16+
for y in range(length):
17+
Re += random.choice(forSelect)
18+
yield Re
19+
20+
21+
def save_code():
22+
conn = mysql.connector.connect(user='root', password='l', database='test')
23+
cursor = conn.cursor()
24+
codes = generate_code(200, 20)
25+
for code in codes:
26+
cursor.execute("INSERT INTO `code`(`code`) VALUES(%s)", params=[code])
27+
conn.commit()
28+
cursor.close()
29+
30+
31+
if __name__ == '__main__':
32+
save_code()

Lyndon1994/0003.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
# **第 0003 题:**将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
3+
4+
import redis
5+
import random
6+
import string
7+
8+
forSelect = string.ascii_letters + string.digits
9+
10+
11+
def generate_code(count, length):
12+
for x in range(count):
13+
Re = ""
14+
for y in range(length):
15+
Re += random.choice(forSelect)
16+
yield Re
17+
18+
19+
def save_code():
20+
r = redis.Redis(host='127.0.0.1', port='6379', password='linyii')
21+
codes = generate_code(200, 20)
22+
p = r.pipeline()
23+
for code in codes:
24+
p.sadd('code', code)
25+
p.execute()
26+
return r.scard('code')
27+
28+
29+
if __name__ == '__main__':
30+
save_code()

Lyndon1994/0004.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# **第 0004 题:**任一个英文的纯文本文件,统计其中的单词出现的个数。
3+
import re
4+
5+
fin = open('source/0004-text.txt', 'r')
6+
str = fin.read()
7+
8+
reObj = re.compile('\b?(\w+)\b?')
9+
words = reObj.findall(str)
10+
11+
wordDict = dict()
12+
13+
for word in words:
14+
if word.lower() in wordDict:
15+
wordDict[word.lower()] += 1
16+
else:
17+
wordDict[word] = 1
18+
19+
for key, value in wordDict.items():
20+
print('%s: %s' % (key, value))

Lyndon1994/0005.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
**第 0005 题:**
4+
你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
5+
"""
6+
7+
from PIL import Image
8+
import os
9+
10+
path = 'source/0005/pics'
11+
resultPath = 'source/0005/result'
12+
if not os.path.isdir(resultPath):
13+
os.mkdir(resultPath)
14+
for picName in os.listdir(path):
15+
picPath = os.path.join(path, picName)
16+
print(picPath)
17+
with Image.open(picPath) as im:
18+
w, h = im.size
19+
n = w / 1366 if (w / 1366) >= (h / 640) else h / 640
20+
im.thumbnail((w / n, h / n))
21+
im.save(resultPath+'/finish_' + picName.split('.')[0] + '.jpg', 'jpeg')

Lyndon1994/0006.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
**第 0006 题:**
4+
你有一个目录,放了你一个月的日记,都是 txt,
5+
为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
6+
"""
7+
import os
8+
import re
9+
10+
11+
def findWord(DirPath):
12+
if not os.path.isdir(DirPath):
13+
return
14+
fileList = os.listdir(DirPath)
15+
reObj = re.compile('\b?(\w+)\b?')
16+
for file in fileList:
17+
filePath = os.path.join(DirPath, file)
18+
if os.path.isfile(filePath) and os.path.splitext(filePath)[1] == '.txt':
19+
with open(filePath) as f:
20+
data = f.read()
21+
words = reObj.findall(data)
22+
wordDict = dict()
23+
for word in words:
24+
word = word.lower()
25+
if word in ['a', 'the', 'to']:
26+
continue
27+
if word in wordDict:
28+
wordDict[word] += 1
29+
else:
30+
wordDict[word] = 1
31+
ansList = sorted(wordDict.items(), key=lambda t: t[1], reverse=True)
32+
print('file: %s->the most word: %s' % (file, ansList[1]))
33+
34+
35+
if __name__ == '__main__':
36+
findWord('source/0006')

Lyndon1994/0007.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
**第 0007 题:**
4+
有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
5+
"""
6+
import os
7+
import re
8+
9+
10+
def stat_code(dir_path):
11+
if not os.path.isdir(dir_path):
12+
return
13+
exp_re = re.compile(r'^#.*')
14+
file_list = os.listdir(dir_path)
15+
print("%s\t%s\t%s\t%s" % ('file', 'all_lines', 'space_lines', 'exp_lines'))
16+
for file in file_list:
17+
file_path = os.path.join(dir_path, file)
18+
if os.path.isfile(file_path) and os.path.splitext(file_path)[1] == '.py':
19+
with open(file_path) as f:
20+
all_lines = 0
21+
space_lines = 0
22+
exp_lines = 0
23+
for line in f.readlines():
24+
all_lines += 1
25+
if line.strip() == '':
26+
space_lines += 1
27+
continue
28+
exp = exp_re.findall(line.strip())
29+
if exp:
30+
exp_lines += 1
31+
print("%s\t%s\t%s\t%s" % (file, all_lines, space_lines, exp_lines))
32+
33+
34+
if __name__ == '__main__':
35+
stat_code('.')

Lyndon1994/0008.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
第 0008 题:一个HTML文件,找出里面的正文。
4+
"""
5+
6+
import requests,re
7+
from bs4 import BeautifulSoup
8+
9+
url = 'http://linyii.com'
10+
data=requests.get(url)
11+
r = re.findall(r'<body>[\s\S]*</body>',data.text)
12+
print(r[0])
13+
14+
print('---------------------------------------------------------------')
15+
soup = BeautifulSoup(data.text,'html.parser')
16+
print(soup.body.text)

Lyndon1994/0009.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
第 0009 题:一个HTML文件,找出里面的链接。
4+
"""
5+
import requests,re,os
6+
from bs4 import BeautifulSoup
7+
8+
url = 'http://linyii.com'
9+
data=requests.get(url)
10+
# urls = re.findall(r'<a.*href=\"(.*?)\".*</a>',data.text)
11+
# print(urls)
12+
13+
soup = BeautifulSoup(data.text,'html.parser')
14+
urls = soup.findAll('a')
15+
for u in urls:
16+
print(u['href'])

0 commit comments

Comments
 (0)