diff --git a/keysona/0000/0000.py b/keysona/0000/0000.py new file mode 100644 index 00000000..b88aca16 --- /dev/null +++ b/keysona/0000/0000.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-16 15:48:35 +# @Last Modified by: key +# @Last Modified time: 2015-11-16 23:10:41 + +#--------------------------------- +#第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果 +#--------------------------------- + +import cv2 +from PIL import Image,ImageFont,ImageDraw + +DEBUG = True + +#The picture size is 200px*200px + +#opencv 3.0 +def opencv(filename,num): + img = cv2.imread(filename,1) + font = cv2.FONT_HERSHEY_PLAIN + width,height,channel = img.shape + cv2.putText(img,str(num),(int(width*5/6),int(height/4)),font,3,(0,0,255),2,cv2.LINE_AA) + cv2.imwrite("result_opencv.png",img) + if DEBUG: + cv2.imshow("Key",img) + cv2.waitKey(0) + cv2.destroyAllWindows() + +zho +#Pillow +def pillow(filename,num): + img = Image.open(filename) + x,y = img.size + font = ImageFont.truetype("arial.ttf",int(x/3)) + ImageDraw.Draw(img).text((x*4/5,0),str(num),font=font,fill = 'red') + img.save("result_pillow.png",) + if DEBUG: + img.show() + +if __name__ == '__main__': + opencv("alice_icon_sm.jpg",4) + pillow("alice_icon_sm.jpg",4) + + + + diff --git a/keysona/0000/arial.ttf b/keysona/0000/arial.ttf new file mode 100644 index 00000000..2107596f Binary files /dev/null and b/keysona/0000/arial.ttf differ diff --git a/keysona/0001/0001.py b/keysona/0001/0001.py new file mode 100644 index 00000000..12cfe63b --- /dev/null +++ b/keysona/0001/0001.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-17 12:11:29 +# @Last Modified by: key +# @Last Modified time: 2015-11-17 17:21:24 + +#-------------------------------- +#--第 0001 题: +#--做为 Apple Store App 独立开发者. +#--你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python如何生成 200 个激活码(或者优惠券)? +#-------------------------------- + +import random + +#digit 优惠码位数 +#count 优惠码数量 +def make_promo_code(digit,count): + base = ord('A') + results = set() + alphanums = [str(i) for i in range(10)] + [chr(base+i) for i in range(26)] + with open('result','w') as f: + while len(results) < 200: + temp = ''.join(random.choice(alphanums) for j in range(digit)) + if temp not in results: + f.write(temp+'\r\n') + results.add(temp) + +if __name__ == '__main__': + make_promo_code(8,200) + + diff --git a/keysona/0001/result b/keysona/0001/result new file mode 100644 index 00000000..c64a26a7 --- /dev/null +++ b/keysona/0001/result @@ -0,0 +1,200 @@ +7HWODFIL +TJYGY89Z +4N6TJP57 +H64T4GTO +XM8JMS6O +I579FT0M +CIKMF0VW +DDVWJUTI +0W5EJMU9 +K883VV6H +OI97WD52 +K424NW9G +PEMJY2TN +WDEYK5RU +L5MC3QBM +45EZV53D +UMDGE6T4 +OSSJMUO2 +4A5S3NYH +P5NKZ6ZW +55GX3ON6 +U8HP55JJ +1356QJX0 +M9VTSMM6 +O6SM0G95 +0KJTEQ0L +D2ZFLTFZ +8KSI44IS +NPGQ3TFU +V1FQLBK0 +9WDNLI7Q +Q15Z0S4Q +MT48TVPY +X8DUG38K +MNRG332L +C9JRIPHG +O47Y7H1R +VGII7SNU +LPUMZKH9 +BYMY4PPK +3FHXS5CZ +S0RRBJBD +C9L24B0Z +DWKU60IF +BBKLN8X8 +9FK3Y7ZK +LPMADTS4 +45MZ0HE7 +07RUOSYZ +12SNOATG +PSEO0VQL +7XUHDWF9 +540970O3 +ZSLOAHU9 +9VV6E51P +14XN8ZK2 +UUM1O2PL +QIXRUP13 +52NA45RQ +VXKP0F1F +E5KUCO9U +NF03VFSR +91ER7PJB +A62A3TMT +OBRFEKCI +3FQZVYLO +03CCKZAG +01D3M77G +ERVLG8TE +2HE1OJF9 +6MLRTPA7 +MLBNAM8L +71FNJL4G +TVZQMKYB +2J10N24F +ZR691YOH +FQLLL4W4 +I7TOYICC +J5NYYJL8 +NVUA19CQ +ON1NULI3 +NM77ATEE +0PDDGQAK +4TEN3AT5 +V1AXQTQB +AONJKCQL +QPUZ25XN +EVEV8787 +DBXIPOWZ +NR2LDVTZ +2Y8NZMAJ +MU9Z4X06 +8H5JBBOD +53AF3UZY +503ANOJB +GSUT0JDP +ZF87XQ92 +H3551Q5V +L3Q74GYU +LC7QZSTT +TUPGZF04 +NG1MS863 +Z2ZBGNDL +A8PPL5FH +5JJFNUQP +BD4JXXH4 +D4DIRYVO +HYDV6Q0B +18MUNHCC +MAK1WB8F +Q56JQI6R +5IB75WXZ +20EW3LDZ +Y6RJD65D +OVHL2VM1 +0HA2DK0W +XI7Y0A67 +8QBOGDMF +9BUPWCHJ +1KXEP2GM +HMENMZEW +D5UEANW3 +NG6KZGCA +PU93KB6H +UKG6FUBY +ZWOG4S2D +3OX30C2Q +U01IX7VE +SMKV3701 +1Q2R0MDW +JD11IEUT +YIIDZ6X5 +XYP0AYSG +NARXWCPN +JNVBDKBA +X5MLD9I3 +04JPW1RR +95NCU2TR +JJ2U4PL7 +GZ5PWPWK +SX0LU0EP +PVMZXFFS +HN50R2PA +TG0OISUK +G93COIO3 +2BJCV40U +RU3N0LLO +XMF8XH6B +QNGL8E01 +MRGUDGJ3 +0YQUD22L +2NTP4CVJ +1106SI79 +9GL9X7VR +XRDLPW1J +3MH03ESS +45E5NR67 +SWDCI8G8 +1EXSIO8E +0MELFD0X +DOGTTMUO +CS61BKQY +R8PYXLNN +NMONHJZT +Z2YJLP0L +2JJ3HWY9 +SF8JTMRP +PSS0SIUQ +IZ272PE9 +MBEH8RSP +NU8TFP5A +37DAXZSD +I7NY0HF2 +KZDGDTGD +8B4QLM92 +R4A5F44W +A5GILRKJ +L3MBLDG2 +JWOWS1RX +Y91Y482I +THO7AWJH +TYY8TAHU +EY954HH5 +H99NKQK9 +3BRH7NR4 +92ZL04FJ +KXVDMLFT +KH0BMEY6 +9ZZR73QN +S685DJX4 +KCXDGSWK +D7JRFO78 +OLY93COS +DAUGXWG6 +CERA0DBU +0J86RUBC +TVTDHY9V +D3GNVMNG +NFELP518 +7E7ABLES diff --git a/keysona/0002/0002.py b/keysona/0002/0002.py new file mode 100644 index 00000000..8c6559f2 --- /dev/null +++ b/keysona/0002/0002.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-17 13:38:09 +# @Last Modified by: key +# @Last Modified time: 2015-11-17 17:24:22 + +#-------------------------------------- +#第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。 +#-------------------------------------- + +import random +import MySQLdb + +#digit 优惠码位数 +#count 优惠码数量 +def make_promo_code(digit,count): + base = ord('A') + results = set() + alphanums = [str(i) for i in range(10)] + [chr(base+i) for i in range(26)] + #数据库连接 + conn = MySQLdb.connect(host='localhost',user='root',passwd='12345678',db='test') + cur = conn.cursor() + cur.execute("create table promo_code(id int auto_increment primary key,promo_code varchar(10))") + conn.commit() + while len(results) < 200: + promo_code = ''.join(random.choice(alphanums) for j in range(digit)) + if promo_code not in results: + cur.execute("insert into promo_code(promo_code) values('{0}')".format(promo_code)) + results.add(promo_code) + conn.commit() + #关闭数据库 + cur.close() + conn.close() + +if __name__ == '__main__': + make_promo_code(8,200) diff --git a/keysona/0004/0004.py b/keysona/0004/0004.py new file mode 100644 index 00000000..fc3488b8 --- /dev/null +++ b/keysona/0004/0004.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-17 14:15:01 +# @Last Modified by: key +# @Last Modified time: 2015-11-17 17:26:46 + +#-------------------------------- +#第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 +#-------------------------------- + + +from collections import Counter + +def words(): + c = Counter() + with open('test.txt') as f: + for line in f: + c.update(line.split(' ')) + return sum(c.values()) + +if __name__ == '__main__': + print(words()) + diff --git a/keysona/0004/test.txt b/keysona/0004/test.txt new file mode 100644 index 00000000..bdd031c4 --- /dev/null +++ b/keysona/0004/test.txt @@ -0,0 +1 @@ +Henry was a pen name used by an American writer of short stories. His real name was William Sydney Porter. He was born in North Carolina in 1862. As a young boy he lived an exciting life. He did not go to school for very long, but he managed to teach himself everything he needed to know. When he was about 20 years old, O. Henry went to Texas, where he tried different jobs. He first worked on a newspaper, and then had a job in a bank, when some money went missing from the bank O. Henry was believed to have stolen it. Because of that, he was sent to prison. During the three years in prison, he learned to write short stories. After he got out of prison, he went to New York and continued writing. He wrote mostly about New York and the life of the poor there. People liked his stories, because simple as the tales were, they would finish with a sudden change at the end, to the reader¡¯s surprise. diff --git a/keysona/0005/0005.py b/keysona/0005/0005.py new file mode 100644 index 00000000..c9eea44e --- /dev/null +++ b/keysona/0005/0005.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-17 22:21:34 +# @Last Modified by: key +# @Last Modified time: 2015-11-18 21:13:01 + +''' +第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 +''' + +import glob +from PIL import Image + +def change_resolution(patterns): + iphone_width = 750 + iphone_height = 1334 + pictures = [] + for pattern in patterns: + pictures.extend(glob.glob(pattern)) + print(pictures) + for picture in pictures: + img = Image.open(picture) + width = img.size[0] if iphone_width > img.size[0] else iphone_width + height = img.size[1] if iphone_height > img.size[1] else iphone_height + print(width,height) + dist = img.resize((width,height),Image.ANTIALIAS) + dist.save("ipone6_"+picture) + +if __name__ == '__main__': + change_resolution(("*.jpg","*.png")) diff --git a/keysona/0006/0006.py b/keysona/0006/0006.py new file mode 100644 index 00000000..c8511dee --- /dev/null +++ b/keysona/0006/0006.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-17 17:32:09 +# @Last Modified by: key +# @Last Modified time: 2015-11-17 19:02:54 + +import os +import glob +from collections import Counter + +def main(): + os.chdir('data') + texts = glob.glob("*.txt") + diff --git a/keysona/0008/0008.py b/keysona/0008/0008.py new file mode 100644 index 00000000..37460c2e --- /dev/null +++ b/keysona/0008/0008.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-18 21:26:32 +# @Last Modified by: key +# @Last Modified time: 2015-11-18 21:58:46 + +''' +第 0008 题:一个HTML文件,找出里面的正文。 +''' + + +import requests as req +from bs4 import BeautifulSoup + +url = "https://github.com/keysona/show-me-the-code" + +response = req.get(url=url) + +soup = BeautifulSoup(response.content,"lxml") + +print(soup.article) + + + + diff --git a/keysona/0009/0009.py b/keysona/0009/0009.py new file mode 100644 index 00000000..36cdb0d2 --- /dev/null +++ b/keysona/0009/0009.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: key +# @Date: 2015-11-18 21:38:31 +# @Last Modified by: key +# @Last Modified time: 2015-11-18 21:58:29 + +''' +第 0009 题:一个HTML文件,找出里面的链接。 +''' + +import requests as req +from bs4 import BeautifulSoup + +url = "https://github.com/keysona/show-me-the-code" + +response = req.get(url=url) + +soup = BeautifulSoup(response.content,"lxml") + +urls = [a_tag.get('href') for a_tag in soup.find_all('a') if a_tag.get('href').startswith('http')] + +print(urls)