最近在找點python語言練習的網站,發現這個網站不錯
http://www.checkio.org/
頁面設計的也比較漂亮,比較適合學習python的語法知識。
不過注冊這個網站 開始就得解決一個python問題,不過很簡單。
1 #python3.3 is inside 2 def checkio(els): 3 return els 4 5 if checkio([1, 2, 3, 4, 5, 6]) == 6: 6 print('Done!')
對上面的代碼 修改checkio中的函數 函數使得輸出的是列表里面前上個數的和。
1 def checkio(els): 2 return els[0]+els[1]+els[2] #后面也看見人家寫 return sum(els[0:3]) 3 if checkio([1, 2, 3, 4, 5, 6]) == 6: 4 print('Done!')
簡單修改一個就夠了 然后直接registration.
進去以后刷python題目 通關 有點植物大戰僵屍的感覺 .
第一個題目 就是Non-unique Elements.
code :
temp=[] a=len(data) def checkio(data): for n in range(a): if data.count(n)>1: temp.append(data[n]) data =temp return data
后面看到 creative 排名第一的寫法 更有創意
def checkio(data): return([x for x in data if data.count(x)>1])
這樣寫的話更簡潔,用的是 列表解析 的方法 可以在一行中用一個for循環將所有的值加入到列表中..