体验游戏编程网站

最近学习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"
View Code

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

 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

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

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

第二题,求中位数

 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!

 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

 判断密码的强度,其实也就是判断是否存在某些值

 1 def checkio(data):
 2 
 3     #replace this for solution
 4     
 5     l=len(data)
 6     digist=False
 7     upp=False
 8     low=False
 9     if l<10:
10         return False
11     for i in data:
12         if i<='9' and i>='0':
13             digist=True
14         elif i<='z' and i>='a':
15             low=True
16         elif i<='Z' and i>='A':
17             upp=True
18     if digist and upp and low:
19         return True
20     else:
21         return False
22 
23 #Some hints
24 #Just check all conditions
25 
26 
27 if __name__ == '__main__':
28     #These "asserts" using only for self-checking and not necessary for auto-testing
29     assert checkio(u'A1213pokl') == False, "1st example"
30     assert checkio(u'bAse730onE4') == True, "2nd example"
31     assert checkio(u'asasasasasasasaas') == False, "3rd example"
32     assert checkio(u'QWERTYqwerty') == False, "4th example"
33     assert checkio(u'123456123456') == False, "5th example"
34     assert checkio(u'QwErTy911poqqqq') == True, "6th example"
View Code

其实还可以用一些函数的但是感觉挺难记,还不如自己写呢

持续更新。。。

posted @ 2014-05-07 00:51  mrbean  阅读(938)  评论(0编辑  收藏  举报