From 9baef80c79884fab6fce4d53edcfee925bdeb700 Mon Sep 17 00:00:00 2001 From: Forec Date: Mon, 30 Nov 2015 23:54:27 +0800 Subject: [PATCH 1/3] 11.30 solve 0001 --- Forec/0001/0001.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Forec/0001/0001.py diff --git a/Forec/0001/0001.py b/Forec/0001/0001.py new file mode 100644 index 00000000..2b8679d7 --- /dev/null +++ b/Forec/0001/0001.py @@ -0,0 +1,18 @@ +# coding = utf-8 +__author__ = 'forec' +import random + +def make_number( num, length ): + str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' + b = set() + i = 0 + while i < num: + ans = '' + for j in range(length): + ans += random.choice(str) + if ans not in b: + b |= {ans} + i += 1 + print(ans) + +make_number( 200, 10) \ No newline at end of file From def94ffe00976a76a04b6662bc99210702050cca Mon Sep 17 00:00:00 2001 From: Forec Date: Tue, 1 Dec 2015 14:42:03 +0800 Subject: [PATCH 2/3] add --- Forec/0002/0002.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Forec/0004/0004.py | 19 +++++++++++++++++++ Forec/0005/0005.py | 12 ++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 Forec/0002/0002.py create mode 100644 Forec/0004/0004.py create mode 100644 Forec/0005/0005.py diff --git a/Forec/0002/0002.py b/Forec/0002/0002.py new file mode 100644 index 00000000..7d8287b3 --- /dev/null +++ b/Forec/0002/0002.py @@ -0,0 +1,46 @@ +# coding = utf-8 +__author__ = 'Forec' + +# 保随机码至MySQL + +import pymysql +import random + +def make_number( num, length ): + letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' + dic = set() + i = 0 + while i < num: + str = '' + for j in range(length): + str += random.choice(letters) + if str not in dic: + dic |= {str} + i += 1 + else: + continue + return dic + +def save( dic ): + connect = pymysql.connect( + host = '127.0.0.1', + port = 3306, + user = 'root', + passwd = 'VKDARK' + ) + cur = connect.cursor() + try: + __create = 'create database if not exists test' + cur.execute(__create) + except: + print('database create error') + connect.select_db('test') + __create_table = 'create table if not exists codes(code char(64))' + cur.execute(__create_table) + cur.executemany('insert into codes values(%s)',dic) + + connect.commit() + cur.close() + connect.close() + +save(make_number(200,10)) \ No newline at end of file diff --git a/Forec/0004/0004.py b/Forec/0004/0004.py new file mode 100644 index 00000000..9827c883 --- /dev/null +++ b/Forec/0004/0004.py @@ -0,0 +1,19 @@ +import re +with open('input.txt',"r") as f: + data = f.read() + +words = re.compile(r'([a-zA-Z]+)') +dic = {} +for x in words.findall(data): + if x not in dic: + dic[x] = 1 + else: + dic[x] += 1 +L = [] +for k,value in dic.items(): + L.append((k, value)) + +L.sort(key = lambda t:t[0]) + +for x in L: + print( x[0], x[1]) \ No newline at end of file diff --git a/Forec/0005/0005.py b/Forec/0005/0005.py new file mode 100644 index 00000000..ba538f49 --- /dev/null +++ b/Forec/0005/0005.py @@ -0,0 +1,12 @@ +from PIL import Image, ImageOps +import os, os.path + +L = [ x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.jpg' ] + +for x in L: + img = Image.open(x) + xsize, ysize = img.size + xsize = 500 + ysize = ysize * 500 // xsize + img = ImageOps.fit(img,(xsize,ysize)) + img.save("out"+x) \ No newline at end of file From eb2101a5023098f09054772c576ee99dd70a4b5b Mon Sep 17 00:00:00 2001 From: Forec Date: Wed, 2 Dec 2015 00:06:28 +0800 Subject: [PATCH 3/3] add --- Forec/0006/0006.py | 27 +++++++++++++++++++++ Forec/0007/0007.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++ Forec/0009/0009.py | 14 +++++++++++ 3 files changed, 101 insertions(+) create mode 100644 Forec/0006/0006.py create mode 100644 Forec/0007/0007.py create mode 100644 Forec/0009/0009.py diff --git a/Forec/0006/0006.py b/Forec/0006/0006.py new file mode 100644 index 00000000..631a9e1b --- /dev/null +++ b/Forec/0006/0006.py @@ -0,0 +1,27 @@ +# coding = utf-8 +__author__ = 'Forec' +import os, re + +def find_word(file_path): + file_list = os.listdir(file_path) + word_dic = {} + word_re = re.compile(r'[\w]+') + for x in file_list: + if os.path.isfile(x) and os.path.splitext(x)[1] =='.txt' : + try: + f = open(x, 'r') + data = f.read() + f.close() + words = word_re.findall(data) + for word in words: + if word not in word_dic: + word_dic[word] = 1 + else: + word_dic[word] += 1 + except: + print('Open %s Error' % x) + Ans_List = sorted(word_dic.items(), key = lambda t : t[1], reverse = True) + for key, value in Ans_List: + print( 'Word ', key, 'appears %d times' % value ) + +find_word('.') \ No newline at end of file diff --git a/Forec/0007/0007.py b/Forec/0007/0007.py new file mode 100644 index 00000000..be31a125 --- /dev/null +++ b/Forec/0007/0007.py @@ -0,0 +1,60 @@ +# coding = utf-8 +# Can detect .py / .c / .cpp +__author__ = 'Forec' +import os +import re + +def get_line( file_path ): + file_dir = os.listdir(file_path) + code ,exp ,space ,alls = ( 0, 0 ,0 , 0) + cFlag = True + exp_re = re.compile(r'[\"\'].*?[\"\']') + for x in file_dir: + if os.path.isfile(x) and ( + os.path.splitext(x)[1] == '.py' + or os.path.splitext(x)[1] == '.c' + or os.path.splitext(x)[1] == '.cpp' + ): + try: + f = open( x, 'r' ) + except: + print('Cannot open file %s' % x) + for line in f.readlines(): + alls += 1 + if line.strip() == '' and cFlag: + space += 1 + continue + find_exp = exp_re.findall(line) + for strs in find_exp: + line = line.replace(strs,'') + if os.path.splitext(x)[1] == '.py': + if '#' in line: + exp += 1 + else : + code += 1 + elif os.path.splitext(x)[1] == '.c' or os.path.splitext(x)[1] == '.cpp': + if r'*/' in line: + cFlag = True + exp += 1 + elif r'/*' in line: + cFlag = False + exp += 1 + elif not cFlag: + exp += 1 + elif r'//' in line: + exp += 1 + else: + code += 1 + cFlag = True + try: + f.close() + except: + pass + + print( '''All Lines total : %d +Codes total : %d +Spaces total: %d +Explanation : %d''' +% ( alls, code, space, exp )) + +get_line('.') diff --git a/Forec/0009/0009.py b/Forec/0009/0009.py new file mode 100644 index 00000000..77baf946 --- /dev/null +++ b/Forec/0009/0009.py @@ -0,0 +1,14 @@ +#coding = utf-8 +__author__ = 'Forec' + +import requests +import re +from bs4 import BeautifulSoup + +url = input() +html = requests.get(url) + +soup = BeautifulSoup(html.text,"html.parser") +find_href = soup.findAll('a') +for x in find_href: + print(x['href']) \ No newline at end of file