In: Computer Science
Python pls
create a function called search_position. This function returns a list.
team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}}
def search_position(team1):
returns
[(5, [('Top', ['Yasuo'])]),
(4, [('Mid', ['Fiora']), ('Support',['Olaf']), ('Jungle',['Shaco'])])
(3, [('Bottom', ['Fiora']), ('Top', ['Olaf'])]),
(2, [('Mid', ['Olaf','Yasuo']), ('Top', ['Shaco'])]),
(1, [('Mid', ['Shaco'])])]
Raw_code:
team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},
'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},
'Yasuo': {'Mid': 2, 'Top': 5},
'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}
}
# search_position function definition
def search_position(team1):
# result variable of list type for storing result
result = []
# checking whether the input team1 is dictionary type or not
if isinstance(team1, dict):
# iterating through each dict item in team1
for key, value in team1.items():
#checking whether each value in dict item is dictionary type or
not
if isinstance(value, dict):
# iterating through each key, value in value dict
for skey, svalue in value.items():
# if result is empty, first element in team1 is added to the
result
if len(result) == 0:
result.append((svalue, [(skey, [key])]))
else:
# iterating through each element in result
for element in result:
# if any main key in element in result equals to sub value in
value
if element[0] == svalue:
# iterating through each subvalue in main key in element in
result
for subelement in element[1]:
# if subkey in value of main key in reuslt is equals to sub key in
value
if subelement[0] == skey:
# append subvalue in value to it
subelement[1].append(key)
break # breaking for loop
else:
# else, appending tuple of subkey and list of subvalue to value of
main key in result
element[1].append((skey, [key]))
break
# if no main key with subvalue in team1 not found
# appending tuple of subvalue and list of tuple of subkey and list
of key
else:
result.append((svalue, [(skey, [key])]))
# storing the result according to the mainkey in result
for i in range(len(result)):
for j in range(i+1, len(result)):
if result[i][0] < result[j][0]:
result[i], result[j] = result[j], result[i]
return result
# calling search_position with team1 as parameter
team = search_position(team1)
# iterating through each element in team and print the
element
for i in team:
print(i)