So first I'll say that this is a code challenge I'm working on, the parameters of which are below.
So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a list of courses from COURSES where the supplied set and the course's value (also a set) overlap. For example, covers({"Python"}) would return ["Python Basics"].
Here's the dict I'm pulling from containing the nested set's for course topics.
COURSES = {
"Python Basics": {"Python", "functions", "variables",
"booleans", "integers", "floats",
"arrays", "strings", "exceptions",
"conditions", "input", "loops"},
"Java Basics": {"Java", "strings", "variables",
"input", "exceptions", "integers",
"booleans", "loops"},
"PHP Basics": {"PHP", "variables", "conditions",
"integers", "floats", "strings",
"booleans", "HTML"},
"Ruby Basics": {"Ruby", "strings", "floats",
"integers", "conditions",
"functions", "input"}}
Now, searching the dict's set's is a pretty strait forward use of for loops, but the challenge sounds like it wants me to create a new set of the course names whose topics match the argument('s) input, and output the result as a list.
def covers(topics):
hold_set = set()
for key in COURSES.keys():
if topics in COURSES[key]:
result = key
hold_set.add(result)
else:
continue
conversion = list(hold_set)
return conversion
Now my first problem was accepting a variable number of arguments, I'm not super familiar with *args / **kwargs, so my attempts to use them in here mostly just resulted in the print function giving me empty lists. The other one was creating an extra unnecessary set to compare with the new one created after searching the subsets within the dict, it seemed redundant an unnecessary as the hold_set already contains names and comparing them with i'd assume, .intersection() before converting the results into a list just nets the same results with more work.
But I digress, right now I'm assuming the main issue here is the functions inability to accept a variable number of arguments regarding topic search parameters (because when submitting the challenge, it says "Didn't get the right output from covers
."), so if anyone could shed light onto how I'd make that possible it's much appreciated as after a couple days my brain has begun to mush and I've wandered off onto reading about other areas of computer science to decompress.
If you want to work with multiple arguments, you can try this:
def covers(*args):
mydict = dict()
for arg in args:
mylist = list()
for k, v in COURSES.items():
if arg in v:
mylist.append(k)
mydict[arg] = mylist
return mydict
Using dict and list comprehensions, you can make this shorter:
def covers(*args):
return {arg: [k for k, v in COURSES.items() if arg in v] for arg in args}
This produces a dictionary with an entry for every string you provide. The value of the entry is a list with all the matching courses. So if you call it like this:
covers('variables', 'Python')
You get the following dict:
{'variables': ['PHP Basics', 'Java Basics', 'Python Basics'], 'Python': ['Python Basics']}
Concerning your problems with multiple arguments: *args catches all positional arguments in a list that are not explicitly declared in the function definition and **kwargs similarly catches all keyword arguments in a dict. So by defining only *args as function argument, all arguments from the function call are stored as list, which can be iterated as usual.