python 编程 3道题 巨急!!
我一共有3道题想破脑袋也不知道怎么做明天就考试了高手请帮帮忙呀T-Tdefsortstuff(mylist):"""Returnanewlistthatisasorted...
我一共有3道题想破脑袋也不知道怎么做
明天就考试了 高手 请帮帮忙呀 T-T
def sortstuff(mylist):
"""
Return a new list that is a sorted version of the parameter
mylist. The original list should remain unaltered.
>>> sortstuff([3, 5, 1, 9, 13, 2])
[1, 2, 3, 5, 9, 13]
>>> sortstuff(["one", "two", "three", "four"])
['four', 'one', 'three', 'two']
>>> 11 = [2, 4, 1, 7, 0]
>>> 12 = sortstuff(11)
>>> 11 == 12
False
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
第二题
def compare(a, b):
"""
Return a string indicating which parameter is greater.
>>> compare(3, 1)
'3 is greater than 1'
>>> compare(2, 5)
'2 is less than 5'
>>> compare(4, 4)
'4 and 4 are equal'
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
第三题
def count_word(sentence):
"""
Return the number of words in a sentence.
>>> count_word("The quick brown fox jumps over the lazy dog")
9
>>> count_word("Hello")
1
>>> count_word("")
0
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
不能使用 print statement....
请帮帮快抓狂的我吧。
忽略以上问题 我已经解决了。。现在帮忙看一下这道吧。
再此感谢 (MR.黄泉客) 帮忙解决以上问题 .
def score(numbers):
"""
Return the average of the numbers excluding the biggest and the smallest one
>>> score([2, 7, 9, 10, 13, 1, 5, 12])
7.5
>>> score([3, 7, 2.5, -4])
2.75
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True) 展开
明天就考试了 高手 请帮帮忙呀 T-T
def sortstuff(mylist):
"""
Return a new list that is a sorted version of the parameter
mylist. The original list should remain unaltered.
>>> sortstuff([3, 5, 1, 9, 13, 2])
[1, 2, 3, 5, 9, 13]
>>> sortstuff(["one", "two", "three", "four"])
['four', 'one', 'three', 'two']
>>> 11 = [2, 4, 1, 7, 0]
>>> 12 = sortstuff(11)
>>> 11 == 12
False
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
第二题
def compare(a, b):
"""
Return a string indicating which parameter is greater.
>>> compare(3, 1)
'3 is greater than 1'
>>> compare(2, 5)
'2 is less than 5'
>>> compare(4, 4)
'4 and 4 are equal'
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
第三题
def count_word(sentence):
"""
Return the number of words in a sentence.
>>> count_word("The quick brown fox jumps over the lazy dog")
9
>>> count_word("Hello")
1
>>> count_word("")
0
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
不能使用 print statement....
请帮帮快抓狂的我吧。
忽略以上问题 我已经解决了。。现在帮忙看一下这道吧。
再此感谢 (MR.黄泉客) 帮忙解决以上问题 .
def score(numbers):
"""
Return the average of the numbers excluding the biggest and the smallest one
>>> score([2, 7, 9, 10, 13, 1, 5, 12])
7.5
>>> score([3, 7, 2.5, -4])
2.75
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True) 展开
4个回答
展开全部
def score(numbers):
"""Return the average of the numbers excluding the biggest and the smallest one
>>> score([2, 7, 9, 10, 13, 1, 5, 12])
7.5
>>> score([3, 7, 2.5, -4])
2.75
"""
if (len(numbers) < 2 ):
raise RuntimeException("")
numbers.sort()
s = sum( numbers[1:-1])
return float(s)/(len(numbers) -2)
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
"""Return the average of the numbers excluding the biggest and the smallest one
>>> score([2, 7, 9, 10, 13, 1, 5, 12])
7.5
>>> score([3, 7, 2.5, -4])
2.75
"""
if (len(numbers) < 2 ):
raise RuntimeException("")
numbers.sort()
s = sum( numbers[1:-1])
return float(s)/(len(numbers) -2)
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
def sortstuff(mylist):
return sorted(mylist)
def compare(a, b):
if a > b:
return '%d is greater than %d' % (a, b)
if a == b:
return '%d and %d are equal' % (a, b)
return '%d is less than %d' % (a, b)
def count_word(sentence):
import re
return len([s, for s in re.split('\W', sentence) if s])
return sorted(mylist)
def compare(a, b):
if a > b:
return '%d is greater than %d' % (a, b)
if a == b:
return '%d and %d are equal' % (a, b)
return '%d is less than %d' % (a, b)
def count_word(sentence):
import re
return len([s, for s in re.split('\W', sentence) if s])
追问
def score(numbers):
"""
Return the average of the numbers excluding the biggest and the smallest one
>>> score([2, 7, 9, 10, 13, 1, 5, 12])
7.5
>>> score([3, 7, 2.5, -4])
2.75
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
请帮忙看一下这道吧 万分万分感谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
def score(numbers):
return sum(sorted(numbers)[1:-1]) / float(len(numbers) - 2)
return sum(sorted(numbers)[1:-1]) / float(len(numbers) - 2)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
def score(l):
l = sorted(l)[1:-1]
return sum(l)/len(l)
l = sorted(l)[1:-1]
return sum(l)/len(l)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询