求助python编程的题目

1.defcount(val,seq):(用Python写出程序,使程序可以通过下面的doctest)""">>>count(5,(1,5,3,7,5,8,5))3>>>... 1.
def count(val, seq):
(用Python写出程序,使程序可以通过下面的doctest)
"""
>>> count(5, (1, 5, 3, 7, 5, 8, 5))
3
>>> count('s', 'Mississippi')
4
>>> count((1, 2), [1, 5, (1, 2), 7, (1, 2), 8, 5])
2
"""

2.
def reverse(seq):
(用Python写出程序,使程序可以通过下面的doctest)
"""
>>> reverse([1, 2, 3, 4, 5])
[5, 4, 3, 2, 1]
>>> reverse(('shoe', 'my', 'buckle', 2, 1))
(1, 2, 'buckle', 'my', 'shoe')
>>> reverse('Python')
'nohtyP'
"""

3.
def sort_sequence(seq):
(用Python写出程序,使程序可以通过下面的doctest)
"""
>>> sort_sequence([3, 4, 6, 7, 8, 2])
[2, 3, 4, 6, 7, 8]
>>> sort_sequence((3, 4, 6, 7, 8, 2))
(2, 3, 4, 6, 7, 8)
>>> sort_sequence("nothappy")
'ahnoppty'
"""

4.
def recursive_min(nested_num_list):
(用Python写出程序,使程序可以通过下面的doctest)
"""
>>> recursive_min([2, 9, [1, 13], 8, 6])
1
>>> recursive_min([2, [[100, 1], 90], [10, 13], 8, 6])
1
>>> recursive_min([2, [[13, -7], 90], [1, 100], 8, 6])
-7
>>> recursive_min([[[-13, 7], 90], 2, [1, 100], 8, 6])
-13
"""

5.
def recursive_count(target, nested_num_list):
(用Python写出程序,使程序可以通过下面的doctest)
"""
>>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]])
4
>>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]])
2
>>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]])
0
>>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]])
6
"""

6.
def flatten(nested_num_list):
(用Python写出程序,使程序可以通过下面的doctest)
"""
>>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
[2, 9, 2, 1, 13, 2, 8, 2, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
[9, 7, 1, 13, 2, 8, 7, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
[9, 7, 1, 13, 2, 8, 2, 6]
>>> flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
[5, 5, 1, 5, 5, 5, 5, 6]
"""
展开
 我来答
PingPingFr
推荐于2016-04-21 · TA获得超过147个赞
知道答主
回答量:47
采纳率:0%
帮助的人:0
展开全部
def count(val, seq):
"""
>>> count(5, (1, 5, 3, 7, 5, 8, 5))
3
>>> count('s', 'Mississippi')
4
>>> count((1, 2), [1, 5, (1, 2), 7, (1, 2), 8, 5])
2
"""
return list(seq).count(val)

def reverse(seq):
"""
>>> reverse([1, 2, 3, 4, 5])
[5, 4, 3, 2, 1]
>>> reverse(('shoe', 'my', 'buckle', 2, 1))
(1, 2, 'buckle', 'my', 'shoe')
>>> reverse('Python')
'nohtyP'
"""
return seq[::-1]

def sort_sequence(seq):
"""
>>> sort_sequence([3, 4, 6, 7, 8, 2])
[2, 3, 4, 6, 7, 8]
>>> sort_sequence((3, 4, 6, 7, 8, 2))
(2, 3, 4, 6, 7, 8)
>>> sort_sequence("nothappy")
'ahnoppty'
"""
if type(seq) == str:
return ''.join(sorted(seq))
else:
return type(seq)(sorted(seq))

def recursive_min(nested_num_list):
"""
>>> recursive_min([2, 9, [1, 13], 8, 6])
1
>>> recursive_min([2, [[100, 1], 90], [10, 13], 8, 6])
1
>>> recursive_min([2, [[13, -7], 90], [1, 100], 8, 6])
-7
>>> recursive_min([[[-13, 7], 90], 2, [1, 100], 8, 6])
-13
"""
smallest = nested_num_list[0]
while type(smallest) == type([]):
smallest = smallest[0]

for element in nested_num_list:
if type(element) == type([]):
min_of_elem = recursive_min(element)
if smallest > min_of_elem:
smallest = min_of_elem
else: # element is not a list
if smallest > element:
smallest = element
return smallest

def recursive_count(target, nested_num_list):
"""
>>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]])
4
>>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]])
2
>>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]])
0
>>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]])
6
"""
count = 0
for element in nested_num_list:
if type(element) == type([]):
count += recursive_count(target, element)
else: # element is not a list
if element == target:
count += 1
return count

def flatten(nested_num_list):
"""
>>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
[2, 9, 2, 1, 13, 2, 8, 2, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
[9, 7, 1, 13, 2, 8, 7, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
[9, 7, 1, 13, 2, 8, 2, 6]
>>> flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
[5, 5, 1, 5, 5, 5, 5, 6]
"""
flat_list = []
for element in nested_num_list:
if type(element) == type([]):
flat_list += flatten(element)
else: # element is not a list
flat_list.append(element)
return flat_list

if __name__ == "__main__":
import doctest
doctest.testmod()

----------------------------------
6 items passed all tests:
3 tests in __main__.count
4 tests in __main__.flatten
4 tests in __main__.recursive_count
4 tests in __main__.recursive_min
3 tests in __main__.reverse
3 tests in __main__.sort_sequence
21 tests in 7 items.
21 passed and 0 failed.
Test passed.
-----------------------------------------

LZ在自学这个把
http://www.openbookproject.net/thinkCSpy/
确实是好书
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式