Skip to content

Commit c065ddf

Browse files
committed
1 parent bc39127 commit c065ddf

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

LiamHuang/0000/0000.py renamed to LiamHuang/0000/solution.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
0000:
44
将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。
55
类似于图中效果 http://i.imgur.com/sg2dkuY.png?1
6+
7+
笔记参见:
8+
http://liam0205.me/2015/04/22/pil-tutorial-basic-usage/
9+
http://liam0205.me/2015/05/05/pil-tutorial-imagedraw-and-imagefont/
610
"""
711

812
from PIL import Image, ImageDraw, ImageFont

LiamHuang/0001/solution.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# coding: utf-8
2+
3+
'''
4+
做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
5+
使用 Python 如何生成 200 个激活码(或者优惠券)?
6+
'''
7+
8+
import random, string
9+
10+
class LengthError(ValueError):
11+
def __init__(self, arg):
12+
self.args = arg
13+
14+
def pad_zero_to_left(inputNumString, totalLength):
15+
'''
16+
takes inputNumString as input,
17+
pads zero to its left, and make it has the length totalLength
18+
1. calculates the length of inputNumString
19+
2. compares the length and totalLength
20+
2.1 if length > totalLength, raise an error
21+
2.2 if length == totalLength, return directly
22+
2.3 if length < totalLength, pads zeros to its left
23+
'''
24+
lengthOfInput = len(inputNumString)
25+
if lengthOfInput > totalLength:
26+
raise LengthError("The length of input is greater than the total\ length.")
27+
else:
28+
return '0' * (totalLength - lengthOfInput) + inputNumString
29+
30+
poolOfChars = string.ascii_letters + string.digits
31+
random_codes = lambda x, y: ''.join([random.choice(x) for i in range(y)])
32+
33+
def invitation_code_generator(quantity, lengthOfRandom, LengthOfKey):
34+
'''
35+
generate `quantity` invitation codes
36+
'''
37+
placeHoldChar = "L"
38+
for index in range(quantity):
39+
tempString = ""
40+
try:
41+
yield random_codes(poolOfChars, lengthOfRandom) + placeHoldChar + \
42+
pad_zero_to_left(str(index), LengthOfKey)
43+
except LengthError:
44+
print "Index exceeds the length of master key."
45+
46+
for invitationCode in invitation_code_generator(200, 16, 4):
47+
print invitationCode

0 commit comments

Comments
 (0)