【每日一题20220422】排队游戏

大佬,这个if direction == "left": values.reverse() 冒号是啥意思

:后面加个回车键你就能看懂了

def element_sort(list_ele):
    list_a = []
    list_b = []

    """
    输出:[['black', 'white'], ['red', 'green'], ['green', 'black']]
    """
    for i in range(len(list_ele)):
        list_temp = list_ele[i].split(' ')
        if list_temp[-1] == 'left':
            list_b.append([list_temp[2], list_temp[0]])
        else:
            list_b.append([list_temp[0], list_temp[2]])
    print(list_b)

    """
    两两比较,可能有:0个交集、1个交集、2个交集
    """
    list_a = list_b.pop(0)
    for j in range(len(list_b)):
        inter = list(set(list_a) & set(list_b[j]))  # inter为交集

        if len(inter) == 0:
            """0个交集,先将没有交集的后移"""
            list_b.append(list_b.pop(j))
            inter = list(set(list_a) & set(list_b[j]))  # inter为交集

            if list_b[j].index(inter[0]) == 1:
                """交集在后往前插"""
                list_a.insert(list_a.index(inter[0]), list_b[j][0])
            elif list_b[j].index(inter[0]) == 0:
                """交集在前往后插"""
                list_a.insert(list_a.index(inter[0]) + 1, list_b[j][1])

        elif len(inter) == 1:

            if list_b[j].index(inter[0]) == 1:
                """交集在后往前插"""
                list_a.insert(list_a.index(inter[0]), list_b[j][0])
            elif list_b[j].index(inter[0]) == 0:
                """交集在前往后插"""
                list_a.insert(list_a.index(inter[0]) + 1, list_b[j][1])

        elif len(inter) == 2:
            """如果相同则跳过本次循环"""
            continue
    print(f"最终结果为:{list_a}")
def solution(hints: list)-> list:
    res_list=[]
    list_hints=[(i[2],i[0]) if i[5]=='left' else (i[0],i[2]) for i in map(str.split,hints)]
    for h in list_hints:
        if res_list==[]:
            res_list=list(h)
        else:
            if h[1]==res_list[0]:
                res_list.insert(0,h[0])
            elif h[0]==res_list[-1]:
                res_list.append(h[1])
    return res_list

assert solution(["d has c on his left","c has b on his left","b has a on his left"]) == ["a", "b", "c", "d"]
assert solution(["d has c on his right","c has b on his right","b has a on his right"]) == ["d", "c", "b", "a"]
assert solution(["red has green on his right","green has red on his left"]) == ["red", "green"]