請編寫函式‘func’, 它返回字串列表中‘k’個相鄰字串中最長的第一個 - 氣泡排序
阿新 • • 發佈:2018-12-02
題,描述:
給定一個字串列表’strlist’和整數‘k’
請編寫函式‘func’, 它返回字串列表中‘k’個相鄰字串中最長的第一個
例項:
func([“this”, “is”, “an”, “example”], 1) --> “example”
func([“this”, “is”, “another”, “example”], 1) --> “another”
func([“this”, “is”, “another”, “example”], 2) --> “anotherexample”
假設字串列表的長度為’n’, 如果’n =0’ 或 ‘k>n’ 或’k <=0’ 則反饋空字串
例項:
func([], 1) --> “”
func([“this”, “is”, “an”, “example”], 5) --> “”
func([“this”, “is”, “an”, “example”], 0) --> “”
def func(strlist, k): your code pass class DefaultTestCase(unittest.TestCase): def test_func(seltf): self.assertEqual(func(["this", "is", "an", "example"], 1), "example") self.assertEqual(func(["this", "is", "another", "example"], 1), "another") self.assertEqual(func(["this", "is", "another", "example"], 2), "anotherexample")
實現
實現思路
- 根據k的大小,基於strlist,重新獲取一個strlist_new – 滿足k個相鄰的條件
比如,
k=1,獲取的新陣列是[‘this’, ‘is’, ‘an’, ‘example’]
k=2,獲取的新陣列是[‘thisis’, ‘isanother’, ‘anotherexample’]
k=3,獲取的新陣列是[‘thisisan’, ‘isanexample’] - 對新陣列strlist_new,以strlist_new列表元素的長度為參考,對strlist進行排序進行氣泡排序 – 滿足長度最長的一個
- 用max函式取出排序後長度最長的元素 – 可以取出第一個最長的字串
具體程式碼,如下:
new_lst
def new_lst(strlist, k):
n = len(strlist)
strlist_new = []
for i in range(n-k+1):
str_temp = ''
for j in range(k):
str_temp = str_temp+strlist[i+j]
strlist_new.append(str_temp)
print strlist_new
return strlist_new
對新陣列進行氣泡排序,並返回列表中最長的字元長的第一個
def func(strlist, k):
strlist = new_lst(strlist, k)
n = len(strlist)
if n==0 or k>n or k<=0:
return ''
for i in range(n-1):
for j in range(n-1-i):
if len(strlist[j]) > len(strlist[j+1]):
strlist[j], strlist[j+1] = strlist[j+1], strlist[j]
return max(strlist, key=len)
test case
class DefaultTestCase(unittest.TestCase):
def test_func(self):
self.assertEqual(func(["this", "is", "an", "example"], 1), "example")
self.assertEqual(func(["this", "is", "another", "example"], 1), "another")
self.assertEqual(func(["this", "is", "another", "example"], 2), "anotherexample")
self.assertEqual(func([], 1), "")
self.assertEqual(func(["this", "is", "an", "example"], 5), "")
self.assertEqual(func(["this", "is", "an", "example"], 0), "")
self.assertEqual(func(["aaaaaaaa", "bbbbbbbb", "c", "dddddddddd", "ee"], 0), "")
self.assertEqual(func(["aaaaaaaa", "bbbbbbbb", "c", "dddddddddd", "ee"], 1), "dddddddddd")
self.assertEqual(func(["aaaaaaaa", "bbbbbbbb", "c", "dddddddddd", "ee"], 2), "aaaaaaaabbbbbbbb")
test
if __name__=='__main__':
# new_lst(["this", "is", "an", "example"],1 )
# new_lst(["this", "is", "an", "example"],2 )
# new_lst(["this", "is", "an", "example"],3 )
# print func(["this", "is", "an", "example"], 1)
# print func(["this", "is", "another", "example"], 1)
# print func(["this", "is", "another", "example"], 2)
# test_case = DefaultTestCase()
# test_case.test_func()
unittest.main()
結果
ssh://[email protected]:22/home/wfq/python27/bin/python -u /home/wfq/ops/test/bubble_sort.py
['this', 'is', 'an', 'example']
['this', 'is', 'another', 'example']
['thisis', 'isanother', 'anotherexample']
[]
[]
['', '', '', '', '']
['', '', '', '', '', '']
['aaaaaaaa', 'bbbbbbbb', 'c', 'dddddddddd', 'ee']
['aaaaaaaabbbbbbbb', 'bbbbbbbbc', 'cdddddddddd', 'ddddddddddee']
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Process finished with exit code 0
c