Skip to content

Commit 64db1e4

Browse files
committed
Merge pull request Show-Me-the-Code#148 from Jaccorot/master
add 004-009
2 parents ae35c08 + d48a595 commit 64db1e4

17 files changed

Lines changed: 2210 additions & 0 deletions

Jaccorot/0004/0004.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/local/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。
6+
"""
7+
import re
8+
9+
def count_the_words(path):
10+
with open(path) as f:
11+
text = f.read()
12+
words_list = re.findall(r'[a-zA-Z0-9]+', text)
13+
count = len(words_list)
14+
return count
15+
16+
if __name__ == '__main__':
17+
nums = count_the_words('file.txt')
18+
print nums

Jaccorot/0004/file.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cocococo asdf asdf asdf sdf
2+
sdf
3+
sdf

Jaccorot/0005/0.jpg

26.1 KB
Loading

Jaccorot/0005/0005.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/local/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
6+
"""
7+
import os
8+
from PIL import Image
9+
10+
iPhone5_WIDTH = 1136
11+
iPhone5_HEIGHT = 640
12+
13+
def resize_iPhone5_pic(path, new_path, width=iPhone5_WIDTH, height=iPhone5_HEIGHT):
14+
im = Image.open(path)
15+
w,h = im.size
16+
17+
if w > width:
18+
h = width * h // w
19+
w = width
20+
if h > height:
21+
w = height * w // h
22+
h = height
23+
24+
im_resized = im.resize((w,h), Image.ANTIALIAS)
25+
im_resized.save(new_path)
26+
27+
28+
def walk_dir_and_resize(path):
29+
for root, dirs, files in os.walk(path):
30+
for f_name in files:
31+
if f_name.lower().endswith('jpg'):
32+
path_dst = os.path.join(root,f_name)
33+
f_new_name = 'iPhone5_' + f_name
34+
resize_iPhone5_pic(path=path_dst, new_path=f_new_name)
35+
36+
if __name__ == '__main__':
37+
walk_dir_and_resize('./')

Jaccorot/0006/0006.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!usr/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,
6+
假设内容都是英文,请统计出你认为每篇日记最重要的词。
7+
"""
8+
9+
import os
10+
import re
11+
12+
def walk_dir(path):
13+
file_path = []
14+
for root, dirs, files in os.walk(path):
15+
for f in files:
16+
if f.lower().endswith('txt'):
17+
file_path.append(os.path.join(root, f))
18+
return file_path
19+
20+
def find_key_word(filepath):
21+
word_dic = {}
22+
filename = os.path.basename(filepath)
23+
with open(filepath) as f:
24+
text = f.read()
25+
words_list = re.findall(r'[A-Za-z]+', text.lower())
26+
for w in words_list:
27+
if w in word_dic:
28+
word_dic[w] += 1
29+
else:
30+
word_dic[w] = 1
31+
sorted_word_list = sorted(word_dic.items(), key=lambda d: d[1])
32+
print u"在%s文件中,%s为关键词,共出现了%s次" %(filename, sorted_word_list[-1][0], sorted_word_list[-1][1])
33+
34+
if __name__ == "__main__":
35+
for file_path in walk_dir(os.getcwd()):
36+
find_key_word(file_path)
37+

Jaccorot/0006/1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a b c d a a d

Jaccorot/0006/2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a b c d a a d d b d d x zx fd fd fd fd fd fd fd fd fd FD FD FD FD D FD FD FD FD fD FD FD FD

Jaccorot/0007/0007.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
"""
5+
第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
6+
"""
7+
8+
import os
9+
10+
def walk_dir(path):
11+
file_path = []
12+
for root, dirs, files in os.walk(path):
13+
for f in files:
14+
if f.lower().endswith('py'):
15+
file_path.append(os.path.join(root, f))
16+
return file_path
17+
18+
19+
def count_the_code(path):
20+
file_name = os.path.basename(path)
21+
note_flag = False
22+
line_num = 0
23+
empty_line_num = 0
24+
note_num = 0
25+
with open(path) as f:
26+
for line in f.read().split('\n'):
27+
line_num += 1
28+
if line.strip().startswith('\"\"\"') and not note_flag:
29+
note_flag =True
30+
note_num += 1
31+
continue
32+
33+
if line.strip().startswith('\"\"\"'):
34+
note_flag = False
35+
note_num += 1
36+
37+
if line.strip().startswith('#') or note_flag:
38+
note_num += 1
39+
40+
if len(line) == 0:
41+
empty_line_num += 1
42+
43+
print u"在%s中,共有%s行代码,其中有%s空行,有%s注释"% (file_name, line_num, empty_line_num, note_num)
44+
45+
46+
if __name__ == '__main__':
47+
for f in walk_dir('.'):
48+
count_the_code(f)

Jaccorot/0008/0008.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0008 题:一个HTML文件,找出里面的正文。
6+
"""
7+
8+
from bs4 import BeautifulSoup
9+
10+
def find_the_content(path):
11+
with open(path) as f:
12+
text = BeautifulSoup(f, 'lxml')
13+
content = text.get_text().strip('\n')
14+
15+
return content.encode('gbk','ignore')
16+
17+
18+
if __name__ == '__main__':
19+
print find_the_content('Show-Me-the-Code_show-me-the-code_1.html')

0 commit comments

Comments
 (0)