【Python CheckiO 题解】Index Power
CheckiO 是面向初學(xué)者和高級(jí)程序員的編碼游戲,使用 Python 和 JavaScript 解決棘手的挑戰(zhàn)和有趣的任務(wù),從而提高你的編碼技能,本博客主要記錄自己用 Python 在闖關(guān)時(shí)的做題思路和實(shí)現(xiàn)代碼,同時(shí)也學(xué)習(xí)學(xué)習(xí)其他大神寫的代碼。
CheckiO 官網(wǎng):https://checkio.org/
我的 CheckiO 主頁(yè):https://py.checkio.org/user/TRHX/
CheckiO 題解系列專欄:https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有題解源代碼:https://github.com/TRHX/Python-CheckiO-Exercise
題目描述
【Index Power】:給定一個(gè)數(shù)組 array 和一個(gè)整數(shù) n,求該數(shù)組 n 位置元素的 n 次方,如果整數(shù) n 大于數(shù)組的長(zhǎng)度,則輸出 -1, 如果數(shù)組 n 位置元素為 0,則輸出 1.
【鏈接】:https://py.checkio.org/mission/index-power/
【輸入】:兩個(gè)參數(shù),一個(gè)數(shù)組,一個(gè)整數(shù)。
【輸出】:計(jì)算結(jié)果(整數(shù))
【范例】:
代碼實(shí)現(xiàn)
def index_power(array: list, n: int) -> int:"""Find Nth power of the element with index N."""if n < len(array):return array[n] ** nelse:return -1if __name__ == '__main__':print('Example:')print(index_power([1, 2, 3, 4], 2))#These "asserts" using only for self-checking and not necessary for auto-testingassert index_power([1, 2, 3, 4], 2) == 9, "Square"assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"assert index_power([0, 1], 0) == 1, "Zero power"assert index_power([1, 2], 3) == -1, "IndexError"print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")大神解答
大神解答 NO.1
def index_power(array: list, n: int) -> int:"""Find Nth power of the element with index N."""if n < len(array):for x in array[n:n+1]:return x**nelse:return -1大神解答 NO.2
def index_power(array: list, n: int) -> int:"""Find Nth power of the element with index N."""return array[n]**n if len(array) > n else -1 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【Python CheckiO 题解】Index Power的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: AMD Zen4+RDNA3超级APU真
- 下一篇: 【Python CheckiO 题解】T