What Is Wrong In My Solution And Algorithm
Solution 1:
Right now you are calling the function again in your return statement with exactly the same arguments and hence the RecursionError
. You need to collect the keys inside a set and then return that set:
defkeys_geq_cutoff(num_dict, min_cutoff):
res = set()
for k, v in num_dict.items():
if (v >= min_cutoff):
res.add(k)
return res
alternatively, a nice set-comprehension could be in use:
defkeys_geq_cutoff(num_dict, min_cutoff):
return {k for k, v in num_dict.items() if v >= min_cutoff}
Solution 2:
Do you really need a function for that? The problem looks quite similiar to: Return all the keys (as set) from num_dict that have value greater than or equal to min_cutoff
you could use a one coder for this:
num_dict = {'Alice': 21, 'Brett': 20, 'Carlos': 31}
min_cutoff = 21
num_dict_keys = [k for k, v in num_dict.items() if v >= min_cutoff ]
print(num_dict_keys)
Solution 3:
Basically you're having a recursion error, which means that you're calling the function itself in the body of the function without providing an exit clause, so the function will go on for ever. to solve this you have to make an exit clause (for example an if statement)
But you probably don't want to use that technique
I think this is what you're trying to do:
defkeys_geq_cutoff(num_dict, min_cutoff):
l = []
for k, v in num_dict.items():
if v >= min_cutoff:
l.append(k)
returnset(l)
Post a Comment for "What Is Wrong In My Solution And Algorithm"