【每日一题20220316】万里追踪

:woman_mage:Harry和他的小伙伴们最近忙于跟踪伏地魔的下落,并且每天将他出现的地点信息用列表格式[上个地点,下个地点]记录了下来,但是不小心把顺序搞得错乱了。我们的任务就是,追溯出伏地魔完整的行动轨迹。

给定一个路径列表routes,其中每个元素是一个,包含2个前后出现的地点信息的小元组,请编写一个函数,返回正确的地点路径。

示例:
输入: [ (USA, BRA), (JPN, PHL), (BRA, UAE), (UAE, JPN) ]
输出: "USA, BRA, UAE, JPN, PHL"
解释:根据(USA, BRA])可以推断USA之后是BRA,再根据(BRA, UAE) 推断得到USA,BRA,UAE,依次类推。

题目难度:中等
题目来源:CodeWars:Follow that Spy

def solution(routes: list) -> str:
    # your code here

assert solution([('BRA','KSA'), ('USA','BRA'), ('JPN','PHL'), ('KSA','UAE'), ('UAE','JPN')]) == "USA,BRA,KSA,UAE,JPN,PHL"
assert solution([('ITA','GER'), ('GER','BEL'), ('BEL','CAN')]) == "ITA,GER,BEL,CAN"
assert solution([('MNL','TAG'), ('CEB','TAC'), ('TAG','CEB'), ('TAC','BOR')]) == "MNL,TAG,CEB,TAC,BOR"
    def solution(routes: list) -> str:
        #获取元组的末尾值
        routes_second_ele = [i[1] for i in routes]
        #获取返回值的第一个元素
        lis_return = [i[0] for i in routes if i[0] not in routes_second_ele]
        while len(routes) > 0:
            for i in routes:
                if i[0] == lis_return[-1]:
                    lis_return.append(i[1])
                    routes.remove(i)
        return ','.join(lis_return)
def find_routes(routes: list) -> str:
    demo = list(routes.pop(0))
    while len(routes) > 0:
        for route in routes:
            if route[0] == demo[-1]:
                demo.append(route[1])
                routes.remove(route)
            elif route[1] == demo[0]:
                demo.insert(0, route[0])
                routes.remove(route)
    return ', '.join(demo)
def find_routes(routes: list) -> str:
    d = dict(routes)
    res = list(d.keys() - d.values())
    while res[-1] in d: res.append(d[res[-1]])
    return ', '.join(res)
def solution(routes: list) -> str:
    # 将列表转换为字典
    routes_dict = dict(routes)
    # 存放结果
    result = []
    # 将列表转为一维数组,找出个数只有1个且属于key的元素,即为第一个元素,添加到result列表中
    routes_L = []
    for x in routes:
        routes_L.extend(x)
    for i in routes_L:
        if routes_L.count(i) == 1 and i in routes_dict.keys():
            result.append(i)
    # 循环次数
    l = len(routes)
    # result列表以第一个元素为key,添加value值,即为第二个元素,再以第二个元素为key,添加value值,以此类推
    for i in range(l):
        result.append(routes_dict[result[i]])
    return ','.join(result)


assert solution(
    [('BRA', 'KSA'), ('USA', 'BRA'), ('JPN', 'PHL'), ('KSA', 'UAE'), ('UAE', 'JPN')]) == "USA,BRA,KSA,UAE,JPN,PHL"
assert solution([('ITA', 'GER'), ('GER', 'BEL'), ('BEL', 'CAN')]) == "ITA,GER,BEL,CAN"
assert solution([('MNL', 'TAG'), ('CEB', 'TAC'), ('TAG', 'CEB'), ('TAC', 'BOR')]) == "MNL,TAG,CEB,TAC,BOR"

def solution(routes: list) -> str:
    # your code here
    list_new = routes[:]
    list_ = []
    for i in routes:
        for j in range(len(list_new)):
            if i[0] == list_new[j][1]:
                list_new[list_new.index(i)], list_new[j] = list_new[j], list_new[list_new.index(i)]
    for o in list_new:
        if o[0] not in list_:
            list_.append(o[0])
        if o[1] not in list_:
            list_.append(o[1])
    str_routes = ",".join(list_)
    return str_routes


assert solution(
    [('BRA', 'KSA'), ('USA', 'BRA'), ('JPN', 'PHL'), ('KSA', 'UAE'), ('UAE', 'JPN')]) == "USA,BRA,KSA,UAE,JPN,PHL"
assert solution([('ITA', 'GER'), ('GER', 'BEL'), ('BEL', 'CAN')]) == "ITA,GER,BEL,CAN"
assert solution([('MNL', 'TAG'), ('CEB', 'TAC'), ('TAG', 'CEB'), ('TAC', 'BOR')]) == "MNL,TAG,CEB,TAC,BOR"
def solution(routes: list) -> str:
    res_list = []
    for i in range(len(routes)):
        for j in range(len(routes)):
            if routes[i][0] == routes[j][1]:
                break
        else:
            res_list.append(routes[i][0])
    while True:
        for route in routes:
            if route[0] == res_list[-1]:
                res_list.append(route[1])
                break
        else:
            break
    return ','.join(res_list)


assert solution(
    [('BRA', 'KSA'), ('USA', 'BRA'), ('JPN', 'PHL'), ('KSA', 'UAE'), ('UAE', 'JPN')]) == "USA,BRA,KSA,UAE,JPN,PHL"
assert solution([('ITA', 'GER'), ('GER', 'BEL'), ('BEL', 'CAN')]) == "ITA,GER,BEL,CAN"
assert solution([('MNL', 'TAG'), ('CEB', 'TAC'), ('TAG', 'CEB'), ('TAC', 'BOR')]) == "MNL,TAG,CEB,TAC,BOR"