体验游戏编程网站
最近学习python,想要找点练习,在看《python核心编程》(真是一本好书,非常详细,觉得看这一本书就够了,余下可以翻翻文档)。觉得cf之类的虽然能用python提交但是重点不是在学习python上 。终于找到了两个不错的网站checkio和pythonchallenge。今天先看看了看checkio确实很适合练习语法。
加载的速度有点慢,进去点两下就可以开始敲题了,题目还有提示
第一个题是清除列表中只出现了一次的元素:
1 #Your optional code here
2 #You can import some modules or create additional functions
3
4
5 def checkio(data):
6 #Your code here
7 #It‘s main function. Don‘t remove this function
8 #It‘s used for auto-testing and must return a result for check.
9
10 l=len(data)
11 temp=[]
12 i=0
13 for i in range(l):
14 if data.count(data[i])>1:
15 temp.append(data[i])
16 data=temp;
17 return data
18
19 #Some hints
20 #You can use list.count(element) method for counting.
21 #Create new list with non-unique elements
22 #or remove elements from original list (but it‘s bad practice for many real cases)
23 #Loop over original list
24
25
26 if __name__ == "__main__":
27 #These "asserts" using only for self-checking and not necessary for auto-testing
28 assert isinstance(checkio([1]), list), "The result must be a list"
29 assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
30 assert checkio([1, 2, 3, 4, 5]) == [], "2nd example"
31 assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
32 assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
当然根据提示敲个这个出来就足以说明我的新手程度了,在这里你还可以看到很多的非常好的解法,比如:
1 #Your optional code here
2 #You can import some modules or create additional functions
3
4
5 def checkio(data):
6 return [i for i in data if data.count(i) > 1]
7
8 #Some hints
9 #You can use list.count(element) method for counting.
10 #Create new list with non-unique elements
11 #or remove elements from original list (but it‘s bad practice for many real cases)
12 #Loop over original list
13
14
15 #These "asserts" using only for self-checking and not necessary for auto-testing
16 if __name__ == "__main__":
17 assert isinstance(checkio([1]), list), "The result must be a list"
18 assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
19 assert checkio([1, 2, 3, 4, 5]) == [], "2nd example"
20 assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
21 assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
当然这里我的主要目的是学习语言,就暂且不最球效率什么的了
其实后面的就没提示了。。。。
第二题,求中位数
1 def checkio(data): 2 3 data.sort() 4 l=len(data) 5 if l%2==0: 6 data[0]=(data[l/2-1]+data[l/2])/2.0 7 else: 8 data[0]=data[l/2] 9 return data[0] 10 11 #These "asserts" using only for self-checking and not necessary for auto-testing 12 if __name__ == ‘__main__‘: 13 assert checkio([1, 2, 3, 4, 5]) == 3, "Sorted list" 14 assert checkio([3, 1, 2, 5, 3]) == 3, "Not sorted list" 15 assert checkio([1, 300, 2, 200, 1]) == 2, "It‘s not an average" 16 assert checkio([3, 6, 20, 99, 10, 15]) == 12.5, "Even length" 17 print("Start the long test") 18 assert checkio(range(1000000)) == 499999.5, "Long." 19 print("The local tests are done.")
另外这有个很有意思的解法,利用了负的索引,写的非常好,语言特性mark!
1 def checkio(data): 2 off = len(data) // 2 3 data.sort() 4 med = data[off] + data[-(off + 1)] 5 return med / 2 6 7 #These "asserts" using only for self-checking and not necessary for auto-testing 8 if __name__ == ‘__main__‘: 9 assert checkio([1, 2, 3, 4, 5]) == 3, "Sorted list" 10 assert checkio([3, 1, 2, 5, 3]) == 3, "Not sorted list" 11 assert checkio([1, 300, 2, 200, 1]) == 2, "It‘s not an average" 12 assert checkio([3, 6, 20, 99, 10, 15]) == 12.5, "Even length"
持续更新。。。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。