码迷,mamicode.com
首页 > Web开发 > 详细

体验游戏编程网站

时间:2014-05-06 13:06:04      阅读:517      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   color   

最近学习python,想要找点练习,在看《python核心编程》(真是一本好书,非常详细,觉得看这一本书就够了,余下可以翻翻文档)。觉得cf之类的虽然能用python提交但是重点不是在学习python上 。终于找到了两个不错的网站checkio和pythonchallenge。今天先看看了看checkio确实很适合练习语法。

加载的速度有点慢,进去点两下就可以开始敲题了,题目还有提示

第一个题是清除列表中只出现了一次的元素:

bubuko.com,布布扣
 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"
View Code

当然根据提示敲个这个出来就足以说明我的新手程度了,在这里你还可以看到很多的非常好的解法,比如:

bubuko.com,布布扣
 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"
View Code

当然这里我的主要目的是学习语言,就暂且不最球效率什么的了

其实后面的就没提示了。。。。

第二题,求中位数

bubuko.com,布布扣
 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.")
View Code

另外这有个很有意思的解法,利用了负的索引,写的非常好,语言特性mark!

bubuko.com,布布扣
 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"
View Code

 

持续更新。。。

体验游戏编程网站,布布扣,bubuko.com

体验游戏编程网站

标签:style   blog   class   code   tar   color   

原文地址:http://www.cnblogs.com/MrLJC/p/3710564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!