日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

【Python CheckiO 题解】Even the Last

發(fā)布時間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python CheckiO 题解】Even the Last 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

CheckiO 是面向初學(xué)者和高級程序員的編碼游戲,使用 Python 和 JavaScript 解決棘手的挑戰(zhàn)和有趣的任務(wù),從而提高你的編碼技能,本博客主要記錄自己用 Python 在闖關(guān)時的做題思路和實(shí)現(xiàn)代碼,同時也學(xué)習(xí)學(xué)習(xí)其他大神寫的代碼。

CheckiO 官網(wǎng):https://checkio.org/

我的 CheckiO 主頁:https://py.checkio.org/user/TRHX/

CheckiO 題解系列專欄:https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有題解源代碼:https://github.com/TRHX/Python-CheckiO-Exercise


題目描述

【Even the Last】:給你一個整數(shù)數(shù)組,需要你把具有偶數(shù)索引的元素相加(0,2,4 …),然后把相加后得到的數(shù)與最后一個元素相乘,不要忘記,第一個元素的索引是0,如果傳入的是一個空數(shù)組,則應(yīng)該返回0。

【鏈接】:https://py.checkio.org/mission/even-last/

【輸入】:一個整數(shù)列表

【輸出】:運(yùn)算得到的結(jié)果(整數(shù)值類型)

【前提】:0 ≤ len(array) ≤ 20;all(isinstance(x, int) for x in array);all(-100 < x < 100 for x in array)

【范例】

checkio([0, 1, 2, 3, 4, 5]) == 30 checkio([1, 3, 5]) == 30 checkio([6]) == 36 checkio([]) == 0

代碼實(shí)現(xiàn)

def checkio(array):"""sums even-indexes elements and multiply at the last"""sum = 0if len(array) == 0:return 0else:for i in range(len(array)):if i % 2 == 0:sum = sum + array[i]return sum * array[-1]#These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__':print('Example:')print(checkio([0, 1, 2, 3, 4, 5]))assert checkio([0, 1, 2, 3, 4, 5]) == 30, "(0+2+4)*5=30"assert checkio([1, 3, 5]) == 30, "(1+5)*5=30"assert checkio([6]) == 36, "(6)*6=36"assert checkio([]) == 0, "An empty array = 0"print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

大神解答

大神解答 NO.1

def checkio(array):return sum(array[i] for i in range(0, len(array), 2))*array[-1] if len(array) >0 else 0

大神解答 NO.2

def checkio(array):sum = 0#evens = array[::2]for i in array[::2]:sum += ireturn sum*array[-1] if array else 0

大神解答 NO.3

def checkio(array):return sum(array[::2]) * array[-1] if array else 0

總結(jié)

以上是生活随笔為你收集整理的【Python CheckiO 题解】Even the Last的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。