In: Computer Science
PYTHON PLS
1) Create a function search_by_pos. This function only has one return statement. This function returns a set statement that finds out the same position and same or higher skill number. This function searches the dictionary and returns the same position and same or higher skill level. The function output the set statements that include the position only.
For example
input : dict = {'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_by_pos(dict, 4):
#ONLY ONE RETURN STATEMENT ALLOWED
output : {'Mid', 'Support','Top','Jungle'}
2) Create a function search_by_rank this function calculates the average skill level for each champion and returns the champion's name by highest to the lowest average skill level. If two champions have the same average skill level, it must appear in increasing alphabetical order by name. This function needs to have only one return statement.
For example
input : dict = {'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_by_rank(dict):
#ONLY ONE RETURN STATEMENT ALLOWED
output: ['Yasuo', 'Olaf', 'Fiora', 'Shaco']
Code:-
d1 = {'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_by_pos(di,n): # define function
result=[]# to store the result
for i in di.keys():# iterate over an every item
of main dict
for j in di[i].keys(): #
iterate items inside every item of main dict
if di[i][j]>=n:# check if equal or more then required
value
result.append(j)# if yes the add it into to result
return result# return result
search_by_pos(d1,n) # calling function
from operator import itemgetter # this will used for sorting
dict or list over multiple field
def search_by_rank(di): #main function
result=[] # for store sorted list from
dict
for i in di.keys(): # loop over every item in
main dict
sum1=0# for sum up
skills of every item in main dict
for j in di[i].keys():#
iterate over dict inside dict means dict for every item in main
dict
sum1+=di[i][j] # sumup thier skill
result.append([i,sum1/len(di[i].keys())]) # now add this into
result
#this line will return name in sorted manner as
per your requirment
return [i[0] for i in
sorted(result,key=itemgetter(1,0),reverse=True)]
search_by_rank(di)
Output:---
Note :- sorted(result,key=itemgetter(1,0),reverse=True this will iterate items in result list and return the avrage skills and then name so it will help us to sort value as we need.