【Python CheckiO 题解】Multicolored Lamp


CheckiO is for beginners and advanced programmers to code games, Python and JavaScript to address difficult challenges and interesting tasks, thereby improving your coding skills, this blog is mainly to record their ideas in Python do problems at the checkpoints and implementation code , but also learn from other great God wrote the code.

CheckiO official website: https://checkio.org/

My CheckiO Home: https://py.checkio.org/user/TRHX/

CheckiO problem solution series of columns: https://itrhx.blog.csdn.net/category_9536424.html

CheckiO all solution to a problem source: https://github.com/TRHX/Python-CheckiO-Exercise


Title Description

Multicolored Lamp [] : New Year is coming, you have decided to decorate the house. But the simple lights and Christmas decorations too boring, so you have realized that you can use programming skills to create some really cool and innovative things. Your task is to create classesLamp()and methodslight()that will make the lamp in one of four colors in the sequence (Green, Red, Blue, Yellowlight. For the first timelight()when the approach should be color Green, the second should be Red, and so on. If the current color is Yellow, the color should be the next Green, and so on.

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

[Input] : string representing the number of turn on the light

[Output] : color of the lamp

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

[Example] :

lamp_1 = Lamp()
lamp_2 = Lamp()

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

Code

colors = ['Green', 'Red', 'Blue', 'Yellow']


class Lamp:
    def __init__(self):
        self.count = 0

    def light(self):
        if self.count == 4:
            self.count = 0
            color=colors[self.count]
        else:
            color=colors[self.count]
            self.count += 1
        return color


if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing

    lamp_1 = Lamp()
    lamp_2 = Lamp()

    lamp_1.light() #Green
    lamp_1.light() #Red
    lamp_2.light() #Green
    
    assert 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!")

Okami answer

Okami answer NO.1

from itertools import cycle

class Lamp:
    sequence = ('Green', 'Red', 'Blue', 'Yellow')

    def __init__(self):
        self.lights = cycle(Lamp.sequence)
    
    def light(self):
        return next(self.lights)

Okami answer NO.2

from itertools import cycle

class Lamp(cycle):
    __new__ = lambda cls: cycle.__new__(cls, 'Green Red Blue Yellow'.split())
    light = cycle.__next__

Okami answer NO.3

from itertools import cycle

sequence = ('Green', 'Red', 'Blue', 'Yellow')
Lamp = type('', (), {'__init__': lambda s: setattr(s, 'c', cycle(sequence)), 'light': lambda s: next(s.c)})
Published 149 original articles · won praise 518 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/103577098