본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 108

반응형

Q>

The New Year is coming and you've decided to decorate your home.

(새해가오고 당신은 집을 장식하기로 결정했습니다.)

But simple lights and Christmas decorations are so boring, so you have figured that you can use your programing skills and create something really cool and original.

(그러나 단순한 조명과 크리스마스 장식은 지루합니다. 그래서 당신은 당신의 프로그래밍 기술을 사용하고 정말 근사하고 독창적 인 것을 창조 할 수 있다고 생각했습니다.)

Your task is to create the class Lamp() and method light() which will make the lamp glow with one of the four colors in the sequence - (‘Green’, ‘Red’, ‘Blue’, ‘Yellow’).

(당신의 임무는 Lamp ()와 method light () 클래스를 만드는 것입니다. 그러면 램프가 네 가지 색상 ( '녹색', '빨강', '파랑', '황색') 중 하나와 함께 빛납니다.)

When the light() method is used for the first time, the color should be 'Green', the second time - 'Red' and so on. If the current color is 'Yellow', the next color should be 'Green' and so on.

(light () 메서드를 처음 사용할 때 색상은 '녹색'이어야하고 두 번째는 '빨강'이어야합니다. 현재 색이 '노란색'이면 다음 색은 '녹색'이어야합니다.)

In this mission you can use the State design pattern.

(이 임무에서 State Design Pattern을 사용할 수 있습니다.)

It's highly useful in the situations where object should change its behaviour depending on the internal state.

(객체가 내부 상태에 따라 동작을 변경해야하는 상황에서 매우 유용합니다.)

 

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"

 

Input: A few strings indicating the number of times the lamp is being turned on.

        (램프가 켜져있는 횟수를 나타내는 문자열입니다.)

 

Output: The color of the lamp.

           (램프의 색상입니다.)

 

How it is used: To implement objects with mutable behavior.

                     (변경 가능한 동작을 사용하여 개체를 구현합니다.)

 

Precondition: 4 colors: Green, Red, Blue, Yellow.

 

A>

# itertools : 반복 가능한 데이터 스트림을 처리
import itertools

class Lamp:
    def __init__(self):
        # itertools.cycle : 순환 가능한 객체에서 요소를 반복적으로 생성
        self.state = itertools.cycle(('Green', 'Red', 'Blue', 'Yellow'))

    def light(self):
        # next : 반복자의 다음 요소 구하기
        return next(self.state)

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!")

 

O>

Coding complete? Let's try tests!

Process finished with exit code 0

 

lamp_4 = Lamp() lamp_4.light() lamp_4.light() lamp_4.light() lamp_4.light() lamp_4.light()

 Your result:
"Green"

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 110  (0) 2019.07.02
Python Learn the basics Quiz 109  (0) 2019.06.28
Python Learn the basics Quiz 107  (0) 2019.06.27
Python Learn the basics Quiz 106  (0) 2019.06.27
Python Learn the basics Quiz 105  (0) 2019.06.25