【每日一题1015】交通信号灯🚥

:woman_mage: 交通信号灯切换的规律依次是:红灯过后是绿色,绿灯过后是黄灯,黄灯过后是红灯。请编写一个函数,接收一个表示当前灯颜色的字符串,返回下一个信号灯颜色。
示例:
输入:“red”,输出:“green”
题目难度:简单
题目来源:codewars : Traffic light

def update_light(current: str) -> str:
    pass

assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"
def update_light(current: str) -> str:
    light_list = ['red', 'green', 'yellow']
    return light_list[light_list.index(current) + 1] if light_list.index(current) < len(light_list) -1 else light_list[0]


assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"
2 Likes
def update_light(current: str) -> str:
    current_list = ["red", "green", "yellow"]
    n = current_list.index(current)
    if n >= 2:
        return current_list[0]
    else:
        return current_list[n + 1]

assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"
def update_light(current):
    singals = ["red", "green", "yellow"]
    if current == "yellow":
        return "red"
    return singals[singals.index(current)+1]


assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"

写的有点low了这个 不应该写死 不好拓展

Python参考题解:

def update_light(current :str) -> str:
    lights = ['green', 'yellow', 'red']
    index = lights.index(current) + 1
    return lights[index % 3]

assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"

Java参考题解:

// 使用Switch

public class TrafficLights {

  public static String updateLight(String current) {
    switch (current) {
      case "red": return "green";
      case "yellow": return "red";
      case "green": return "yellow";
      default: throw new IllegalArgumentException();
    }
  }
}

//使用三目表达式

public class TrafficLights {

  public static String updateLight(String current) {
    return (current.equals("red") ? "green" : current.equals("yellow") ? "red" : "yellow");
  }
}
def update_light(current: str) -> str:
    list=["red","green","yellow"]
    if list.index(current)+1>2:
        return list[0]
    else:
        return list[list.index(current) + 1]


assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"
def update_light(current: str) -> str:
    data = {"red": "green", "green": "yellow", "yellow": "red"}
    return data.get(current)


assert update_light("red") == "green"
assert update_light("green") == "yellow"
assert update_light("yellow") == "red"
def solution(light: str):
    RGB = ["red", "green", "yellow"]
    if light in RGB:
        res = RGB.index(light) + 1
        if res == 3:
            res = 0
            return RGB[res]
        else:
            return RGB[res]



assert solution("red") == "green"
assert solution("green") == "yellow"
assert solution("yellow") == "red"
def solution(light: str):
    RGB = ["red", "green", "yellow"]
    if light in 'yellow':
        return 'red'
    return RGB[RGB.index(light) + 1]


assert solution("red") == "green"
assert solution("green") == "yellow"
assert solution("yellow") == "red"

就这个需求而言,写死应该没太大毛病吧,少了判断性能上也会好一点

def update_light(current: str) -> str:
    l = ["red", "green", "yellow"]
    return l[(l.index(current)+1)%len(l)]
关闭