diff --git a/q01_read_data/build.py b/q01_read_data/build.py index e13d2f74..8873c032 100644 --- a/q01_read_data/build.py +++ b/q01_read_data/build.py @@ -1,12 +1,19 @@ +# %load q01_read_data/build.py import yaml def read_data(): - # import the csv file into `data` variable + # import the csv file into variable # You can use this path to access the CSV file: '../data/ipl_match.yaml' # Write your code here - - data = - + + #with open('./data/ipl_match.yaml','r') as yaml_file_read: + yaml_file_read = open('./data/ipl_match.yaml','r') + data = yaml.load(yaml_file_read) + # return data variable return data + +read_data() + + diff --git a/q02_teams/build.py b/q02_teams/build.py index 3cf9d3cf..90207d92 100644 --- a/q02_teams/build.py +++ b/q02_teams/build.py @@ -1,11 +1,21 @@ +# %load q02_teams/build.py # default imports from greyatomlib.python_getting_started.q01_read_data.build import read_data data = read_data() +#data.keys() +#data['info']['teams'] +#a['teams'] + # solution -def teams(data=data): +def teams(data): # write your code here - #teams = + teams = data['info']['teams'] return teams + + + + + diff --git a/q03_first_batsman/build.py b/q03_first_batsman/build.py index 84984081..36e9a36c 100644 --- a/q03_first_batsman/build.py +++ b/q03_first_batsman/build.py @@ -2,12 +2,15 @@ from greyatomlib.python_getting_started.q01_read_data.build import read_data data = read_data() +#a=data['innings'][0]['1st innings']['deliveries'][0][0.1]['batsman'] +#a + # Your Solution def first_batsman(data=data): # Write your code here + name = data['innings'][0]['1st innings']['deliveries'][0][0.1]['batsman'] + return name - - return name diff --git a/q04_count/build.py b/q04_count/build.py index 6cf3dcbc..cc0ee626 100644 --- a/q04_count/build.py +++ b/q04_count/build.py @@ -1,11 +1,41 @@ # Default Imports from greyatomlib.python_getting_started.q01_read_data.build import read_data data = read_data() +# dict lst dict dict v_lst v_dict dict +#x=data['innings'][0]['1st innings']['deliveries'][0]['0.1']['batsman'] +x=data['innings'][0]['1st innings']['deliveries'] +#print(type(x)) +#print(x) + # Your Solution Here -def deliveries_count(data=data): +def deliveries_count(data): # Your code here + count=0 + + for i,v in enumerate(x): + y=x[i] + #print(type(y)) + for k,v in y.items(): + if y[k]['batsman']=='RT Ponting': + count += 1 + + return count + +deliveries_count(data) + + + + + + + + + + + + diff --git a/q05_runs/build.py b/q05_runs/build.py index a250631a..6d8d5547 100644 --- a/q05_runs/build.py +++ b/q05_runs/build.py @@ -1,12 +1,41 @@ +# %load q05_runs/build.py # Default Imports from greyatomlib.python_getting_started.q01_read_data.build import read_data data = read_data() - # Your Solution def BC_runs(data): # Write your code here + + #Defining variable 'a'. This will store the content of 1st innings delivery wise + a=data['innings'][0]['1st innings']['deliveries'] + + #initialising variable runs = 0 + runs = 0 + + #to check the type of next key/index. Accordingly use .key() or index to access the event drilling down in the .yaml file. + print(type(a)) + + #For dicts - To see next level keys while drilling down in the list. + #print(a.keys()) + + #For lists - To access next level index, values while drilling down in the list. + #for i,v in enumerate(a): + # if i<=9: + # print(i,v) + + #Each delivery is stored in a separate index. So for loop to iterate thru the deliveries. + for i,v in enumerate(a): + for k,v in a[i].items(): #To loop thru each delivery viz. 0.1, 0.2 etc in dicts. + #print(a[i][k]['batsman']) #Print batsman name for test purpose + #print(a[i][k]['runs']['batsman']) #Print runs scored by above batsman on each delivery. + if a[i][k]['batsman'] == 'BB McCullum': #Narrowing down on BB McCullum + runs += a[i][k]['runs']['batsman'] #Storing runs scored by BB McCullum in runs variable. + return(runs) + +BC_runs(data) + + - return(runs) diff --git a/q06_bowled_players/build.py b/q06_bowled_players/build.py index 914cb6d2..17a1d5df 100644 --- a/q06_bowled_players/build.py +++ b/q06_bowled_players/build.py @@ -1,11 +1,28 @@ +# %load q06_bowled_players/build.py # Default Imports from greyatomlib.python_getting_started.q01_read_data.build import read_data data = read_data() # Your Solution -def bowled_out(data=data): +def bowled_out(data): + + #assign a with avlues of data which would remain static throughout the problem statement + a = data['innings'][1]['2nd innings']['deliveries']#[7][1.1]['wicket']['kind'] #For testing to access subsequent element. + #print(type(a)) + #print(a) + bowled_players=[] #To store return data + + for i,v in enumerate(a): #looping thru deliveries + for k,v in a[i].items(): #looping thru deliveries viz. 0.1, 0.2 etc. + if 'wicket' in a[i][k].keys(): #narrowing down in dicts which have 'wicket' as key. + if 'bowled' in a[i][k]['wicket']['kind']: #further filtering to narrow dewn on 'bowled' as kind of wicket. + #print(a[i][k]['wicket']['kind'],' ',a[i][k]['wicket']['player_out']) # just to check the player names who were bowled out. + bowled_players.append(a[i][k]['wicket']['player_out']) + + return bowled_players + + +bowled_out(data) - # Write your code here - return bowled_players diff --git a/q07_extras/build.py b/q07_extras/build.py index cdeb803b..d1f8867c 100644 --- a/q07_extras/build.py +++ b/q07_extras/build.py @@ -1,3 +1,4 @@ +# %load q07_extras/build.py # Default Imports from greyatomlib.python_getting_started.q01_read_data.build import read_data data = read_data() @@ -6,9 +7,32 @@ def extras_runs(data=data): # Write your code here + + a = data['innings'][0]['1st innings']['deliveries'] + b = data['innings'][1]['2nd innings']['deliveries'] + + extra_1st_inn = [] + extra_2nd_inn = [] + #For 1st innings + for i,v in enumerate(a): #Tracking delivery by delivery elemnt of list viz. [0],[1],[2] etc. + for k,v in a[i].items(): #Going inside each delivery viz. 0.1, 0.2 etc. + if 'extras' in a[i][k].keys(): #if there's an 'extra' key in the delivery go inside it and get its value. + for key,val in a[i][k]['extras'].items(): + print(a[i][k]['extras'][key]) + extra_1st_inn.append(a[i][k]['extras'][key]) - difference = - + #For second innings (logic same as 1st innings) + for i,v in enumerate(b): + for k,v in b[i].items(): + if 'extras' in b[i][k].keys(): + for key,val in b[i][k]['extras'].items(): + print(b[i][k]['extras'][key]) + extra_2nd_inn.append(b[i][k]['extras'][key]) + difference = len(extra_2nd_inn) - len(extra_1st_inn) + return difference + + +