Skip to content

Commit c28f9ba

Browse files
authored
Merge pull request #197 from Show-Me-the-Code/master
add a solution
2 parents a9371a2 + f19071c commit c28f9ba

File tree

876 files changed

+32651
-1251
lines changed

Some content is hidden

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

876 files changed

+32651
-1251
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ target/
5555

5656
# Mac File
5757
.DS_Store
58+
.idea

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "JiYouMCC"]
2+
path = JiYouMCC
3+
url = https://github.com/JiYouMCC/python-show-me-the-code

4disland/0000/add_num.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from PIL import Image, ImageDraw, ImageFont
2+
3+
def add_num(img):
4+
draw = ImageDraw.Draw(img)
5+
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=40)
6+
fillcolor = "#ff0000"
7+
width, height = img.size
8+
draw.text((width-40, 0), '99', font=myfont, fill=fillcolor)
9+
img.save('result.jpg','jpeg')
10+
11+
return 0
12+
if __name__ == '__main__':
13+
image = Image.open('image.jpg')
14+
add_num(image)

AK-wang/0001/key_gen.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
5+
# 使用 Python 如何生成 200 个激活码(或者优惠券)?
6+
7+
import string
8+
import random
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters + string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def key_num(num, result=None):
24+
if result is None:
25+
result = []
26+
for i in range(num):
27+
result.append(key_gen())
28+
return result
29+
30+
31+
def print_key(num):
32+
for i in key_num(num):
33+
print i
34+
35+
36+
if __name__ == "__main__":
37+
print_key(KEY_ALL)

AK-wang/0001/key_gen_deco.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
5+
# 使用 Python 如何生成 200 个激活码(或者优惠券)?
6+
7+
import string
8+
import random
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters+string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def print_key(func):
24+
def _print_key(num):
25+
for i in func(num):
26+
print i
27+
return _print_key
28+
29+
30+
@print_key
31+
def key_num(num, result=None):
32+
if result is None:
33+
result = []
34+
for i in range(num):
35+
result.append(key_gen())
36+
return result
37+
38+
39+
if __name__ == "__main__":
40+
# print_key(KEY_ALL)
41+
key_num(KEY_ALL)

AK-wang/0002/save_key.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
5+
6+
import MySQLdb
7+
import string
8+
import random
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters + string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def key_num(num, result=None):
24+
if result is None:
25+
result = []
26+
for i in range(num):
27+
result.append(str(key_gen()))
28+
return result
29+
30+
31+
class mysql_init(object):
32+
33+
def __init__(self, conn):
34+
self.conn = None
35+
36+
# connect to mysql
37+
def connect(self):
38+
self.conn = MySQLdb.connect(
39+
host="localhost",
40+
port=3306,
41+
user="root",
42+
passwd="123456",
43+
db="test",
44+
charset="utf8"
45+
)
46+
47+
def cursor(self):
48+
try:
49+
return self.conn.cursor()
50+
except (AttributeError, MySQLdb.OperationalError):
51+
self.connect()
52+
return self.conn.cursor()
53+
54+
def commit(self):
55+
return self.conn.commit()
56+
57+
def close(self):
58+
return self.conn.close()
59+
60+
61+
def process():
62+
dbconn.connect()
63+
conn = dbconn.cursor()
64+
DropTable(conn)
65+
CreateTable(conn)
66+
InsertDatas(conn)
67+
QueryData(conn)
68+
dbconn.close()
69+
70+
# def execute(sql):
71+
# '''执行sql'''
72+
# conn=dbconn.cursor()
73+
# conn.execute(sql)
74+
75+
# def executemany(sql, tmp):
76+
# '''插入多条数据'''
77+
# conn=dbconn.cursor()
78+
# conn.executemany(sql,tmp)
79+
80+
81+
def query(sql, conn):
82+
'''查询sql'''
83+
# conn=dbconn.cursor()
84+
conn.execute(sql)
85+
rows = conn.fetchall()
86+
return rows
87+
88+
89+
def DropTable(conn):
90+
# conn=dbconn.cursor()
91+
conn.execute("DROP TABLE IF EXISTS `user_key`")
92+
93+
94+
def CreateTable(conn):
95+
# conn=dbconn.cursor()
96+
sql_create = ''' CREATE TABLE `user_key` (`key` varchar(50) NOT NULL)'''
97+
conn.execute(sql_create)
98+
99+
100+
def InsertDatas(conn):
101+
# conn=dbconn.cursor()
102+
# insert_sql = "insert into user_key values(%s)"
103+
insert_sql = "INSERT INTO user_key VALUES (%(value)s)"
104+
key_list = key_num(KEY_ALL)
105+
# print len(key_list)
106+
# conn.executemany(insert_sql,str(key_listi))
107+
# conn.executemany("INSERT INTO user_key VALUES (%(value)s)",
108+
# [dict(value=v) for v in key_list])
109+
conn.executemany(insert_sql, [dict(value=v) for v in key_list])
110+
111+
112+
def DeleteData():
113+
del_sql = "delete from user_key where id=2"
114+
execute(del_sql)
115+
116+
117+
def QueryData(conn):
118+
sql = "select * from user_key"
119+
rows = query(sql, conn)
120+
printResult(rows)
121+
122+
123+
def printResult(rows):
124+
if rows is None:
125+
print "rows None"
126+
for row in rows:
127+
print row
128+
129+
if __name__ == "__main__":
130+
dbconn = mysql_init(None)
131+
process()

AK-wang/0003/save_to_redis.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
5+
6+
import string
7+
import random
8+
import redis
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters + string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def key_num(num, result=None):
24+
if result is None:
25+
result = []
26+
for i in range(num):
27+
result.append(key_gen())
28+
return result
29+
30+
31+
def redis_init():
32+
r = redis.Redis(host='localhost', port=6379, db=0)
33+
return r
34+
35+
36+
def push_to_redis(key_list):
37+
for key in key_list:
38+
redis_init().lpush('key', key)
39+
40+
41+
def get_from_redis():
42+
key_list = redis_init().lrange('key', 0, -1)
43+
for key in key_list:
44+
print key
45+
46+
if __name__ == "__main__":
47+
push_to_redis(key_num(200))
48+
get_from_redis()

AK-wang/0004/0004.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数
5+
6+
from collections import Counter
7+
import re
8+
9+
10+
def creat_list(filename):
11+
datalist = []
12+
with open(filename, 'r') as f:
13+
for line in f:
14+
content = re.sub("\"|,|\.", "", line)
15+
datalist.extend(content.strip().split(' '))
16+
return datalist
17+
18+
19+
def wc(filename):
20+
print Counter(creat_list(filename))
21+
22+
if __name__ == "__main__":
23+
filename = 'test.txt'
24+
wc(filename)

AK-wang/0004/test.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
In the latest move to support the economy,
2+
Shanghai, Beijing, Chongqing and six other provinces and municipalities will allow banks to refinance high-quality credit assets rated by the People's Bank of China,
3+
said the central bank, as the program was first introduced in Guangdong and Shandong provinces last year.
4+

AK-wang/0006/00.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
In China, when people go across the road, they will never wait for the red light
2+
patiently. In fact, Chinese people are famous for running the green light, it
3+
seems to be a habit for them, the traffic rule is just the paper for them, they
4+
never obey it. The result of going against the traffic rule is serious.
5+
we we we we we we we

AK-wang/0006/01.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
On the one hand, running the red light is not a civilized behavior, Chinese
2+
people will bring the foreign people the bad impression. When a foreigner comes
3+
to China, he is so curious about the way Chinese people go across the road, he
4+
waits for the green light, while a lot of Chinese people ignore the traffic
5+
rule and go directly. He feels so hilarious about the situation, it is so
6+
uncivilized behavior.
7+
python python python python python python python python python python python
8+
is the best useful language!

AK-wang/0006/02.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
On the other hand, running the red light results in accident, people will lose
2+
their lives. Every year, many people die of car accident, the main reason is
3+
that they do not obey the traffic rule, when they go across the road, the car
4+
hits them and the tragedy happens.

AK-wang/0006/key_word.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
5+
6+
import glob
7+
from collections import Counter
8+
import re
9+
10+
11+
def list_txt():
12+
return glob.glob("*.txt")
13+
14+
15+
def wc(filename):
16+
datalist = []
17+
with open(filename, 'r') as f:
18+
for line in f:
19+
content = re.sub("\"|,|\.", "", line)
20+
datalist.extend(content.strip().split(' '))
21+
# print datalist
22+
return Counter(datalist).most_common(1)
23+
24+
25+
def most_comm():
26+
all_txt = list_txt()
27+
for txt in all_txt:
28+
print wc(txt)
29+
30+
if __name__ == "__main__":
31+
# most_comm()
32+
print map(wc, list_txt())

AK-wang/0011/filtered_words.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,
5+
# 当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
6+
7+
8+
def filtered_words(f_file):
9+
filtered_list = []
10+
with open(f_file, 'r') as f:
11+
for line in f:
12+
filtered_list.append(line.strip())
13+
return filtered_list
14+
15+
16+
def filtered_or_not(input_word, f_file):
17+
filtered_words_list = filtered_words(f_file)
18+
return (input_word in filtered_words_list)
19+
20+
21+
def print_user_input(input_word, f_file):
22+
if filtered_or_not(input_word, f_file):
23+
return "Freedom"
24+
return "Human Rights"
25+
26+
27+
if __name__ == "__main__":
28+
input_word = raw_input("please input your word:")
29+
f_file = "filtered_words.txt"
30+
print print_user_input(input_word, f_file)

0 commit comments

Comments
 (0)