나는 이렇게 학습한다/Algorithm & SQL

0925. Thinkful - Logic Drills: Traffic light

daco2020 2022. 9. 25. 14:43
반응형

You're writing code to control your town's traffic lights. You need a function to handle each change from green, to yellow, to red, and then to green again.

Complete the function that takes a string as an argument representing the current state of the light and returns a string representing the state the light should change to.

For example, when the input is green, output should be yellow.



Solution:

from typing import Dict, Optional


CHANGE_LIGHT = {
    "green": "yellow",
    "yellow": "red",
    "red": "green"
}

def update_light(current: str, light: Dict[str, str] = CHANGE_LIGHT, is_on: bool = True) -> Optional[str]:
    if not is_on: return None
    return light[current]


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

0927. Opposites Attract  (0) 2022.09.27
0926. Merge two sorted arrays into one  (0) 2022.09.26
0924. Sum of positive  (0) 2022.09.24
0923. Sum of differences in array  (0) 2022.09.23
0922. Love vs friendship  (0) 2022.09.22