日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【Python CheckiO 题解】Right to Left

發(fā)布時間:2023/12/10 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python CheckiO 题解】Right to Left 小編覺得挺不錯的,現(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


題目描述

【Right to Left】:給定一個字符串序列,以元組的形式輸入,將其中的 right 關(guān)鍵字替換成 left,要求輸出為字符串,原來元組之間的元素用逗號連接。

【鏈接】:https://py.checkio.org/mission/right-to-left/

【輸入】:元組

【輸出】:字符串

【前提】:0 < len(phrases) < 42

【范例】

left_join(("left", "right", "left", "stop")) == "left,left,left,stop" left_join(("bright aright", "ok")) == "bleft aleft,ok" left_join(("brightness wright",)) == "bleftness wleft" left_join(("enough", "jokes")) == "enough,jokes"

解題思路

先用 join() 方法將元組轉(zhuǎn)換成字符串,并用逗號連接,再用 replace() 方法將 right 替換成 left 即可

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

def left_join(phrases):"""Join strings and replace "right" to "left""""return ','.join(phrases).replace('right', 'left')if __name__ == '__main__':print('Example:')print(left_join(("left", "right", "left", "stop")))#These "asserts" using only for self-checking and not necessary for auto-testingassert left_join(("left", "right", "left", "stop")) == "left,left,left,stop", "All to left"assert left_join(("bright aright", "ok")) == "bleft aleft,ok", "Bright Left"assert left_join(("brightness wright",)) == "bleftness wleft", "One phrase"assert left_join(("enough", "jokes")) == "enough,jokes", "Nothing to replace"print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

大神解答

大神解答 NO.1

def left_join(phrases):"""Join strings and replace "right" to "left""""text = ''for word in phrases: text +=word+','text = text[:-1]text = text.replace('right','left')

大神解答 NO.2

def left_join(x): return ','.join([i.replace('right','left') for i in x])

總結(jié)

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

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