Skip to content

Instantly share code, notes, and snippets.

@tom-wagner
Last active March 12, 2019 00:28
Show Gist options
  • Save tom-wagner/d6d9b1ecad54fdb456dfe1acaf2d4993 to your computer and use it in GitHub Desktop.
Save tom-wagner/d6d9b1ecad54fdb456dfe1acaf2d4993 to your computer and use it in GitHub Desktop.
python set playground
# Let's write some functions to explore set math a bit more.
# We're going to be using this COURSES dict in all of the examples. Don't change it, though!
# 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"].
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"}
}
def covers(topics):
res = set()
for (key, val) in COURSES.items():
if topics & val:
res.update(topics & val)
return list(res)
def covers_refactor(topics):
return [key for (key, val) in COURSES.items() if topics & val]
covers({'Python'}) # ['Python Basics']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment