Skip to content

Instantly share code, notes, and snippets.

@sburns
Last active August 29, 2015 14:01
Show Gist options
  • Save sburns/3780e72a4f6a604deb2f to your computer and use it in GitHub Desktop.
Save sburns/3780e72a4f6a604deb2f to your computer and use it in GitHub Desktop.
Answers to [Aur Saraf's Software Carpentry Instructor Training Assessment](http://teaching.software-carpentry.org/2014/05/15/assessment-for-python-dicts-aur-saraf/)

These are good questions. Q1 covers the very important knowledge that a key has one and only one value. I think Q2 requires a little more python knowledge than what's covered in the dictionary concept map but is otherwise a good exercise in assessing how to iterate over a dictionary and operate on the key, value pairs.

Q1

3

Q2

cities = {
    'Jerusalem': 'Israel',
    'Tel Aviv': 'Israel',
    'Denver': 'USA',
}
reversed = {}
for key, value in cities.items():  # to iterate over key,value tuples, use the .items function
    if value not in reversed:
        reversed[value] = []
    reversed[value].append(key)
print reversed['Israel']
# note this order is variable due to random ordering from .items
['Tel Aviv', 'Jerusalem']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment