国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

體驗游戲編程網站

2019-11-14 17:47:08
字體:
來源:轉載
供稿:網友

最近學習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"
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 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"
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-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.")
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 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"
View Code

其實還可以用一些函數的但是感覺挺難記,還不如自己寫呢

持續更新。。。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹溪县| 阿鲁科尔沁旗| 忻州市| 和田市| 辉南县| 鄂州市| 贵阳市| 临武县| 弋阳县| 柳林县| 天全县| 阿拉善盟| 蓬溪县| 棋牌| 凤阳县| 横峰县| 胶州市| 德阳市| 朝阳县| 海阳市| 兴仁县| 新平| 沧源| 黔西县| 越西县| 蕉岭县| 阳朔县| 镇原县| 凤庆县| 乾安县| 平顶山市| 延寿县| 唐山市| 韩城市| 遂川县| 元谋县| 盐亭县| 兰溪市| 望江县| 伊金霍洛旗| 博罗县|