Skip to content

Commit 431aac7

Browse files
committed
solved ex0003 and ex0005
1 parent 47229d9 commit 431aac7

7 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎GitRangerLiu/0004/count_word.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#count the frequency of each word or number( namely not space character)
2+
import string, re
3+
4+
5+
def count_word(filename):
6+
f = open(filename, 'rb')
7+
wdic = {}
8+
for line in f:
9+
line = line.lower().strip()
10+
#split by regular expression, more powerful function
11+
words = re.split('\W+', line)
12+
#words = re.findall('[a-zA-Z0-9]+', line)
13+
14+
for word in words:
15+
if word in wdic:
16+
wdic[word] += 1
17+
else:
18+
wdic[word] = 1
19+
f.close()
20+
#sort by frequency
21+
#Returns a new sorted list with (key, value) pairs, dict cannot be sorted
22+
wdic = sorted(wdic.items(), key = lambda x: x[1], reverse = True)
23+
print wdic
24+
25+
if __name__ == '__main__':
26+
filename = 'testtext.txt'
27+
count_word(filename)

‎GitRangerLiu/0004/testtext.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
where the optional sign may by either + or -, integer and fraction are strings of hexadecimal digits, and exponent is a decimal integer with an optional leading sign. Case is not significant, and there must be at least one hexadecimal digit in either the integer or the fraction. This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. In particular, the output of float.hex() is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C��s %a format character or Java��s Double.toHexString are accepted by float.fromhex().
2+
To read a file��s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it��s your problem if the file is twice as large as your machine��s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").
3+
15, 13, 22,
4+
hello, python

‎GitRangerLiu/0005/cat2.jpg‎

11.7 KB
Loading

‎GitRangerLiu/0005/change_size.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from PIL import Image
2+
import os
3+
from os.path import join
4+
5+
#iPhone 5 display resolution
6+
i5size = (1136, 640)
7+
def change_size(picdir):
8+
9+
for root, dirs, files in os.walk(picdir):
10+
for name in files:
11+
filename = join(root, name)
12+
print filename
13+
change_picsize(filename)
14+
15+
16+
def change_picsize(filename):
17+
im = Image.open(filename)
18+
w = im.width;
19+
h = im.height;
20+
21+
if(w > i5size[0]):
22+
im = im.resize((i5size[0], h))
23+
else:
24+
pass
25+
26+
if(h > i5size[1]):
27+
im = im.resize((im.width, i5size[1]))
28+
else:
29+
pass
30+
im.save(filename.split('.')[0] + '2.jpg')
31+
32+
if __name__ == '__main__':
33+
change_size('test')
34+
35+

‎GitRangerLiu/0005/dog2.jpg‎

101 KB
Loading

‎GitRangerLiu/0005/test/cat.jpg‎

11.7 KB
Loading

‎GitRangerLiu/0005/test/dog.jpg‎

209 KB
Loading

0 commit comments

Comments
 (0)