diff --git a/0000/0000.py b/0000/0000.py new file mode 100644 index 0000000000..1cb8a78ea3 --- /dev/null +++ b/0000/0000.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +''' + 在你的头像上加上数字,像微信的消息提示一样。 +''' +from PIL import Image,ImageFont,ImageDraw +import random + +def add_num(image_path): + img=Image.open(image_path) + x,y=img.size #得到头像的宽和高 + draw=ImageDraw.Draw(img) + text=str(random.randint(1,99)) #产生随机数字并字符化 + myfont=ImageFont.truetype('Futura.ttf',100) #初始text的字体类型和尺寸 + draw.text((x*3/4,0),text,font=myfont,fill='red') #修改图像 + + img.save('result.jpg','jpeg') #保存 + +if '__name__'=='__main__': + add_num('pic.jpg') + diff --git a/0000/Futura.ttf b/0000/Futura.ttf new file mode 100644 index 0000000000..ddedd04e86 Binary files /dev/null and b/0000/Futura.ttf differ diff --git a/0000/pic.jpg b/0000/pic.jpg new file mode 100644 index 0000000000..6520a83428 Binary files /dev/null and b/0000/pic.jpg differ diff --git a/0000/result.jpg b/0000/result.jpg new file mode 100644 index 0000000000..cab11e088b Binary files /dev/null and b/0000/result.jpg differ diff --git a/0001/0001.py b/0001/0001.py new file mode 100644 index 0000000000..f1ccdae1d3 --- /dev/null +++ b/0001/0001.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- + +'''生成200个长度为6的随机码''' + +import random,string + +#生成随机码 +def generateCodes(code_amount,code_len): + Strs=string.digits+string.letters + codes=[] + while(code_amount>0): + code=''.join([random.choice(Strs) for i in range(code_len)]) + if code not in codes: + code+='\n' + codes.append(code) + code_amount-=1 + return codes + +#将随机码写入codes.txt +def file_write(code_amount,code_len): + f=open('codes.txt','w') + for code in generateCodes(code_amount,code_len): + f.write(code) + f.close() + +if __name__=='__main__': + file_write(200,6) diff --git a/0001/codes.txt b/0001/codes.txt new file mode 100644 index 0000000000..4c6570e729 --- /dev/null +++ b/0001/codes.txt @@ -0,0 +1,200 @@ +TgPBxf +CVowQf +3geks8 +C39x3H +asH2cO +ep77Sz +L4LXqC +eLESID +FmMU9Y +AXWzNa +JDeiTS +0iK5yg +8M82Hk +wPsXqs +4HqdvX +9NTGrC +1vcmsr +dwbtWU +FspcuB +Xjvt6j +cWU8nc +MVcDlL +Rscydc +dAPTb3 +tFsY5X +scplD2 +GlcOIC +CF5iYb +4enhxh +G8JHXo +C4Hg1m +TRpJyp +u70BSQ +kxzpHM +27rbMo +wbfhKe +4AtXQf +msXapJ +hDwi45 +O94zet +Fn9iL2 +DaGRIW +8e7Nje +fuGWr7 +PgNyBh +aodJiZ +1yAqTt +asySdM +cxyVRp +pzQihk +4kdUWF +QjbSSg +vx0nSu +QW7CHb +hLHU6K +tjlpCA +a7myrr +CEhXox +II8Zis +OfeFPS +Kufn4p +raYXEj +P30UDq +NCGjuS +rjly2n +R9VVhY +U2zUJa +WqpCvB +S3e1jC +VRlvgM +7JNuxW +3dQS6n +y4EBt2 +WHMnuj +v17BOt +MgSqcA +tFlG84 +3t6bOM +MmEoos +VYyZZe +8zcayM +uhAisc +kNJP5r +gVxHKY +DyrX3j +ZGVVaw +5sYHyW +YvbBJh +wBrtLl +9vcOIC +Twww7u +9KFuQS +fc8ew9 +ibtWFT +CZMwx1 +KThLy6 +oV3bPx +TJP2qN +xC9k4V +lopHZW +C3rSPX +JpWpmS +iOTmZK +Rx7fAL +IWo6Nw +78ky9O +ZFDtTg +Sya00v +mi3JiK +xFL6qr +mkvuVp +27Xhbj +7VP3pN +NEex7s +GFtkQh +eI4ZHD +1md6Ic +zqXka7 +WJ6Z1Q +D8o2Hm +ti9GAm +yx7Qza +mHpgLm +scqKkT +2nXgJk +McuxGq +h81m6b +YQwNEz +5AbGOk +cjuNpX +k2eZXi +hjifpP +VdA8kn +8YodQm +PbNST6 +ZhuBzC +z8n3pW +Hf4QeY +gZKugx +EUaHan +qeRz11 +dpXPow +Iz8KI1 +FZsGYk +4t0DRM +ILroFm +jkGSdF +m8duxP +BjguYn +ceKOHq +XqFIQK +e5kwXM +MN7Pbt +BOtcpc +TRPtgu +Ye02Oa +utKUZa +FRAyEJ +5Fx6XB +zZznzk +FxhJMy +WcF9WX +2aUaWN +alVYGH +OH4VpS +U3GDTz +3pvi9S +L6TA6F +XUhihY +8v65Cm +tWogs3 +NXAc3j +D0oJ3I +C8Zv52 +Hte3mo +4sOLRT +BFUtkA +aZtMED +oEb8il +jO9Eu5 +0Hj646 +9r1X2g +hGnNVd +srGsiQ +Skja0g +fkFIMY +IiOAp5 +PVGMn6 +8k6c8e +a49vkH +oH8CEw +ivQNAl +d2nUpu +ihkj27 +P19Fj8 +SidKm8 +8LXLPq +xQwvLp +hdop4B +MbHqZK diff --git a/0002/0002.py b/0002/0002.py new file mode 100644 index 0000000000..3e714bfebe --- /dev/null +++ b/0002/0002.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +'''将随机码存入mysql''' + +import mysql.connector + +#读取文件 +def file_read(filename): + #从文件中读取每行的码 + codes=[] + with open(filename,'r') as f: + lines=f.readlines() + for n in range(200): + #去除每行的空白符 + codes.append(lines[n].strip()) + return codes + +#存入数据库 +def store(): + codes=file_read('codes.txt') + conn=mysql.connector.connect(user='root',password='666666',database='test',use_unicode=True) + cursor=conn.cursor() + cursor.execute('create table codes(id int(3) primary key,code varchar(6))') + for n in range(200): + cursor.execute('insert into codes (id,code) values (%s,%s)',[n+1,codes[n]]) + conn.commit() + cursor.close() + conn.close() + print 'finish' + +if __name__ == '__main__': + store() diff --git a/0002/codes.txt b/0002/codes.txt new file mode 100644 index 0000000000..4c6570e729 --- /dev/null +++ b/0002/codes.txt @@ -0,0 +1,200 @@ +TgPBxf +CVowQf +3geks8 +C39x3H +asH2cO +ep77Sz +L4LXqC +eLESID +FmMU9Y +AXWzNa +JDeiTS +0iK5yg +8M82Hk +wPsXqs +4HqdvX +9NTGrC +1vcmsr +dwbtWU +FspcuB +Xjvt6j +cWU8nc +MVcDlL +Rscydc +dAPTb3 +tFsY5X +scplD2 +GlcOIC +CF5iYb +4enhxh +G8JHXo +C4Hg1m +TRpJyp +u70BSQ +kxzpHM +27rbMo +wbfhKe +4AtXQf +msXapJ +hDwi45 +O94zet +Fn9iL2 +DaGRIW +8e7Nje +fuGWr7 +PgNyBh +aodJiZ +1yAqTt +asySdM +cxyVRp +pzQihk +4kdUWF +QjbSSg +vx0nSu +QW7CHb +hLHU6K +tjlpCA +a7myrr +CEhXox +II8Zis +OfeFPS +Kufn4p +raYXEj +P30UDq +NCGjuS +rjly2n +R9VVhY +U2zUJa +WqpCvB +S3e1jC +VRlvgM +7JNuxW +3dQS6n +y4EBt2 +WHMnuj +v17BOt +MgSqcA +tFlG84 +3t6bOM +MmEoos +VYyZZe +8zcayM +uhAisc +kNJP5r +gVxHKY +DyrX3j +ZGVVaw +5sYHyW +YvbBJh +wBrtLl +9vcOIC +Twww7u +9KFuQS +fc8ew9 +ibtWFT +CZMwx1 +KThLy6 +oV3bPx +TJP2qN +xC9k4V +lopHZW +C3rSPX +JpWpmS +iOTmZK +Rx7fAL +IWo6Nw +78ky9O +ZFDtTg +Sya00v +mi3JiK +xFL6qr +mkvuVp +27Xhbj +7VP3pN +NEex7s +GFtkQh +eI4ZHD +1md6Ic +zqXka7 +WJ6Z1Q +D8o2Hm +ti9GAm +yx7Qza +mHpgLm +scqKkT +2nXgJk +McuxGq +h81m6b +YQwNEz +5AbGOk +cjuNpX +k2eZXi +hjifpP +VdA8kn +8YodQm +PbNST6 +ZhuBzC +z8n3pW +Hf4QeY +gZKugx +EUaHan +qeRz11 +dpXPow +Iz8KI1 +FZsGYk +4t0DRM +ILroFm +jkGSdF +m8duxP +BjguYn +ceKOHq +XqFIQK +e5kwXM +MN7Pbt +BOtcpc +TRPtgu +Ye02Oa +utKUZa +FRAyEJ +5Fx6XB +zZznzk +FxhJMy +WcF9WX +2aUaWN +alVYGH +OH4VpS +U3GDTz +3pvi9S +L6TA6F +XUhihY +8v65Cm +tWogs3 +NXAc3j +D0oJ3I +C8Zv52 +Hte3mo +4sOLRT +BFUtkA +aZtMED +oEb8il +jO9Eu5 +0Hj646 +9r1X2g +hGnNVd +srGsiQ +Skja0g +fkFIMY +IiOAp5 +PVGMn6 +8k6c8e +a49vkH +oH8CEw +ivQNAl +d2nUpu +ihkj27 +P19Fj8 +SidKm8 +8LXLPq +xQwvLp +hdop4B +MbHqZK diff --git a/0004/0004.py b/0004/0004.py new file mode 100644 index 0000000000..95a32e0fa2 --- /dev/null +++ b/0004/0004.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- + +'''任一个英文的纯文本文件,统计其中的单词出现的个数''' + +import re + +def word_counter(filename): + with open(filename) as f: + text=f.read() + #匹配文本中的单词 + words=re.findall(r'[a-zA-Z]+',text) + #存放单词及其出现次数的字典 + result={} + #遍历每个单词并计数 + for word in words: + result.setdefault(word,0) + result[word]+=1 + print result + +if __name__=='__main__': + word_counter('input_text.txt') diff --git a/0004/input_text.txt b/0004/input_text.txt new file mode 100644 index 0000000000..da4e48e54b --- /dev/null +++ b/0004/input_text.txt @@ -0,0 +1,2 @@ + As food is to the body, so is learning to the mind. Our bodies grow and muscles develop with the intake of adequate nutritious food. Likewise, we should keep learning day by day to maintain our keen mental power and expand our intellectual capacity. Constant learning supplies us with inexhaustible fuel for driving us to sharpen our power of reasoning, analysis, and judgment. Learning incessantly is the surest way to keep pace with the times in the information age, and an infallible warrant of success in times of uncertainty. + \ No newline at end of file diff --git a/0005/0005.py b/0005/0005.py new file mode 100644 index 0000000000..ff4bf8a8d6 --- /dev/null +++ b/0005/0005.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +''' +你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 +iPhone5 分辨率为(1136,640) +''' +from PIL import Image +import glob + +#得到指定目录下所有图片的列表 +def get_imagelist(): + return glob.glob(r'C:\Users\Administrator\Pictures\*.jpg') + +#图片处理 +def image_process(image,num,size=(1136,640)): + img=Image.open(image) + sizex,sizey=img.size + if sizex > size[0]: + sizex=size[0] + if sizey > size[1]: + xizey=size[1] + img=img.resize((sizex,sizey)) + num=str(num) + img.save(num+'.jpg','jpeg') + +#主函数 +def main(): + image_list=get_imagelist() + n=1 + for image in image_list: + image_process(image,n) + n+=1 + +if __name__=='__main__': + main() diff --git a/0006/0006.py b/0006/0006.py new file mode 100644 index 0000000000..df5a49fbbf --- /dev/null +++ b/0006/0006.py @@ -0,0 +1,40 @@ +#!/bin/env python +# -*- coding: utf-8 -*- + +'''你有一个目录,放了你一个月的日记,都是 txt, + 假设内容都是英文,请统计出你认为每篇日记最重要的词''' + +import re,glob + +#得到当前目录里所有日记的列表 +def get_bloglist(): + return glob.glob('*.txt') + +#单词计数函数 +def word_counter(blog): + #用来存放单词及其出现次数的字典 + counter={} + with open(blog,'r') as f: + article=f.read() + #得到日记中所以的单词 + words=re.findall(r'[\w]+',article) + #记录每个单词并计数 + for word in words: + counter.setdefault(word,0) + counter[word]+=1 + #转换字典的键-值,即键为次数,值为单词 + counter={counter[key]:key for key in counter} + #将出现次数最多的单词及其出现次数以元组形式返回 + return (counter[max(counter)],max(counter)) + +#主函数 +def main(): + n=1 + #遍历每篇日记 + for blog in get_bloglist(): + word,times=word_counter(blog) + print '第%d篇blog中出现最多的单词为:'%n,word,str(times)+'次' + n+=1 + +if __name__=='__main__': + main() diff --git a/0006/a.txt b/0006/a.txt new file mode 100644 index 0000000000..da4e48e54b --- /dev/null +++ b/0006/a.txt @@ -0,0 +1,2 @@ + As food is to the body, so is learning to the mind. Our bodies grow and muscles develop with the intake of adequate nutritious food. Likewise, we should keep learning day by day to maintain our keen mental power and expand our intellectual capacity. Constant learning supplies us with inexhaustible fuel for driving us to sharpen our power of reasoning, analysis, and judgment. Learning incessantly is the surest way to keep pace with the times in the information age, and an infallible warrant of success in times of uncertainty. + \ No newline at end of file diff --git a/0006/b.txt b/0006/b.txt new file mode 100644 index 0000000000..c3369848d2 --- /dev/null +++ b/0006/b.txt @@ -0,0 +1,27 @@ +every night in my dreams +i see you, i feel you, +that is how i know you go on +far across the distance +and spaces between us +you have come to show you go on +near, far, wherever you are +i believe that the heart does go on +once more you open the door +and you're here in my heart +and my heart will go on and on +love can touch us one time +and last for a lifetime +and never let go till we're one +love was when i loved you +one true time i hold to +in my life we'll always go on +near, far, wherever you are +i believe that the heart does go on +once more you open the door +and you're here in my heart +and my heart will go on and on +there is some love that will not go away +you're here, there's nothing i fear, +and i know that my heart will go on +we'll stay forever this way +you are safe in my heart diff --git a/0006/c.txt b/0006/c.txt new file mode 100644 index 0000000000..b9a719097a --- /dev/null +++ b/0006/c.txt @@ -0,0 +1,4 @@ +There are few opportunities for me to physically go out of my city for sightseeing, and even fewer to take part in a tourist group. Such an occasion came when I was invited to a day trip to Wujiaqu, or Five Family Creek, a new farm-turned city 32 km away from Urumqi to which Id never been before. Without the least hesitation I accepted the invitation. + +It was an outing organized by the local disabled persons federation. A 30-strong party of special citizens and their caretakers, it was a special group consisting of people with cerebral palsy, polio, and permanent spinal cord injuries. A few sat in wheelchairs, some leaned on crutches, and still some limped around with their heads and hands turning and wringing at odd angles. They could create an attraction unique in its own! But each and every one of them wore a happy face and talked animatedly with one another as I joined the lot on April 30, 2009. + diff --git a/0007/0007.py b/0007/0007.py new file mode 100644 index 0000000000..8a24d90d89 --- /dev/null +++ b/0007/0007.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +#-*- coding:utf-8 -*- +""" +有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。空行和注释要分别列出来。 +""" +import glob +import os + +def get_file_pathes(): + return glob.glob('*.py') + +def codes_count(path): + code_line=comment_line=blank_line=0 + with open(path,'r') as f: + lines=f.readlines() + flag=False + for line in lines: + #如果该行为空行 + if not line.strip(): + blank_line+=1 + + #如果为多行注释,则将flag置”1“ + elif line.strip().startswith(('\"\"\"',"\'\'\'")) and not flag: + comment_line+=1 + flag=True + continue + #如果为多行注释的末行,则把flag置”0“ + elif line.strip().endswith(('\"\"\"',"\'\'\'")) and flag: + comment_line+=1 + flag=False + + #如果该行为单行注释,或者多行注释里的内容 + elif line.strip().startswith('#') or flag : + comment_line+=1 + #否则为代码行 + else: + code_line+=1 + return code_line,comment_line,blank_line + +def main(): + for path in get_file_pathes(): + #得到文件的名字 + file_name=os.path.basename(path) + results=codes_count(path) + print '在%s文件中,代码行数为:%d 注释行数为:%d 空行数为:%d'%(file_name,results[0],results[1],results[2]) + +if __name__=='__main__': + main() diff --git a/0007/codes/codes1.py b/0007/codes/codes1.py new file mode 100644 index 0000000000..74638911a3 --- /dev/null +++ b/0007/codes/codes1.py @@ -0,0 +1,39 @@ +#!/bin/env python +# -*- coding: utf-8 -*- + +'''你有一个目录,放了你一个月的日记,都是 txt, + 假设内容都是英文,请统计出你认为每篇日记最重要的词''' + +import re,glob + +#得到当前目录里所有日记的列表 +def get_bloglist(): + return glob.glob('*.txt') + +#单词计数函数 +def word_counter(blog): + #用来存放单词及其出现次数的字典 + counter={} + with open(blog,'r') as f: + article=f.read() + #得到日记中所以的单词,以列表形式返回 + words=re.findall(r'[\w]+',article) + #记录每个单词并计数 + for word in words: + counter.setdefault(word,0) + counter[word]+=1 + #转换字典的键-值,即键为次数,值为单词 + counter={counter[key]:key for key in counter} + #将出现次数最多的单词及其出现次数以元组形式返回 + return (counter[max(counter)],max(counter)) + +#主函数 +def main(): + n=1 + for blog in get_bloglist(): + word,times=word_counter(blog) + print '第%d篇blog中出现最多的单词为:'%n,word,str(times)+'次' + n+=1 + +if __name__=='__main__': + main() diff --git a/0007/codes/codes2.py b/0007/codes/codes2.py new file mode 100644 index 0000000000..cb9201a7f3 --- /dev/null +++ b/0007/codes/codes2.py @@ -0,0 +1,109 @@ +# -*- coding:utf-8 -*- +import urllib +import urllib2 +import re +import thread +import time + +class QSBK: + + #初始化方法,定义一些变量 + def __init__(self): + self.pageIndex = 1 + self.user_agent = 'Mozilla/5.0 (Windows NT 6.1)' + #初始化headers + self.headers = { 'User-Agent' : self.user_agent } + #存放段子的变量,每一个元素是每一页的段子们 + self.stories = [] + #存放程序是否继续运行的变量 + self.enable = False + #传入某一页的索引获得页面代码 + def getPage(self,pageIndex): + try: + url = 'http://www.qiushibaike.com/hot/page/' + str(pageIndex) + #构建请求的request + request = urllib2.Request(url,headers = self.headers) + #利用urlopen获取页面代码 + response = urllib2.urlopen(request) + #将页面转化为UTF-8编码 + pageCode = response.read().decode('utf-8') + return pageCode + + except urllib2.URLError, e: + if hasattr(e,"reason"): + print u"连接糗事百科失败,错误原因",e.reason + return None + + #传入某一页代码,返回本页不带图片的段子列表 + def getPageItems(self,pageIndex): + pageCode = self.getPage(pageIndex) + if not pageCode: + print "页面加载失败...." + return None + pattern = re.compile('