1

Returns all the keys (as a set) from num_dict that have value greater than or equal to min_cutoff.

Parameters:

  • num_dict: dictionary. All the values in num_dict are numeric.
  • min_cutoff: float. Comparison with the num_dict values. Return all keys, where their values >= min_cutoff.
  • set: All keys from num_dict whose values meet the cutoff criterion.

Examples:

keys_geq_cutoff({'Alice': 21, 'Brett': 20, 'Carlos': 31}, 21)
{'Alice', 'Carlos'}

My code:

def keys_geq_cutoff(num_dict, min_cutoff):
    for k, v in num_dict.items():
        if (v >= min_cutoff):
            return(keys_geq_cutoff(num_dict, min_cutoff))

ERROR:

test_keys_geq_cutoff (test_methods.TestPython1)
Traceback (most recent call last):
  File "/usr/src/app/test_methods.py", line 13, in test_keys_geq_cutoff
    result1 = main.keys_geq_cutoff(test_d, 0)
  File "/usr/src/app/main.py", line 4, in keys_geq_cutoff
    return(keys_geq_cutoff(num_dict, min_cutoff))
  File "/usr/src/app/main.py", line 4, in keys_geq_cutoff
    return(keys_geq_cutoff(num_dict, min_cutoff))
  File "/usr/src/app/main.py", line 4, in keys_geq_cutoff
    return(keys_geq_cutoff(num_dict, min_cutoff))
  [Previous line repeated 956 more times]
  File "/usr/src/app/main.py", line 3, in keys_geq_cutoff
    if (v >= min_cutoff):
RecursionError: maximum recursion depth exceeded in comparison
0

3 Answers 3

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:

def keys_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:

def keys_geq_cutoff(num_dict, min_cutoff):
    return {k for k, v in num_dict.items() if v >= min_cutoff}
Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

What's wrong with a one-line function? and that creates a list, not a set
0

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:

def keys_geq_cutoff(num_dict, min_cutoff):
    l = []
    for k, v in num_dict.items():
        if v >= min_cutoff:
            l.append(k)
    return set(l)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.