A generic solution for the first question: "" def parsed_sum(item): total = 0 if type(item) == dict: for _, val in item.items(): total += parsed_sum(val) elif type(item) in [list, tuple, set]: for val in item: total += parsed_sum(val) else: total += item return total my_dict = { 'v1': 10, 'v2': { 'v3': 20, 'v6': [30], 'v7': { 'v8': 40 } }, 'v5': [25, 25], 'v6': (25, 25), 'v7': {50, 100, 50} } print(f'Total: {parsed_sum(my_dict)}') """
sum_val=0 def sum_of_vals_recursive(my_dict): global sum_val if (type(my_dict)==dict):#processing logic for dictionary for k,v in my_dict.items(): if(type(v)==int or type(v)==float):#if the value is a simple int/float sum_val=sum_val + v else: sum_of_vals_recursive(v)#this will get invoked during 1st nested level else:#this bit of logic is for nested ones, where it is list, set, tuple from 1st nesting for x in my_dict:#iteration logic for set, tuple, list if(type(x)==int or type(x)==float):#type check of elements of the value of each iterated from previous step sum_val = sum_val + x else: sum_of_vals_recursive(x)#this one is for handling next set of nestng and then, it will again follow return sum_val; inp_dict = {'ireland':100,'india':[200,300,[400,[200,200],400]],'uk':{'scotland':[50]}} print(sum_of_vals_recursive(inp_dict))
for SQL query why u complicated here is a simple solution : select e.event_name , p.participant_name from participant p join Events e on e.event_id = p.event_id group by e.event_name , p.participant_name having count(e.event_id) > 1;
It easy to answer when you are not under pressure of an interviwer. A simple problem could become enormous if you do not have a the best control of your emotions.
On top of that, there is a possibility that there are 2 participants with the same name but different participant ids. In that case, your solution will just return 1 name instead of 2. That is why the interviewer stated it’s a seeming simple yet tricky question to solve.
Its a fabulous initiative. Please continue with this concept.
Great content. Pls never stop posting
A generic solution for the first question:
""
def parsed_sum(item):
total = 0
if type(item) == dict:
for _, val in item.items():
total += parsed_sum(val)
elif type(item) in [list, tuple, set]:
for val in item:
total += parsed_sum(val)
else:
total += item
return total
my_dict = {
'v1': 10,
'v2': {
'v3': 20,
'v6': [30],
'v7': {
'v8': 40
}
},
'v5': [25, 25],
'v6': (25, 25),
'v7': {50, 100, 50}
}
print(f'Total: {parsed_sum(my_dict)}')
"""
the first Python solution is not correct here: for x in v: sum += x, because X could be list, dict again
Informative one!
sum_val=0
def sum_of_vals_recursive(my_dict):
global sum_val
if (type(my_dict)==dict):#processing logic for dictionary
for k,v in my_dict.items():
if(type(v)==int or type(v)==float):#if the value is a simple int/float
sum_val=sum_val + v
else:
sum_of_vals_recursive(v)#this will get invoked during 1st nested level
else:#this bit of logic is for nested ones, where it is list, set, tuple from 1st nesting
for x in my_dict:#iteration logic for set, tuple, list
if(type(x)==int or type(x)==float):#type check of elements of the value of each iterated from previous step
sum_val = sum_val + x
else:
sum_of_vals_recursive(x)#this one is for handling next set of nestng and then, it will again follow
return sum_val;
inp_dict = {'ireland':100,'india':[200,300,[400,[200,200],400]],'uk':{'scotland':[50]}}
print(sum_of_vals_recursive(inp_dict))
for SQL query why u complicated here is a simple solution :
select e.event_name , p.participant_name
from participant p
join Events e
on e.event_id = p.event_id
group by e.event_name , p.participant_name
having count(e.event_id) > 1;
It easy to answer when you are not under pressure of an interviwer. A simple problem could become enormous if you do not have a the best control of your emotions.
On top of that, there is a possibility that there are 2 participants with the same name but different participant ids. In that case, your solution will just return 1 name instead of 2.
That is why the interviewer stated it’s a seeming simple yet tricky question to solve.