Skip to content

Commit d439b5d

Browse files
committed
solved ex0006 and ex0007
1 parent 431aac7 commit d439b5d

6 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎GitRangerLiu/0006/count_words.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
from os.path import join
3+
import re
4+
5+
6+
def count_words(root):
7+
wdic = {}
8+
for root, dirs, files in os.walk(root):
9+
for name in files:
10+
filename = join(root, name)
11+
12+
f = open(filename, 'rb')
13+
for line in f:
14+
line = line.lower().strip()
15+
words = re.findall('[a-zA-Z0-9]+', line)
16+
for word in words:
17+
if word in wdic:
18+
wdic[word] += 1
19+
else:
20+
wdic[word] = 1
21+
f.close()
22+
wdic = sorted(wdic.items(), key = lambda x: x[1], reverse = True)
23+
print wdic
24+
25+
if __name__ == '__main__':
26+
root = 'test'
27+
count_words(root)

‎GitRangerLiu/0006/test/1.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
one one one
2+
two two
3+
five

‎GitRangerLiu/0006/test/2.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
five
2+
five
3+
five
4+
five

‎GitRangerLiu/0007/count_line.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
from os.path import join
3+
import re
4+
5+
comm = '[(#)(//)(/*)(*)]'
6+
prog = re.compile(comm)
7+
8+
def count_line(root):
9+
codecount = 0
10+
emptycount = 0
11+
commcount = 0
12+
for root, dirs, files in os.walk(root):
13+
for name in files:
14+
filename = join(root, name)
15+
f = open(filename)
16+
for line in f:
17+
if line.strip() == '':
18+
emptycount += 1
19+
#python script
20+
elif prog.match(line.strip()) != None:
21+
commcount += 1
22+
else:
23+
codecount += 1
24+
f.close()
25+
26+
print 'code lines: ', codecount
27+
print 'comment lines: ', commcount
28+
print 'empty lines: ', emptycount
29+
30+
if __name__ == '__main__':
31+
root = 'test'
32+
count_line(root)

‎GitRangerLiu/0007/test/Test.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Test {
2+
public static void main(String[] args) {
3+
//print hello world
4+
/**
5+
*
6+
*/
7+
System.out.println("Hello World!");
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import socket
2+
import SocketServer as socketserver
3+
import select
4+
REMOTE_SERVER = '127.0.0.1'
5+
REMOTE_PORT = 9999
6+
LOCAL_SERVER = '127.0.0.1'
7+
LOCAL_PORT = 10001
8+
PORT = 10000
9+
BUFFER = 4096
10+
#Hello
11+
if __name__ == '__main__':
12+
main()

0 commit comments

Comments
 (0)