Skip to content

Commit a9371a2

Browse files
committed
Revert "Complete 0000-0003"
1 parent 9ccaac3 commit a9371a2

File tree

455 files changed

+21733
-1580
lines changed

Some content is hidden

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

455 files changed

+21733
-1580
lines changed

JiYouMCC/0000/0000.png

2.54 KB
Loading

JiYouMCC/0000/0000.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
# 第0000题:将你的QQ头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示。
3+
4+
# using PIL in http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow
5+
from PIL import Image
6+
from PIL import ImageFont
7+
from PIL import ImageDraw
8+
9+
10+
def write_number(image_file_path, number=1):
11+
img = Image.open(image_file_path)
12+
font_size = img.size[0] if img.size[0] < img.size[1] else img.size[1]
13+
font_size = font_size / 4
14+
number_txt = str(number) + ' ' if number < 100 else '99+'
15+
font = ImageFont.truetype("arial.ttf", size=font_size)
16+
if font.getsize(number_txt)[0] > img.size[0] or font.getsize(number_txt)[1] > img.size[1]:
17+
return img
18+
position = img.size[0] - font.getsize(number_txt)[0]
19+
ImageDraw.Draw(img).text((position, 0), number_txt, (255, 0, 0), font)
20+
return img
21+
22+
write_number('0000.png').save('result.png')
23+
write_number('0000.png', 100).save('result100.png')

JiYouMCC/0001/0001.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
# 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
3+
4+
import uuid
5+
6+
7+
def create_code(number=200):
8+
result = []
9+
while True is True:
10+
temp = str(uuid.uuid1()).replace('-', '')
11+
if not temp in result:
12+
result.append(temp)
13+
if len(result) is number:
14+
break
15+
return result
16+
17+
print create_code()

JiYouMCC/0002/0002.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
3+
# using sina app
4+
# test page:http://mccatcivitas.sinaapp.com/showmecode2
5+
import sae.const
6+
import MySQLdb
7+
import uuid
8+
9+
10+
def create_code(number=200):
11+
result = []
12+
while True is True:
13+
temp = str(uuid.uuid1()).replace('-', '')
14+
if not temp in result:
15+
result.append(temp)
16+
if len(result) is number:
17+
break
18+
return result
19+
20+
21+
def insertCode(code, table='app_mccatcivitas.showmethecode'):
22+
conn = MySQLdb.connect(
23+
host=sae.const.MYSQL_HOST,
24+
user=sae.const.MYSQL_USER,
25+
passwd=sae.const.MYSQL_PASS,
26+
port=int(sae.const.MYSQL_PORT),
27+
charset='utf8')
28+
cur = conn.cursor()
29+
cur.execute("""insert into %s values('%s')""" % (
30+
table, code))
31+
conn.commit()
32+
cur.close()
33+
conn.close()
34+
35+
36+
def selectCodes(table='app_mccatcivitas.showmethecode'):
37+
connection = MySQLdb.connect(
38+
host=sae.const.MYSQL_HOST,
39+
user=sae.const.MYSQL_USER,
40+
passwd=sae.const.MYSQL_PASS,
41+
port=int(sae.const.MYSQL_PORT),
42+
init_command='set names utf8')
43+
cur = connection.cursor()
44+
cur.execute("""select * from %s""" % (table))
45+
result = []
46+
rows = cur.fetchall()
47+
for row in rows:
48+
result.append(str(row[0]))
49+
return result
50+
51+
52+
def cleanUp(table='app_mccatcivitas.showmethecode'):
53+
connection = MySQLdb.connect(
54+
host=sae.const.MYSQL_HOST,
55+
user=sae.const.MYSQL_USER,
56+
passwd=sae.const.MYSQL_PASS,
57+
port=int(sae.const.MYSQL_PORT),
58+
init_command='set names utf8')
59+
cur = connection.cursor()
60+
try:
61+
cur.execute("""drop table %s""" % (table))
62+
except Exception, e:
63+
print e
64+
connection.commit()
65+
cur.execute(
66+
"""create table %s (code char(32) not null primary key)""" % (table))
67+
connection.commit()
68+
cur.close()
69+
connection.close()
70+
71+
72+
def Process():
73+
cleanUp()
74+
code = create_code()
75+
for c in code:
76+
insertCode(c)
77+
result = selectCodes()
78+
return result

JiYouMCC/0003/0003.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
3+
# fail to install redis, skip it

JiYouMCC/0004/0004.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。
3+
import io
4+
import operator
5+
6+
7+
def get_count_table(file='0004.txt', ignore=[',', '.', ':', '!', '?', '”', '“', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'], lower=True):
8+
txt = open(file).read()
9+
for i in ignore:
10+
txt = txt.replace(i, ' ')
11+
if lower:
12+
txt = txt.lower()
13+
words = txt.split(' ')
14+
dic = {}
15+
for word in words:
16+
if word is '':
17+
continue
18+
if word in dic:
19+
dic[word] += 1
20+
else:
21+
dic[word] = 1
22+
return dic
23+
24+
25+
result = sorted(
26+
get_count_table().items(), key=operator.itemgetter(1), reverse=True)
27+
for item in result:
28+
print item[0], item[1]

JiYouMCC/0004/0004.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Then I looked, and behold, on Mount Zion stood the Lamb, and with him 144,000 who had his name and his Father’s name written on their foreheads.And I heard a voice from heaven like the roar of many waters and like the sound of loud thunder. The voice I heard was like the sound of harpists playing on their harps,and they were singing a new song before the throne and before the four living creatures and before the elders. No one could learn that song except the 144,000 who had been redeemed from the earth.It is these who have not defiled themselves with women, for they are virgins. It is these who follow the Lamb wherever he goes. These have been redeemed from mankind as firstfruits for God and the Lamb,and in their mouth no lie was found, for they are blameless.The Messages of the Three AngelsThen I saw another angel flying directly overhead, with an eternal gospel to proclaim to those who dwell on earth, to every nation and tribe and language and people.And he said with a loud voice, “Fear God and give him glory, because the hour of his judgment has come, and worship him who made heaven and earth, the sea and the springs of water.”Another angel, a second, followed, saying, “Fallen, fallen is Babylon the great, she who made all nations drink the wine of the passion1 of her sexual immorality.”And another angel, a third, followed them, saying with a loud voice, “If anyone worships the beast and its image and receives a mark on his forehead or on his hand,10 he also will drink the wine of God’s wrath, poured full strength into the cup of his anger, and he will be tormented with fire and sulfur in the presence of the holy angels and in the presence of the Lamb.And the smoke of their torment goes up forever and ever, and they have no rest, day or night, these worshipers of the beast and its image, and whoever receives the mark of its name.”Here is a call for the endurance of the saints, those who keep the commandments of God and their faith in Jesus.2And I heard a voice from heaven saying, “Write this: Blessed are the dead who die in the Lord from now on.” “Blessed indeed,” says the Spirit, “that they may rest from their labors, for their deeds follow them!”The Harvest of the EarthThen I looked, and behold, a white cloud, and seated on the cloud one like a son of man, with a golden crown on his head, and a sharp sickle in his hand.And another angel came out of the temple, calling with a loud voice to him who sat on the cloud, “Put in your sickle, and reap, for the hour to reap has come, for the harvest of the earth is fully ripe.”So he who sat on the cloud swung his sickle across the earth, and the earth was reaped.Then another angel came out of the temple in heaven, and he too had a sharp sickle.And another angel came out from the altar, the angel who has authority over the fire, and he called with a loud voice to the one who had the sharp sickle, “Put in your sickle and gather the clusters from the vine of the earth, for its grapes are ripe.”So the angel swung his sickle across the earth and gathered the grape harvest of the earth and threw it into the great winepress of the wrath of God.And the winepress was trodden outside the city, and blood flowed from the winepress, as high as a horse’s bridle, for 1,600 stadia.

JiYouMCC/0005/0005-r.jpg

786 KB
Loading

JiYouMCC/0005/0005.jpg

760 KB
Loading

JiYouMCC/0005/0005.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
3+
# using PIL in http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow
4+
from PIL import Image
5+
6+
7+
def change_image_size(image_path='0005.jpg', size=(1136, 640)):
8+
im = Image.open(image_path)
9+
size = (size[1], size[0]) if im.size[1] > im.size[0] else size
10+
im.thumbnail(size, Image.ANTIALIAS)
11+
im.save('result-' + image_path)
12+
13+
change_image_size('0005-r.jpg')
14+
change_image_size('0005.jpg')

JiYouMCC/0013/0013.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 第 0013 题: 用 Python 写一个爬图片的程序,爬 [这个链接里](http://tieba.baidu.com/p/2166231880)的日本妹子图片 :-) #
2+
3+
----
4+
5+
其实我是我为了爬我自己一个[老博客](http://ycool.com/post/ae3u4zu)上面的图片才做这题的。。
6+
7+
我很懒惰没用啥爬虫架构请无视..
8+
9+
百度贴吧上面格式绝对没有我老博客上那么老实,就将就下吧

JiYouMCC/0013/0013.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import urllib2
2+
import urllib
3+
import re
4+
import os
5+
import uuid
6+
7+
8+
def get_images(html_url='http://ycool.com/post/ae3u4zu',
9+
folder_name='jiyou_blog_PingLiangRoad',
10+
extensions=['gif', 'jpg', 'png']):
11+
request_html = urllib2.Request(html_url)
12+
try:
13+
response = urllib2.urlopen(request_html)
14+
html = response.read()
15+
r1 = r'<img.+src=\".+?\"'
16+
r2 = r'<img.+src=\"(.+?)\"'
17+
results = []
18+
imgs = []
19+
p = re.compile(r1)
20+
for m in p.finditer(html):
21+
results.append(m.group())
22+
for result in results:
23+
compile_result = re.compile(r2)
24+
imgs.append(compile_result.sub(r'\1', result))
25+
if not os.path.exists(folder_name):
26+
os.makedirs(folder_name)
27+
for img in imgs:
28+
filename = str(uuid.uuid1())
29+
ex = ''
30+
for extension in extensions:
31+
if '.%s' % extension in img:
32+
ex = '.%s' % extension
33+
if ex is '':
34+
continue
35+
filename += ex
36+
try:
37+
urllib.urlretrieve(img, os.path.join(folder_name, filename))
38+
print 'Image save at %s' % output.name
39+
except Exception, ex:
40+
print ex
41+
except Exception, e:
42+
print e
43+
44+
get_images()
45+
# japanese girls
46+
get_images(html_url='http://tieba.baidu.com/p/2166231880', folder_name='jp')

JiYouMCC/0014/0014.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 第 0014 题 #
2+
3+
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
4+
5+
{
6+
"1":["张三",150,120,100],
7+
"2":["李四",90,99,95],
8+
"3":["王五",60,66,68]
9+
}
10+
11+
请将上述内容写到 student.xls 文件中,如下图所示:
12+
13+
![student.xls](https://camo.githubusercontent.com/18dea9401449e4ca894d40d55134d9c28083280d/687474703a2f2f692e696d6775722e636f6d2f6e50446c706d652e6a7067)

JiYouMCC/0014/0014.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
import xlwt
3+
import json
4+
import sys
5+
reload(sys)
6+
7+
sys.setdefaultencoding('utf8')
8+
9+
10+
file = xlwt.Workbook(encoding='utf-8')
11+
table = file.add_sheet('student', cell_overwrite_ok=True)
12+
txt = open('student.txt').read()
13+
json_txt = json.loads(txt)
14+
for x in range(len(json_txt)):
15+
table.write(x, 0, x + 1)
16+
for y in range(len(json_txt[str(x + 1)])):
17+
table.write(x, y + 1, json_txt[str(x + 1)][y])
18+
file.save('students.xls')

Jimmy66/0014/student.txt renamed to JiYouMCC/0014/student.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"1":["张三",150,120,100],
33
"2":["李四",90,99,95],
44
"3":["王五",60,66,68]
5-
}
5+
}

JiYouMCC/0015/0015.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
2+
3+
{
4+
"1" : "上海",
5+
"2" : "北京",
6+
"3" : "成都"
7+
}
8+
9+
请将上述内容写到 city.xls 文件中,如下图所示:
10+
11+
![city.xls](https://camo.githubusercontent.com/61120377319bfe5520c9d73f51776f923d6bd3b7/687474703a2f2f692e696d6775722e636f6d2f724f4862557a672e706e67)

JiYouMCC/0015/0015.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
import xlwt
3+
import json
4+
import sys
5+
reload(sys)
6+
7+
sys.setdefaultencoding('utf8')
8+
9+
10+
file = xlwt.Workbook(encoding='utf-8')
11+
table = file.add_sheet('city', cell_overwrite_ok=True)
12+
txt = open('city.txt').read()
13+
json_txt = json.loads(txt)
14+
for x in range(len(json_txt)):
15+
table.write(x, 0, x + 1)
16+
table.write(x, 1, json_txt[str(x + 1)])
17+
file.save('city.xls')
File renamed without changes.

JiYouMCC/0016/0016.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
第 0016 题: 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:
2+
3+
[
4+
[1, 82, 65535],
5+
[20, 90, 13],
6+
[26, 809, 1024]
7+
]
8+
9+
请将上述内容写到 numbers.xls 文件中,如下图所示:
10+
11+
![numbers.xls](https://camo.githubusercontent.com/60da4d596289212b517547ddcc2408bfc9f39087/687474703a2f2f692e696d6775722e636f6d2f69757a305062762e706e67)

JiYouMCC/0016/0016.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
import xlwt
3+
import json
4+
import sys
5+
reload(sys)
6+
7+
sys.setdefaultencoding('utf8')
8+
9+
10+
file = xlwt.Workbook(encoding='utf-8')
11+
table = file.add_sheet('numbers', cell_overwrite_ok=True)
12+
txt = open('numbers.txt').read()
13+
json_txt = json.loads(txt)
14+
for x in range(len(json_txt)):
15+
for y in range(len(json_txt[x])):
16+
table.write(x, y, json_txt[x][y])
17+
file.save('numbers.xls')

JiYouMCC/0016/numbers.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
[1, 82, 65535],
3+
[20, 90, 13],
4+
[26, 809, 1024]
5+
]

JiYouMCC/0021/0021.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0021 题: 通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。
3+
import os
4+
from hashlib import sha256
5+
from hmac import HMAC
6+
7+
SALT_LENGTH = 8
8+
9+
10+
def encrypt_password(password, salt=None):
11+
if salt is None:
12+
salt = os.urandom(SALT_LENGTH)
13+
if isinstance(password, unicode):
14+
password = password.encode('UTF-8')
15+
result = password
16+
for i in xrange(8):
17+
result = HMAC(result, salt, sha256).digest()
18+
return salt + result
19+
20+
21+
def validate_password(hashed, input_password):
22+
return hashed == encrypt_password(input_password, salt=hashed[:SALT_LENGTH])
23+
24+
25+
password = 'secret'
26+
encrypt = encrypt_password(password)
27+
print validate_password(encrypt, password)

JiYouMCC/0022/0022.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0022 题: iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用。
3+
# using PIL in http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow
4+
# 我承认我是先看到22才看到5的
5+
from PIL import Image
6+
7+
8+
def change_image_size(image_path='0005.jpg', size=(1136, 640)):
9+
im = Image.open(image_path)
10+
size = (size[1], size[0]) if im.size[1] > im.size[0] else size
11+
im.thumbnail(size, Image.ANTIALIAS)
12+
im.save('result-' + image_path)
13+
14+
change_image_size('0005-r.jpg')
15+
change_image_size('0005.jpg')
16+
17+
# ip6
18+
change_image_size(image_path='0005.jpg', size=(1334, 750))
19+
20+
# ip6plus
21+
change_image_size(image_path='0005.jpg', size=(1920, 1080))

0 commit comments

Comments
 (0)