最近學習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=013 for i in range(l):14 if data.count(data[i])>1:15 temp.append(data[i])16 data=temp;17 return data18 19 #Some hints20 #You can use list.count(element) method for counting.21 #Create new list with non-unique elements22 #or remove elements from original list (but it's bad PRactice for many real cases)23 #Loop over original list24 25 26 if __name__ == "__main__":27 #These "asserts" using only for self-checking and not necessary for auto-testing28 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 elements11 #or remove elements from original list (but it's bad practice for many real cases)12 #Loop over original list13 14 15 #These "asserts" using only for self-checking and not necessary for auto-testing16 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-testing12 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"
判斷密碼的強度,其實也就是判斷是否存在某些值

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 False11 for i in data:12 if i<='9' and i>='0':13 digist=True14 elif i<='z' and i>='a':15 low=True16 elif i<='Z' and i>='A':17 upp=True18 if digist and upp and low:19 return True20 else:21 return False22 23 #Some hints24 #Just check all conditions25 26 27 if __name__ == '__main__':28 #These "asserts" using only for self-checking and not necessary for auto-testing29 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"
其實還可以用一些函數的但是感覺挺難記,還不如自己寫呢
持續更新。。。
新聞熱點
疑難解答