Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Instructions:
- 1. Look at the example database below.
- Each person is represented by a dictionary that maps attribute names to their values.
- For example, to get the first person's first name, you access via this expression:
- >>> example_db[0]['first_name']
- 'Thanadol'
- Test this on your interactive shell. (>>> means what you type. The line after means the result you should see.)
- 2. Try accessing non-existent attributes, like this:
- >>> example_db[0]['department']
- Traceback (most recent call last):
- ...
- KeyError: 'department'
- Observe there is a KeyError. When solving problems, know that you can check whether a key exists by the `in` operator:
- >>> 'salary' in example_db[0]
- True
- >>> 'salary' in example_db[1]
- False
- >>> 'department' in example_db[0]
- False
- This operator will be useful when solving the problems.
- 3. Now, let's look at the problems. There are four (4) problems for you to solve.
- In each problem, you will receive the database (and any additional information) through function arguments.
- Therefore, to test your solutions, you need to create an example database and pass it into the function.
- (I do not recommend using `input` or `print` functions in this homework.)
- See the example test cases below. I've already created an example database called `example_db` for you.
- If you wish to create a new `gunn_db` for your own testing, that's okay.
- I think my test cases are sufficient for preliminary testing though.
- As with any problems we've seen so far, your solution should work for *any* possible database, not just the example.
- I should be able to give you any database via the function argument, and your function should return the correct answer.
- Once you are done solving a problem, uncomment that problem's test cases and run the program.
- If there's no error, that means the test cases are satisfied.
- Each problem is solvable in 10 lines or fewer. Good luck!
- """
- # see example database and test cases below
- # a database is a list of people
- def count_people(db):
- """
- Given a database, return how many people there are in the database.
- """
- # hint: should be a one-liner
- return 0
- def get_mean_salary(db):
- """
- Given a database, find the mean of people's salaries in the database.
- Note that the salary field might be missing from some people. In that case, simply ignore that person.
- """
- # hint: you want to find
- # - the sum of the salaries (for people with salary information)
- # - the number of people who have salary information
- # this will require a loop with running sum and counter variables.
- # then finally you just divide the sum by the number of people and return.
- return 0
- def find_people_with_last_name(db, last_name):
- """
- Given a database and the desired last name, return a list of people objects corresponding to the given last name.
- (It's guaranteed that everyone in the database has name info.)
- """
- # hint: start off with a new empty list. go through each person in the database;
- # if they have the last name you want, just append that person (not just the name) to your empty list.
- # return the list.
- return []
- def sort_by_birth_months(db):
- """
- Given a database, construct a new dictionary mapping birth month (1-12) to list of people's names with that birth month.
- (It's guaranteed that everyone in the database has name and date of birth info.)
- """
- # hint: start with a new dictionary like this:
- birth_month_to_people = {
- 1: [],
- 2: [],
- 3: []
- # ...
- }
- # then, go through each person in the database:
- # - get their full name by concatenating the attributes first_name and last_name
- # - get their birth month by looking at date_of_birth, index 1
- # then do something like:
- # birth_month_of_people[found_birth_month].append(found_full_name)
- return birth_month_to_people
- # -------------------------------
- # Test cases
- # -------------------------------
- # example database
- example_db = [
- {
- 'first_name': 'Thanadol',
- 'last_name': 'Chomphoochan',
- 'aliases': ['Pleng', 'Aqua'],
- 'date_of_birth': (2021, 11, 27),
- 'salary': 84000,
- 'position': 'Lecturer'
- },
- {
- 'first_name': 'Apichaya',
- 'last_name': 'Chomphoochan',
- 'aliases': ['Piano'],
- 'date_of_birth': (2023, 8, 21),
- # salary missing
- 'position': 'Student'
- },
- {
- 'first_name': 'Siraphob',
- 'last_name': 'Wanavejkul',
- 'aliases': ['Gunn'],
- 'date_of_birth': (2000, 9, 3),
- 'salary': 65000,
- 'position': 'Affiliated Artist'
- },
- {
- 'first_name': 'Imaginary',
- 'last_name': 'Person',
- 'aliases': [],
- 'date_of_birth': (2000, 11, 3),
- 'salary': 50000,
- 'position': 'Filler'
- },
- ]
- # # problem 1
- # assert count_people(example_db) == 4
- # # problem 2
- # assert get_mean_salary(example_db) == (84000+65000+50000)/3 # salary is missing for Apichaya Comphoochan
- # # problem 3
- # assert find_people_with_last_name(example_db, 'Chomphoochan') == [
- # {
- # 'first_name': 'Thanadol',
- # 'last_name': 'Chomphoochan',
- # 'aliases': ['Pleng', 'Aqua'],
- # 'date_of_birth': (2021, 11, 27),
- # 'salary': 84000,
- # 'position': 'Lecturer'
- # },
- # {
- # 'first_name': 'Apichaya',
- # 'last_name': 'Chomphoochan',
- # 'aliases': ['Piano'],
- # 'date_of_birth': (2023, 8, 21),
- # # salary missing
- # 'position': 'Student'
- # }
- # ]
- # # problem 4
- # assert sort_by_birth_months(example_db) == {
- # 1: [],
- # 2: [],
- # 3: [],
- # 4: [],
- # 5: [],
- # 6: [],
- # 7: [],
- # 8: ['Apichaya Chomphoochan'],
- # 9: ['Siraphob Wanavejkul'],
- # 10: [],
- # 11: ['Thanadol Chomphoochan', 'Imaginary Person'],
- # 12: []
- # }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement