diff --git a/bit1002lst/0000 b/bit1002lst/0000 new file mode 100644 index 00000000..fcbad5b9 --- /dev/null +++ b/bit1002lst/0000 @@ -0,0 +1,15 @@ +from PIL import Image,ImageDraw,ImageFont + +def addnum(img, num): + myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf') + mydraw = ImageDraw.Draw(img) + fillcolor = "#ff0000" + width, height = img.size + mydraw.text((width-40, 0), str(num), font=myfont, fill=fillcolor) + img.save('saved.jpg', 'jpeg') + + return 0 + +if __name__ == '__main__': + img = Image.open('0.bmp') + addnum(img, 10) diff --git a/bit1002lst/0001 b/bit1002lst/0001 new file mode 100644 index 00000000..6d596804 --- /dev/null +++ b/bit1002lst/0001 @@ -0,0 +1,28 @@ + +#coding=utf-8 +import random +import string + +def get_activation_code(number, length): + + result = {} + source = list(string.ascii_uppercase) + for index in range(0,10): + source.append(str(index)) + + while len(result) < number: + key = '' + for index in range(length): + key += random.choice(source) + if key in result: + pass + else: + result[key] = 1 + + for key in result: + print key + +if __name__ == "__main__": + number = 200 + length = 16 + get_activation_code(number,length) diff --git a/bit1002lst/0004 b/bit1002lst/0004 new file mode 100644 index 00000000..e98821cc --- /dev/null +++ b/bit1002lst/0004 @@ -0,0 +1,42 @@ +#coding:utf-8 +import sys +import re + +def CalcWords1(): + f = open("english.txt", 'r') + + lines = f.read() + words = re.findall(r'[a-zA-Z0-9]+', lines) + return len(words) + +def CalcWords2(): + nums = 0 + f = open("english.txt", 'r') + lines = f.readlines() + for line in lines: + #print line + for word in line.split(): + #print word + nums += 1 + return nums + +def my_calc(): + dic = {} + f = open("english.txt", "r") + art = f.read() + for word in art.split(): + + if dic.has_key(word): + dic[word] = dic[word] + 1 + else: + dic[word] = 1 + print dic.items() + + + + +if __name__ == '__main__': + print CalcWords1() + print CalcWords2() + my_calc() + print len([x for lines in open('english.txt', 'r').readlines() for x in lines.split()]) diff --git a/bit1002lst/0004-txt b/bit1002lst/0004-txt new file mode 100644 index 00000000..570bc239 --- /dev/null +++ b/bit1002lst/0004-txt @@ -0,0 +1 @@ +Now that the city is back in the hands of terrorists, officials "are" diff --git a/bit1002lst/0006/code b/bit1002lst/0006/code new file mode 100644 index 00000000..d20a8b51 --- /dev/null +++ b/bit1002lst/0006/code @@ -0,0 +1,29 @@ +import os + +dairy_path = "dairy" + +def main(): + if os.path.exists(dairy_path): + for f in os.listdir(dairy_path): + art = open(os.path.join(dairy_path, f), 'r') + dict_tmp = {} + content = art.read() + for word in content.split(): + if dict_tmp.has_key(word): + dict_tmp[word] = dict_tmp[word] + 1 + else: + dict_tmp[word] = 1 + max_num = 0 + max_word = "" + for key in dict_tmp: + + if dict_tmp[key] > max_num: + max_num = dict_tmp[key] + max_word = key + print "the max word in "+f+" is "+max_word + print dict_tmp[max_word] + + + +if __name__ == '__main__': + main() diff --git a/bit1002lst/0007 b/bit1002lst/0007 new file mode 100644 index 00000000..77eeedf0 --- /dev/null +++ b/bit1002lst/0007 @@ -0,0 +1,81 @@ +#coding=utf-8 +#代码情况 +#1——纯代码 +#2——空行 +#3——注释 +#4——连续注释 +#5——代码+注释 + +import os +import re + +def main(): + if os.path.exists(code_path): + for f in os.listdir(code_path): + if ".cpp" in f: + my_code = open(os.path.join(code_path, f)) + lines = my_code.readlines() + num_empty = 0 + pattern_empty = re.compile(r'\s') + num_code = 0 + num_zhushi = 0 + num_line = 0 + pattern_zhushi_1 = re.compile(r'//') + pattern_zhushi_2 = re.compile(r'/A *//') + pattern_zhushi_3 = re.compile(r'/\*') + pattern_zhushi_4 = re.compile(r'/A */\*') + pattern_zhushi_5 = re.compile(r'\*/') + pattern_zhushi_6 = re.compile(r'\*//Z') + is_zhushi = False + for line in lines: + num_line += 1 + if is_zhushi: + num_zhushi += 1 + if pattern_zhushi_6.match(line): + is_zhushi = False + continue + else: + is_zhushi = False + num_code += 1 + continue + continue + else: + #if pattern_empty.match(line): + if len(line.split()) == 0: + num_empty += 1 + continue + elif pattern_zhushi_1.match(line): + if pattern_zhushi_2.match(line): + num_zhushi += 1 + continue + else: + num_code += 1 + num_zhushi += 1 + continue + elif pattern_zhushi_3.match(line): + is_zhushi = True + if pattern_zhushi_4.match(line): + num_zhushi += 1 + continue + else: + num_code += 1 + num_zhushi += 1 + continue + else: + num_code += 1 + + + + + else: + print "wrong path" + print "code = ", num_code + print "empty = " , num_empty + print "zhushi = " , num_zhushi + print "line = ", num_line + + +code_path = "code" + +if __name__ == '__main__': + main() diff --git a/bit1002lst/0008 b/bit1002lst/0008 new file mode 100644 index 00000000..8102af89 --- /dev/null +++ b/bit1002lst/0008 @@ -0,0 +1,36 @@ +#coding=utf-8 + +from HTMLParser import HTMLParser +from re import sub +import urllib2 +#for chinese +import sys +reload(sys) +sys.setdefaultencoding('utf-8') + +class my_parser(HTMLParser): + def __init__(self): + self.body = '' + self.processing = None + HTMLParser.__init__(self) + + def handle_starttag(self, tag, attrs): + if tag == 'body': + self.processing = 'body' + pass + + def handle_data(self, data): + if self.processing: + self.body += data + pass + + def handle_endtag(self, tag): + if tag == self.processing: + self.processing = None + print "the body in this html is:" + print self.body + +if __name__ == '__main__': + url = "http://www.bbc.com/" + html = urllib2.urlopen(url).read() + tp.feed(html) diff --git a/bit1002lst/0009 b/bit1002lst/0009 new file mode 100644 index 00000000..2c977c22 --- /dev/null +++ b/bit1002lst/0009 @@ -0,0 +1,32 @@ +#coding=utf-8 +from HTMLParser import HTMLParser +from re import sub +import urllib2 +#for chinese +import sys +reload(sys) +sys.setdefaultencoding('utf-8') + +class my_parser(HTMLParser): + def __init__(self): + + HTMLParser.__init__(self) + self.http_link = "" + self.other_link = "" + + def handle_starttag(self, tag, attrs): + for key, value in attrs: + if key == 'href' : + if "http" in value: + self.http_link += (value + '\n') + else: + self.other_link += (value + '\n') + + +if __name__ == '__main__': + tp = my_parser() + url = "http://www.bbc.com/" + html = urllib2.urlopen(url).read() + tp.feed(html) + print tp.http_link + print tp.other_link diff --git a/bit1002lst/0010 b/bit1002lst/0010 new file mode 100644 index 00000000..482c7853 --- /dev/null +++ b/bit1002lst/0010 @@ -0,0 +1,37 @@ +from random import choice +from PIL import Image +from PIL import ImageFont +from PIL import ImageFilter +from PIL import ImageDraw +import string + +def generate_random_letter(): + my_str = "" + + for x in range(4): + my_str += choice(string.ascii_uppercase) + + img = Image.new('RGB', (240, 60)) + my_font = ImageFont.truetype('DejaVuSansMono-Bold.ttf', 36) + my_draw = ImageDraw.Draw(img) + + for y in range(240): + for x in range(60): + my_draw.point((y, x), tuple([choice(range(128,255)) for color in range(3)])) + #my_draw.point((x,y), tuple([choice(range(128, 255) for color in range(3))])) + pass + pass + + for index in range(len(my_str)): + my_draw.text((60 * index + 10, 10), my_str[index], tuple([choice(range(32, 127)) for color in range(3)]), my_font) + + img = img.filter(ImageFilter.BLUR) + img.show() + + + +if __name__ == '__main__': + print tuple([choice(range(128,255)) for color in range(3)]) + generate_random_letter() + + diff --git a/bit1002lst/0011/min_gan_ci b/bit1002lst/0011/min_gan_ci new file mode 100644 index 00000000..9bcb9ec1 --- /dev/null +++ b/bit1002lst/0011/min_gan_ci @@ -0,0 +1,29 @@ +#coding=utf-8 + +import os + + +name_txt = "min_gan_ci.txt" +def filter_word(): + my_txt = open(name_txt, "r") + my_list = [] + for word in my_txt.readlines(): + my_list.append(word.split()) + #print my_list + + while(1): + content = raw_input("input_please: ") + if content == "exist": + break + else: + str_show = "human right" + for word_tmp in range(len(my_list)): + #print my_list[word_tmp] + if "".join(my_list[word_tmp]) in content: + str_show = "freedom" + break + + print str_show + +if __name__ == '__main__': + filter_word() diff --git a/bit1002lst/0012 b/bit1002lst/0012 new file mode 100644 index 00000000..5d8edd83 --- /dev/null +++ b/bit1002lst/0012 @@ -0,0 +1,29 @@ +#coding=utf-8 + +import os + +name_txt = "min_gan_ci.txt" +def filter_word(): + my_txt = open(name_txt, "r") + my_list = [] + for word in my_txt.readlines(): + my_list.append(word.split()) + + while(1): + content = raw_input("input_please: ") + if content == "exist": + break + else: + str_show = "human right" + for word_tmp in range(len(my_list)): + while "".join(my_list[word_tmp]) in content: + index = content.find("".join(my_list[word_tmp])) + if index == -1: + break + content_new = content[0:index] + "*"*(len("".join(my_list[word_tmp]))) + content[index+len("".join(my_list[word_tmp])):] + content = content_new + print content + + +if __name__ == '__main__': + filter_word() diff --git a/bit1002lst/0013 b/bit1002lst/0013 new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/bit1002lst/0013 @@ -0,0 +1 @@ + diff --git a/bit1002lst/0014 b/bit1002lst/0014 new file mode 100644 index 00000000..e631a1c7 --- /dev/null +++ b/bit1002lst/0014 @@ -0,0 +1,35 @@ +import sys + +reload(sys) + +sys.setdefaultencoding('utf8') + +from HTMLParser import HTMLParser +import urllib2 + +class my_parser(HTMLParser): + def __init__(self): + HTMLParser.__init__(self) + self.pic_url = [] + def handle_starttag(self, tag, attrs): + if tag == "img": + if(len(attrs) > 0): + print attrs + for key, value in attrs: + if key == "src": + self.pic_url.append(value) + +def save_pic(pic_url): + count = 1 + for tmp_url in pic_url: + tmp = urllib2.urlopen(tmp_url) + f = open(str(count) + ".jpg", "wb") + f.write(tmp.read()) + f.close() + count += 1 + +if __name__ == '__main__': + html = urllib2.urlopen("http://tieba.baidu.com/p/2166231880").read() + MyParser = my_parser() + MyParser.feed(html) + save_pic(MyParser.pic_url)