Skip to content

Commit ac44ef6

Browse files
committed
Add solution to 0007
1 parent 2aa3490 commit ac44ef6

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

burun/0007/0007.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
# Date: 17-03-15
4+
# Author: Liang
5+
6+
'''
7+
第 0007 题:
8+
有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来.
9+
'''
10+
11+
'''
12+
Not to specific code files yet, like "py", "cpp" .etc.
13+
Not consider different comment methods like ''' ''' yet.
14+
Not consider sub-directory yet.
15+
'''
16+
17+
import os
18+
19+
# set codes path
20+
codes_path = "codes/"
21+
22+
23+
def line_counter(dir):
24+
codes = os.listdir(dir)
25+
code_lines = 0
26+
empty_lines = 0
27+
comment_lines = 0
28+
for i in codes:
29+
with open(dir + i) as code_file:
30+
codes_lines = code_file.readlines()
31+
32+
for line in codes_lines:
33+
line = line.strip()
34+
if line.startswith("#"):
35+
comment_lines += 1
36+
elif line == "":
37+
empty_lines += 1
38+
else:
39+
code_lines += 1
40+
print("There are " +
41+
str(code_lines) + " code lines, " +
42+
str(comment_lines) + " comment lines, and " +
43+
str(empty_lines) + " empty lines.")
44+
45+
if __name__ == '__main__':
46+
line_counter(codes_path)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
# Date: 22-02-15
4+
# Author: Liang
5+
6+
7+
from PIL import Image, ImageDraw, ImageFont
8+
9+
10+
def add_number(image_path, number):
11+
im = Image.open(image_path)
12+
width, height = im.size
13+
14+
# get a drawing context
15+
avatar_draw = ImageDraw.Draw(im)
16+
# set the font of number to draw
17+
font_size = int(height / 4)
18+
number_font = ImageFont.truetype('microsoft_yahei.TTF', font_size)
19+
20+
# Draw the image with a number(xy, text, color, font)
21+
avatar_draw.text(
22+
(width - font_size, 0), str(number), (255, 0, 0), font=number_font)
23+
24+
im.save('./new_avatar.jpg')
25+
im.show()
26+
27+
28+
add_number('avatar.png', 3)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
# Date: 23-02-15
4+
# Author: Liang
5+
6+
import random
7+
import string
8+
9+
coupon_number = 200
10+
coupon_size = 12
11+
12+
for i in range(coupon_number):
13+
coupon = ''.join(
14+
random.sample(string.digits + string.ascii_uppercase, coupon_size))
15+
print(coupon)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
# Date: 13-03-15
4+
# Author: Liang
5+
6+
import os
7+
import re
8+
9+
# set diaries path
10+
diaries_path = "diaries/"
11+
diaries = os.listdir(diaries_path)
12+
13+
# set stop words to make informative keywords
14+
stop_words = open("Stop Words.txt", 'r').read()
15+
stop_words_list = stop_words.split(" ")
16+
17+
18+
# Find top 5 keywords in a txt
19+
def find_keywords(words):
20+
words_dictionary = {}
21+
for word in words:
22+
if word.lower() not in words_dictionary and word.lower() not in stop_words_list:
23+
# Put word in dictionary
24+
words_dictionary[word] = 0
25+
for item in words:
26+
if item == word:
27+
words_dictionary[word] += 1
28+
# Find 5 keywords which by highest frequency
29+
keywords = sorted(
30+
words_dictionary, key=words_dictionary.__getitem__, reverse=True)[0:5]
31+
return keywords
32+
33+
for diary in diaries:
34+
# Coding by utf-8
35+
with open(diaries_path + diary, "r", encoding='utf-8', errors='ignore') as content:
36+
diary_words_list = re.findall(r"[\w']+", content.read())
37+
print("The keywords of diary " + diary + " is: ", end="")
38+
print(find_keywords(diary_words_list))

0 commit comments

Comments
 (0)