Last active
March 12, 2019 00:28
-
-
Save tom-wagner/d6d9b1ecad54fdb456dfe1acaf2d4993 to your computer and use it in GitHub Desktop.
python set playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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