 # 【每日一题20220422】排队游戏 ‍ 我们今天的任务是给元素进行排序，已知的条件是一个表示元素顺序的提示文字。请编写一个函数返回正确顺序的列表。

【示例】

``````def solution(hints: list)-> 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"]
``````
``````
def solution(hints: list) -> list:
li = []
for s in hints:
*values, direction = s.replace("has ", "").replace("on his ", "").split(" ")
if direction == "left": values.reverse()
if not li:
li.extend(values)
continue
if values in li and values in li:
continue
elif values in li:
li.insert(li.index(values) + 1, values)
elif values in li:
li.insert(li.index(values), values)
else:
if s == hints[-1]:
raise AttributeError
hints.append(s)
return li

``````
4 Likes
``````def solution(hints: list) -> list:
li = []
try:
for s in hints:
values = s.replace("has ", "").replace("on his ", "").split(" ")
if values[-1] == "left":
if len(li) == 0:
li.append(values)
li.append(values)
continue
elif values not in li and values in li:
li.reverse()
li.append(values)
li.reverse()
continue
elif values[-1] == "right":
if len(li) == 0:
li.append(values)
li.append(values)
continue
elif values not in li and values in li:
li.append(values)
continue
elif values not in li and values not in li:
li.reverse()
li.append(values)
li.append(values)
li.reverse()
continue
except Exception as e:
print(e)
return li
``````

``````['red', 'green', 'green'] should equal ['red', 'green']
``````

``````def solution(hints: list)-> list:
item_dict = {}
for index,item in enumerate(hints):
hints[index] = []
hints[index].append(item.split())
hints[index].append(item.split())
hints[index].append(item.split()[-1])
item_dict.setdefault(item.split(),0)
item_dict.setdefault(item.split(), 0)
for item in hints:
if "left" == item[-1]:
item_dict[item] = item_dict[item] - 1
elif "right" == item[-1]:
item_dict[item] = item_dict[item] + 1
item_dict = sorted(item_dict.items(), key=lambda k: k)
return [item for item in item_dict]

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"]

`````` ``````else:
if s == hints[-1]:
raise AttributeError
hints.append(s)
``````

``````def solution(hints: list) -> list:
hints = list(map(lambda x: x.split(" "), hints))
chaos_list = [list([sentence, sentence]) if sentence == "right" else (sentence, sentence) for sentence in hints]

ranked_dict = list(chaos_list)
chaos_list.pop(0)

counter = 0
while len(chaos_list) > 0:
counter += 1
index = counter % len(chaos_list)
if chaos_list[index] == ranked_dict[-1]:
ranked_dict.append(chaos_list[index])
chaos_list.pop(index)
elif chaos_list[index] == ranked_dict:
ranked_dict.insert(0, chaos_list[index])
chaos_list.pop(index)
elif chaos_list[index] == ranked_dict[-1] or chaos_list[index] == ranked_dict:
chaos_list.pop(index)
else:
continue
return ranked_dict
``````
``````def solution(hints: list)-> list:
l1 = []
for a in hints:
if a.split() not in l1:
l1.append(a.split())
if a.split() not in l1:
l1.append(a.split())
for a in hints:
for i in range(len(l1) - 1):
for j in range(i + 1, len(l1)):
if a.split()[-1] == "left" and l1.index(a.split()) < l1.index(a.split()):
l1[i], l1[j]= l1[j], l1[i]
return l1

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"]

``````
``````#!/usr/bin/python
# -*- coding: utf-8 -*-
import re

def solution(hits: list) -> list:
rlt = []
patten = re.compile(r"(\w+) has (\w+) on his (\w+)")
for hit in hits:
match = patten.fullmatch(hit)
e1, e2, direct = match.groups()
if direct not in ['left', 'right']:
raise ValueError("unknown direction")
if direct == 'right':
e1, e2 = e2, e1
index1 = -1
index2 = -1
try:
index1 = rlt.index(e1)
index2 = rlt.index(e2)
except ValueError:
pass
if index1 != -1:
rlt.insert(index1, e2)
elif index2 != -1:
rlt.insert(index2 + 1, e1)
else:
rlt.append(e2)
rlt.append(e1)
# print(hit, rlt)
# print(rlt)
return rlt

if __name__ == '__main__':
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"]``````
``````def solution(hints: list) -> list:
result1 = {}
result2 = []
for x in hints:
if 'left' in x:
result1[x.split(' ')] = x.split(' ')
else:
result1[x.split(' ')] = x.split(' ')
keys = []
values = []
for k, v in result1.items():
keys.append(k)
values.append(v)
for k in keys:
if k not in values:
result2.append(k)
for i in range(len(result1)):
result2.append(result1[result2[i]])
return result2

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"]

``````
``````def solution(hints: list) -> list:
result, result_last = [], []
for i in hints:
index1 = i.find(" ")
index2 = i.find(" ", index1 + 1, len(i))
index3 = i.find(" ", index2 + 1, len(i))
str1, str2, str3 = i[:index1], i[index2 + 1:index3], i[i.rfind(" ") + 1:len(i) + 1]
lists = [str2, str1] if str3 == "left" else [str1, str2]
if len(result) > 0 and (str1 in result or str2 in result):
result.insert(0, lists)
else:
result.append(lists)
for i in range(len(result)):
if len(result_last) == 0:
result_last.append(result[i])
result_last.append(result[i])
elif result[i] == result_last:
result_last.insert(0, result[i])
elif result[i] == result_last[-1]:
result_last.append(result[i])
return result_last
``````
``````def solution(hints: list)-> list:
list1 = []
for hint in hints:
li = hint.split(" ")
if li not in list1:
list1.append(li)
if li not in list1:
if li == "right":
list1.append(li)
else:
list1.insert(0,li)
elif li in list1:
if li =="right":
list1.insert(list1.index(li),li)
else:
list1.insert(list1.index(li)+1,li)
elif li in list1 and li not in list1:
if li == "right":
list1.insert(list1.index(li)+1, li)
else:
list1.insert(list1.index(li), li)
elif li in list1 and li in list1:
continue
return list1

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"]
``````
``````封装一个装饰器
def qianzhi(*args):
def exe(func):
def wrapper():
list_1=[]
for arg in args:
old_list=[str(i).replace(' has ', '|').replace(' on his ', '|').split('|')  for i in arg]
list_1.append(old_list)
for y in list_1:
for i in y:
if 'left' in i:
i.remove('left')
i,i=i,i
if 'right' in i:i.remove('right')
return func(list_1)
return wrapper
return exe

@qianzhi(["d has c on his left","c has b on his left","b has a on his left"],["d has c on his right","c has b on his right","b has a on his right"],["red has green on his right","green has red on his left"])
def solution(old_list: list)-> list:
all_result=[]
new_list=[]
for i in old_list:
for y in i:
if not new_list:
new_list.extend(y)
elif y in new_list and y not in new_list:
new_list.insert(new_list.index(y)+1,y)
elif y in new_list and y not in new_list:
new_list.insert(new_list.index(y), y)
all_result.append(new_list)
new_list = []
return all_result

print(solution())
``````

``````def solution(hints: list) -> list:
result = []
while len(hints) > 0:
i = hints[:]
str_list = i.split(' ')
if len(result) > 0:
if str_list in result and str_list in result:
if 'left' in str_list:
if result.index(str_list) > result.index(str_list):
result[result.index(str_list)], result[result.index(str_list)] = str_list, str_list
hints.remove(i)
else:
hints.remove(i)
else:
if result.index(str_list) < result.index(str_list):
result[result.index(str_list)], result[result.index(str_list)] = str_list, str_list
hints.remove(i)
elif str_list in result and str_list not in result:
if 'left' in str_list:
result.insert(result.index(str_list), str_list)
hints.remove(i)
else:
result.insert(result.index(str_list) + 1, str_list)
hints.remove(i)
elif str_list not in result and str_list in result:
if 'left' in str_list:
result.insert(result.index(str_list) + 1, str_list)
hints.remove(i)
else:
result.insert(result.index(str_list), str_list)
hints.remove(i)
else:
hints, hints[len(hints) - 1] = hints[len(hints) - 1], i
else:
if 'left' in str_list:
result.extend([str_list, str_list])
hints.remove(i)
else:
result.extend([str_list, str_list])
hints.remove(i)
return result
``````

``````def solution(hints: list) -> list:
d = dict((h, h) if h == 'right' else (h, h) for h in map(str.split, hints))
res = list(d.keys() - d.values())
while res[-1] in d: res.append(d[res[-1]])
return res
``````
1 Like

``````def solution(hints: list)-> list:
graph = dict( (a,b) if side=='right' else (b,a) for a,_,b,_,_,side in map(str.split,hints))
result = list(set(graph) - set(graph.values()))
while graph.get(result[-1]) is not None:
result.append(graph.get(result[-1]))
return result
``````