Skip to content

Commit 479e621

Browse files
authored
Merge pull request Show-Me-the-Code#247 from willhunger/master
weekly update
2 parents 2e3ab19 + 1294d21 commit 479e621

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

will/0001/app_store.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 第 0001 题: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
2+
'''
3+
想法思路:
4+
1. 字符串方式
5+
2. 时间戳方式
6+
3. UUID全局标识符,使用uuid1或者uuid5算法
7+
4. 加密算法
8+
'''
9+
10+
import random, string, time, math, uuid
11+
12+
chars = string.ascii_letters + string.digits
13+
14+
def gen1():
15+
'''
16+
根据26个大小写字母和数字随机选择10个
17+
涉及模块:
18+
1. random:
19+
random.random()函数是这个模块中最常用的方法了,它会生成一个随机的浮点数,范围是在0.0~1.0之间。
20+
random.uniform()正好弥补了上面函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限。
21+
random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值。
22+
random.choice()可以从任何序列,比如list列表中,选取一个随机的元素返回,可以用于字符串、列表、元组等。
23+
random.shuffle()如果你想将一个序列中的元素,随机打乱的话可以用这个函数方法。
24+
random.sample()可以从指定的序列中,随机的截取指定长度的片断,不作原地修改。
25+
2. string
26+
string.digits: 0-9
27+
string.printable:可打印字符集
28+
string.ascii_letters: 大小字母集
29+
'''
30+
key = ''.join(random.sample(chars, 10))
31+
#key2 = ''.join(random.choice(chars) for i in range(10))
32+
return key
33+
34+
def gen2():
35+
'''
36+
当前时间戳生成
37+
1. math.modf(x)返回一个list,包括小数部分及整数部分
38+
2. https://gist.github.com/willhunger/85b119793f01211de50db0e0a257dbf0
39+
3. http://www.wklken.me/posts/2015/03/03/python-base-datetime.html
40+
'''
41+
key = math.modf(time.time())[0]
42+
return key
43+
44+
def gen3():
45+
'''
46+
UUID:通用唯一识别码,由一组32位数的16进制数字所构成
47+
uuid1()——基于时间戳
48+
由MAC地址、当前时间戳、随机数生成。可以保证全球范围内的唯一性,但MAC的使用同时带来安全性问题,局域网中可以使用IP来代替MAC。
49+
uuid2()——基于分布式计算环境DCE(Python中没有这个函数)
50+
算法与uuid1相同,不同的是把时间戳的前4位置换为POSIX的UID,实际中很少用到该方法。
51+
uuid3()——基于名字的MD5散列值
52+
通过计算名字和命名空间的MD5散列值得到,保证了同一命名空间中不同名字的唯一性,和不同命名空间的唯一性,但同一命名空间的同一名字生成相同的uuid。
53+
uuid4()——基于随机数
54+
由伪随机数得到,有一定的重复概率,该概率可以计算出来。
55+
uuid5()——基于名字的SHA-1散列值
56+
算法与uuid3相同,不同的是使用 Secure Hash Algorithm 1 算法
57+
58+
'''
59+
return uuid.uuid4()
60+
61+
for i in range(200):
62+
print(gen2())

will/0002/into_mysql.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 第 0002 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
2+
'''
3+
相关模块/库 : pymysql
4+
1. http://www.runoob.com/python3/python3-mysql.html
5+
'''
6+
import random, string, time, math, uuid, pymysql
7+
8+
chars = string.ascii_letters + string.digits
9+
10+
def gen1():
11+
key = ''.join(random.sample(chars, 10))
12+
#key2 = ''.join(random.choice(chars) for i in range(10))
13+
return key
14+
15+
def gen2():
16+
key = math.modf(time.time())[0]
17+
return key
18+
19+
def gen3():
20+
return uuid.uuid4()
21+
22+
def dbp():
23+
db = pymysql.connect('localhost', 'root', '1213', 'python')
24+
cursor = db.cursor()
25+
cursor.execute("DROP TABLE IF EXISTS CODE")
26+
sql = """CREATE TABLE CODE (
27+
app_code CHAR(100) NOT NULL
28+
)"""
29+
cursor.execute(sql)
30+
return db, cursor
31+
32+
if __name__ == '__main__':
33+
db, cursor = dbp()
34+
for i in range(200):
35+
add_code = "INSERT INTO CODE(app_code) VALUES ('%s')" % gen2()
36+
# print(add_code)
37+
try:
38+
cursor.execute(add_code)
39+
db.commit()
40+
except:
41+
db.rollback()
42+
db.close()
43+
print('finish')
44+

will/0003/into_redis.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 第 0003 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
2+
import random, string, time, math, uuid, redis
3+
4+
chars = string.ascii_letters + string.digits
5+
6+
def gen1():
7+
key = ''.join(random.sample(chars, 10))
8+
#key2 = ''.join(random.choice(chars) for i in range(10))
9+
return key
10+
11+
def gen2():
12+
key = math.modf(time.time())[0]
13+
return key
14+
15+
def gen3():
16+
return uuid.uuid4()
17+
18+
if __name__ == '__main__':
19+
r = redis.Redis(host='localhost', port=6379, db=0)
20+
# r.set('name', 'will')
21+
# print(r.get('name'))
22+
for i in range(200):
23+
r.sadd('code', gen1())
24+
r.save()

will/0004/sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。
2+
3+
import re
4+
path = './a.txt'
5+
6+
def count(data):
7+
words = re.compile('[a-zA-Z0-9]+')
8+
di = {}
9+
for i in words.findall(data):
10+
if i not in di:
11+
di[i] = 1
12+
else:
13+
di[i] += 1
14+
return di
15+
16+
if __name__ == '__main__':
17+
with open(path, 'r') as file:
18+
data = file.read().lower()
19+
sumofword = count(data)
20+
print(sumofword)
21+
file.close()

0 commit comments

Comments
 (0)