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

歡迎訪問 生活随笔!

生活随笔

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

python

【Python CheckiO 题解】Multicolored Lamp

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

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


題目描述

【Multicolored Lamp】:新年快到了,您已經(jīng)決定要裝修房屋。但是簡單的燈光和圣誕節(jié)裝飾太無聊了,因此您已經(jīng)意識(shí)到可以使用編程技巧來創(chuàng)建一些非常酷和新穎的東西。您的任務(wù)是創(chuàng)建類 Lamp() 和方法 light(),它們將使燈以序列中的四種顏色之一(Green, Red, Blue, Yellow)發(fā)光。第一次使用 light() 方法時(shí),顏色應(yīng)為 Green,第二次應(yīng)為 Red,依此類推。如果當(dāng)前顏色為 Yellow,則下一個(gè)顏色應(yīng)為 Green,依此類推。

【鏈接】:https://py.checkio.org/mission/multicolored-lamp/

【輸入】:字符串,表示打開燈的次數(shù)

【輸出】:燈的顏色

【前提】:4 colors: Green, Red, Blue, Yellow.

【范例】

lamp_1 = Lamp() lamp_2 = Lamp()lamp_1.light() #Green lamp_1.light() #Red lamp_2.light() #Greenlamp_1.light() == "Blue" lamp_1.light() == "Yellow" lamp_1.light() == "Green" lamp_2.light() == "Red" lamp_2.light() == "Blue"

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

colors = ['Green', 'Red', 'Blue', 'Yellow']class Lamp:def __init__(self):self.count = 0def light(self):if self.count == 4:self.count = 0color=colors[self.count]else:color=colors[self.count]self.count += 1return colorif __name__ == '__main__':#These "asserts" using only for self-checking and not necessary for auto-testinglamp_1 = Lamp()lamp_2 = Lamp()lamp_1.light() #Greenlamp_1.light() #Redlamp_2.light() #Greenassert lamp_1.light() == "Blue"assert lamp_1.light() == "Yellow"assert lamp_1.light() == "Green"assert lamp_2.light() == "Red"assert lamp_2.light() == "Blue"print("Coding complete? Let's try tests!")

大神解答

大神解答 NO.1

from itertools import cycleclass Lamp:sequence = ('Green', 'Red', 'Blue', 'Yellow')def __init__(self):self.lights = cycle(Lamp.sequence)def light(self):return next(self.lights)

大神解答 NO.2

from itertools import cycleclass Lamp(cycle):__new__ = lambda cls: cycle.__new__(cls, 'Green Red Blue Yellow'.split())light = cycle.__next__

大神解答 NO.3

from itertools import cyclesequence = ('Green', 'Red', 'Blue', 'Yellow') Lamp = type('', (), {'__init__': lambda s: setattr(s, 'c', cycle(sequence)), 'light': lambda s: next(s.c)})

總結(jié)

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

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