Skip to content

Commit c560a24

Browse files
author
Kevin Li
committed
update: 2014wk04
1 parent fd66193 commit c560a24

27 files changed

+693
-2
lines changed

Aglorithm/data_unpack_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
__author__ = 'k22li'
2+
3+
contact = ('Li', 'Kevin', 'Nokia', '87111870', '139111134736')
4+
5+
surName, firName, Comp, *phones = contact
6+
7+
for item in phones:
8+
print item
9+
10+
11+
#print _

Aglorithm/python_digits_round.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
__author__ = 'k22li'
2+
3+
print round(1/3)
4+
5+
print round(20/3)
6+
7+
8+
print int(1/3)
9+
10+
print int(20/3)
11+
12+
print round(2/3)
13+
14+
15+
print 2/round(int(20/3), 2)
16+
17+
print '_-'*50, hash('test')

Aglorithm/python_get_median.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#-*- coding: utf-8 -*-
2+
__author__ = 'k22li'
3+
__doc__ = """
4+
题目:现在有两个排好序的整数数组,a[N]和b[N],要求写一个函数,功能为返回两个数组中第N大数和第N+1大数的中间值,即求解两者的和除以2。
5+
6+
函数原型:double getMedian( int a[], int b[] );
7+
8+
下面,我们先来分析一个类似的问题,假设a和b都是升序的,分别有n1和n2个元素,求两个数组合并后第k大元素值。
9+
10+
分别取两个数组中间索引的数,a[x]和b[y],比较两个数的大小:
11+
12+
if( a[x] <= a[y] )
13+
14+
——————————————————————————————————————————————————————————————
15+
16+
如果k <= x+y+1,则可以判断出b[y]以及b[y]后面的元素都可以排除在外,减小搜索规模。
17+
18+
如果k > x+y+1,则可以判断出a数组的前半部分元素都不符合条件,减少a一半的搜索规模。
19+
20+
该算法利用了递归的思想,结束条件是:
21+
22+
a中元素排除出去,则选择b中得第k大元素;
23+
24+
b中元素全部排除,选择a中第k大元素。
25+
26+
——————————————————————————————————————————————————————————————
27+
"""
28+
import random
29+
30+
def generateTestList(length = 0):
31+
tmpList = []
32+
if length:
33+
for i in range(length):
34+
tmpList.append(random.randint(1, 10))
35+
return tmpList
36+
37+
def getMedianFigure(listA, listB, n):
38+
listA.extend(listB)
39+
listA.sort()
40+
return listA[n-1], listA
41+
42+
43+
if __name__ == '__main__':
44+
listA = generateTestList(10)
45+
listB = generateTestList(10)
46+
print getMedianFigure(listA, listB, 10)
47+

Aglorithm/python_heapq_usage.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#-*- coding: utf-8 -*-
2+
3+
__author__ = 'k22li'
4+
5+
#
6+
#需1求:给出N长的序列,求出TopK大的元素,使用小顶堆,heapq模块实现。
7+
#
8+
9+
import heapq
10+
import random
11+
12+
class TopkHeap(object):
13+
14+
def __init__(self, k):
15+
self.k = k
16+
self.data = []
17+
18+
def Push(self, elem):
19+
if len(self.data) < self.k:
20+
heapq.heappush(self.data, elem)
21+
else:
22+
topk_small = self.data[0]
23+
24+
if elem > topk_small:
25+
heapq.heapreplace(self.data, elem)
26+
27+
def TopK(self):
28+
# return [x for x in reversed([heapq.heappop(self.data) for x in xrange(len(self.data))])]
29+
return reversed([heapq.heappop(self.data) for x in xrange(len(self.data))])
30+
31+
if __name__ == "__main__":
32+
print "Hello"
33+
list_rand = random.sample(xrange(1000000), 100)
34+
th = TopkHeap(3)
35+
for i in list_rand:
36+
th.Push(i)
37+
print '*'*30, th.data
38+
print th.TopK()
39+
print sorted(list_rand, reverse=True)[0:3]
40+
print sorted(list_rand, reverse=True)[:]

Aglorithm/python_iterator_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
__author__ = 'k22li'
2+
3+
def yieldTest(length = 0):
4+
5+
for i in range(length):
6+
yield i * 3
7+
8+
9+
if __name__ == '__main__':
10+
n = 10
11+
k = yieldTest(n)
12+
13+
print k
14+
15+
for i in range(n+1):
16+
17+
try:
18+
print k.next()
19+
except StopIteration as e:
20+
print 'failed to iterating, reason: %s' %e

Logs/nst_testplan_generator.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
__author__ = 'k22li'
2+
3+
import re
4+
import os
5+
import os.path as Path
6+
7+
PATH_TO_TEST=r'C:\Users\k22li\workspace\bsta\bsta\cases\test'
8+
TARGET_PATH=r'/home/k22li/N/NST_Home/NSTRunner/cases/test'
9+
10+
11+
def listAllFiles(path):
12+
13+
if os.path.isdir(path):
14+
return os.listdir(path)
15+
else:
16+
return ''
17+
18+
def removeNonPythonScripts(fileList):
19+
20+
if '__init__.py' in fileList:
21+
fileList.remove('__init__.py')
22+
return fileList
23+
24+
def caseFilter(fileName):
25+
26+
patten_gourNo = re.compile('self._grouNO = \'(\d*)\'')
27+
patten_caseNo = re.compile('self._caseNO = \'(\d*)\'')
28+
29+
file = Path.join(PATH_TO_TEST, fileName)
30+
if not Path.isfile(file):
31+
raise BaseException, 'Invalid file path provided!'
32+
else:
33+
with open(file, 'r+') as filer:
34+
for line in filer.readlines():
35+
if re.search(patten_gourNo, line):
36+
groupNo = re.search(patten_gourNo, line).group(1)
37+
elif re.search(patten_caseNo, line):
38+
caseNo = re.search(patten_caseNo, line).group(1)
39+
realPath=os.path.join(TARGET_PATH, fileName)
40+
return groupNo, caseNo, realPath
41+
42+
def composeTestPlan(caseDict, whiteList):
43+
44+
template='<testcase no="%s" name="" location="%s" refPhone = ""/>'
45+
46+
prefix ="""
47+
<?xml version="1.0" encoding="utf-8"?>
48+
<testplan assignTo="a030623e,b3b9521d,1a2215a" resultPath="/home/k22li/N/NST_Home/TestResult" syncQRDLog="true">
49+
<target-device hwversion="" osversion="" deviceid=""/>
50+
<testtask id="2" type="monkeyrunner" timeout="120">"""
51+
52+
postfix = """ </testtask>
53+
</testplan>"""
54+
print prefix
55+
for case in cases.items():
56+
if whiteList:
57+
if case[0] in whiteList:
58+
print '\t\t'+template %(case[0], case[1])
59+
else:
60+
print '\t\t'+template %(case[0], case[1])
61+
print postfix
62+
63+
if __name__ == '__main__':
64+
cases = {}
65+
whiteList = ['5900.315', '5104.895', '5104.894', '5104.818', '5104.817', '5104.851', '5900.210', '5104.851', \
66+
'5104.116', '5104.944', '5104.945', '5104.946', '5104.947', '5104.951', '5104.826']
67+
68+
files = listAllFiles(PATH_TO_TEST)
69+
70+
scriptFiles = removeNonPythonScripts(files)
71+
for file in scriptFiles:
72+
# print file
73+
groupNo, caseNo, filePath = caseFilter(file)
74+
cases.update({groupNo+'.'+caseNo : filePath})
75+
76+
composeTestPlan(cases, whiteList)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
__author__ = 'k22li'
2+
3+
import random
4+
import string
5+
6+
# list the functions/ attributes of random class
7+
for command in dir(random):
8+
print command
9+
10+
# list the functions/ attributes of String class
11+
for item in dir(string):
12+
print item
13+
14+
# test random choice
15+
print random.choice(string.letters)
16+
17+
# test random sample
18+
for i in range(10):
19+
print random.sample(string.letters,5)
20+
21+
# list all lower case letters
22+
print string.lowercase
23+
24+
25+
#print map(lambda x : ''.join(x), random.sample(string.uppercase, 2))
26+
27+
# join the elements from per list
28+
print ''.join(random.sample(string.uppercase, 2))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
__author__ = 'k22li'
2+
3+
4+
import threading
5+
import string, random
6+
7+
class TestClass():
8+
9+
def __init__(self, name, loop):
10+
self.name = name
11+
self.loop = loop
12+
13+
def printOut(self):
14+
15+
print 'Name of the current method is: %s and the loop number is: %d' %(self.name, self.loop)
16+
17+
def run(self):
18+
self.printOut()
19+
20+
21+
if __name__ == '__main__':
22+
23+
# for i in range(5):
24+
name = random.sample(string.letters, 4)
25+
loop = 2
26+
test1 = TestClass(name, loop)
27+
28+
thread1 = threading.Thread(test1)
29+
thread1.start()
1.45 KB
Binary file not shown.

Python_Basic/appen_vs_extend.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
__author__ = 'k22li'
23
"""
34
difference between extend & append

0 commit comments

Comments
 (0)