体验游戏编程网站

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

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

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

1#Your optional code here2#You can import some modules or create additional functions345defcheckio(data):6#Your code here7#It's main function. Don't remove this function8#It's used for auto-testing and must return a result for check.910l=len(data)11temp=[]12i=013foriinrange(l):14ifdata.count(data[i])>1:15temp.append(data[i])16data=temp;17returndata1819#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 badPRactice for many real cases)23#Loop over original list242526if__name__=="__main__":27#These "asserts" using only for self-checking and not necessary for auto-testing28assertisinstance(checkio([1]), list),"The result must be a list"29assertcheckio([1, 2, 3, 1, 3]) == [1, 3, 1, 3],"1st example"30assertcheckio([1, 2, 3, 4, 5]) == [],"2nd example"31assertcheckio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5],"3rd example"32assertcheckio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9],"4th example"

View Code

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

1#Your optional code here2#You can import some modules or create additional functions345defcheckio(data):6return[iforiindataifdata.count(i) > 1]78#Some hints9#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 list131415#These "asserts" using only for self-checking and not necessary for auto-testing16if__name__=="__main__":17assertisinstance(checkio([1]), list),"The result must be a list"18assertcheckio([1, 2, 3, 1, 3]) == [1, 3, 1, 3],"1st example"19assertcheckio([1, 2, 3, 4, 5]) == [],"2nd example"20assertcheckio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5],"3rd example"21assertcheckio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9],"4th example"

View Code

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

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

第二题,求中位数

1defcheckio(data):23data.sort()4l=len(data)5ifl%2==0:6data[0]=(data[l/2-1]+data[l/2])/2.07else:8data[0]=data[l/2]9returndata[0]1011#These "asserts" using only for self-checking and not necessary for auto-testing12if__name__=='__main__':13assertcheckio([1, 2, 3, 4, 5]) == 3,"Sorted list"14assertcheckio([3, 1, 2, 5, 3]) == 3,"Not sorted list"15assertcheckio([1, 300, 2, 200, 1]) == 2,"It's not an average"16assertcheckio([3, 6, 20, 99, 10, 15]) == 12.5,"Even length"17print("Start the long test")18assertcheckio(range(1000000)) == 499999.5,"Long."19print("The local tests are done.")

View Code

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

1defcheckio(data):2off = len(data) // 23data.sort()4med = data[off] + data[-(off + 1)]5returnmed / 267#These "asserts" using only for self-checking and not necessary for auto-testing8if__name__=='__main__':9assertcheckio([1, 2, 3, 4, 5]) == 3,"Sorted list"10assertcheckio([3, 1, 2, 5, 3]) == 3,"Not sorted list"11assertcheckio([1, 300, 2, 200, 1]) == 2,"It's not an average"12assertcheckio([3, 6, 20, 99, 10, 15]) == 12.5,"Even length"

View Code

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

1defcheckio(data):23#replace this for solution45l=len(data)6digist=False7upp=False8low=False9ifl<10:10returnFalse11foriindata:12ifi<='9'andi>='0':13digist=True14elifi<='z'andi>='a':15low=True16elifi<='Z'andi>='A':17upp=True18ifdigistanduppandlow:19returnTrue20else:21returnFalse2223#Some hints24#Just check all conditions252627if__name__=='__main__':28#These "asserts" using only for self-checking and not necessary for auto-testing29assertcheckio(u'A1213pokl') == False,"1st example"30assertcheckio(u'bAse730onE4') == True,"2nd example"31assertcheckio(u'asasasasasasasaas') == False,"3rd example"32assertcheckio(u'QWERTYqwerty') == False,"4th example"33assertcheckio(u'123456123456') == False,"5th example"34assertcheckio(u'QwErTy911poQQqq') == True,"6th example"

View Code

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

持续更新。。。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航